使用@FeignClient来一个demo
时间: 2023-07-08 07:49:18 浏览: 51
好的,下面是一个简单的使用@FeignClient的demo:
首先,需要在pom.xml中添加Feign的依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```
然后,在启动类上添加@EnableFeignClients注解,开启Feign客户端:
```java
@SpringBootApplication
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
接着,创建一个Feign客户端接口,使用@FeignClient注解指定要调用的服务名称和URL:
```java
@FeignClient(name = "demo-service", url = "http://localhost:8080")
public interface DemoClient {
@GetMapping("/hello")
String sayHello();
}
```
最后,在需要调用服务的地方注入该接口,直接调用方法即可:
```java
@RestController
public class DemoController {
@Autowired
private DemoClient demoClient;
@GetMapping("/test")
public String test() {
String result = demoClient.sayHello();
return result;
}
}
```
以上就是一个简单的使用@FeignClient的demo。在实际使用中,还可以使用@PathVariable、@RequestParam等注解来传递参数。