openfeign例子
时间: 2023-12-16 19:27:32 浏览: 145
以下是一个使用OpenFeign调用Restful服务的例子:
1.添加依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```
2.创建Feign接口
```java
@FeignClient(name = "provider-service")
public interface ProviderService {
@GetMapping("/hello")
String hello();
}
```
3.在启动类上添加@EnableFeignClients注解开启Feign功能
```java
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
```
4.在Controller中注入Feign接口并调用
```java
@RestController
public class ConsumerController {
@Autowired
private ProviderService providerService;
@GetMapping("/hello")
public String hello() {
return providerService.hello();
}
}
```
阅读全文