OpenFeign 是Spring在feign的基础上做的改造,是 声明式的伪RPC,可以支持OKHTTP
1,创建test-user项目
依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
配置application.properties
spring.application.name=test-user
test-user.ribbon.listOfServers=\
localhost:8080,localhost:8083
代码示例:
@FeignClient("test-user")
public interface UserServiceFeignService {
@GetMapping("/sayHello") //这里的"/sayHello是被调用方暴露出来的接口"
public String sayHello();
}
@RestController
public class UserServiceFeignController {
@Autowired
UserServiceFeignService userServiceFeignService;
@GetMapping("/testSayHelloByFeign")
public String sayHello(){
return userServiceFeignService.sayHello();
}
}
/**
* 需要加上EnableFeignClients这样的注解
*/
@EnableFeignClients
@SpringBootApplication
public class TestUserApplication {
public static void main(String[] args) {
SpringApplication.run(TestUserApplication.class, args);
}
}
2,被调用方集群项目:spring-boot-user-demo 跟前一篇文章Spring Boot Ribbon 测试一样,两个Node做集群。 项目启动,调用 http://localhost:8081/testSayHelloByFeign,
spring-boot-user-demo项目控制台如下: