本文共 3996 字,大约阅读时间需要 13 分钟。
作为Spring Cloud的子项目之一,Spring Cloud OpenFeign以将OpenFeign集成到Spring Boot应用中的方式,为微服务架构下服务之间的调用提供了解决方案。首先,利用了OpenFeign的声明式方式定义Web服务客户端;其次还更进一步,通过集成Ribbon或Eureka实现负载均衡的HTTP客户端。
Spring Cloud OpenFeign的最新版本是2018.8发布的2.0.1.RELEASE。
在Spring Cloud OpenFeign中,除了OpenFeign自身提供的标注(annotation)之外,还可以使用JAX-RS标注,或者Spring MVC标注。下面还是以OpenFeign标注为例介绍用法。
1. OpenFeign的标注@FeignClient和@EnableFeignClients
OpenFeign提供了两个重要标注@FeignClient和@EnableFeignClients。
@FeignClient标注用于声明Feign客户端可访问的Web服务。
@EnableFeignClients标注用于修饰Spring Boot应用的入口类,以通知Spring Boot启动应用时,扫描应用中声明的Feign客户端可访问的Web服务。
1) @FeignClient标注的参数
2) @FeignClient标注的configuration参数
@FeignClient标注的configuration参数,默认是通过FeignClientsConfiguration类定义的,可以配置Client,Contract,Encoder/Decoder等。
FeignClientsConfiguration类中的配置方法及默认值如下:
3) 定制@FeignClient标注的configuration类
@FeignClient标注的默认配置类为FeignClientsConfiguration,我们可以定义自己的配置类如下:
@Configurationpublic class MyConfiguration { @Bean public Contract feignContract(...) {...} @Bean public Encoder feignEncoder() {...} @Bean public Decoder feignDecoder() {...} ...}
然后在使用@FeignClient标注时,给出参数如下:
@FeignClient(name = "myServiceName", configuration = MyConfiguration.class, ...)public interface MyService { @RequestMapping("/") public String getName(); ...}
当然,定制@FeignClient标注的configuration类还可以有另一个方法,直接配置application.yaml文件即可,示例如下:
feign: client: config: feignName: myServiceName connectTimeout: 5000 readTimeout: 5000 loggerLevel: full encoder: com.example.MyEncoder decoder: com.example.MyDecoder contract: com.example.MyContract
4) @EnableFeignClients标注的参数
2. 使用OkHttpClient作为client的Feign客户端
在FeignAutoConfiguration的预配置基础上,要使用OkHttpClient作为client的Feign客户端非常简单,步骤如下:
1) pom.xml中,加入对OkHttp的依赖
com.squareup.okhttp3 okhttp 3.10.0
2) application.yaml中
feign: okhttp: enabled: true
或者application.properties中,
feign.okhttp.enabled = true
3) 必要时,还可以在Spring配置类中定义一个Spring Bean方法,返回OkHttpClient对象,以定制OkHttpClient。
3. 使用Apache HttpComponents HttpClient作为client的Feign客户端
在FeignAutoConfiguration的预配置基础上,要使用Apache HttpComponents HttpClient作为client的Feign客户端非常简单,步骤如下:
1) pom.xml中,加入对Apache HttpClient的依赖 (略)
2) application.yaml中,
feign: httpclient: enabled: true
或者application.properties中,
feign.httpclient.enabled = true
3) 必要时,定义Spring Bean定制返回一个ClosableHttpClient对象
4. 应用示例
1) 创建Spring Boot应用
在应用中,通过Feign客户端访问另一个服务(localhost:8888)
2) pom.xml
org.springframework.cloud spring-cloud-openfeign 2.0.1.RELEASE pom import org.springframework.cloud spring-cloud-starter-openfeign
3) 定义服务接口
@FeignClient(name="myServiceName", url="localhost:8888")public interface MyService { @RequestMapping("/") public String getName();}
4) 定义访问Web服务的REST控制器类
@RestControllerpublic class MyController { @Autowired MyService myService; @RequestMapping("/hello") public String helloName() { return myService.getName(); }}
5) 编写Spring Boot应用的入口类
@SpringBootApplication@EnableFeignClientspublic class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}
参考链接:
https://cloud.spring.io/spring-cloud-openfeign/
http://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.0.1.RELEASE/single/spring-cloud-openfeign.html https://github.com/spring-cloud/spring-cloud-openfeign转载地址:http://qmlai.baihongyu.com/