远程调用步骤FeignClient
时间: 2023-11-14 12:08:50 浏览: 108
使用FeignClient进行远程调用的步骤如下:
1. 在pom.xml中添加Feign依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```
2. 在启动类上添加@EnableFeignClients注解开启FeignClient:
```java
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
3. 创建一个接口,并在接口上添加@FeignClient注解,指定要调用的服务名:
```java
@FeignClient(name = "service-provider")
public interface HelloService {
@GetMapping("/hello")
String hello();
}
```
4. 在需要调用远程服务的地方注入该接口,并调用其中的方法即可:
```java
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.hello();
}
}
```
阅读全文
相关推荐

















