博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud OpenFeign详解
阅读量:4178 次
发布时间:2019-05-26

本文共 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标注的参数

  • name, value (默认""),两者等价
  • qualifier (默认"")
  • url (默认"")
  • decode404 (默认false)
  • configuration (默认FeignClientsConfiguration.class)
  • fallback (默认void.class)
  • fallbackFactory (默认void.class)
  • path (默认"")
  • primary (默认true)

2) @FeignClient标注的configuration参数

@FeignClient标注的configuration参数,默认是通过FeignClientsConfiguration类定义的,可以配置Client,Contract,Encoder/Decoder等。

FeignClientsConfiguration类中的配置方法及默认值如下:

  • feignContract: SpringMvcContract
  • feignDecoder: ResponseEntityDecoder
  • feignEncoder: SpringEncoder
  • feignLogger: Slf4jLogger
  • feignBuilder: Feign.Builder
  • feignClient: LoadBalancerFeignClient(开启Ribbon时)或默认的HttpURLConnection

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标注的参数

  • value, basePackages (默认{})
  • basePackageClasses (默认{})
  • defaultConfiguration (默认{})
  • clients (默认{})

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/

你可能感兴趣的文章
Linux内核-------同步机制(二)
查看>>
面试题31-------连续子数组的最大和(数组)
查看>>
epoll 实现Chat
查看>>
21. Merge Two Sorted Lists(链表)
查看>>
2. Add Two Numbers(链表)
查看>>
637. Average of Levels in Binary Tree(Tree)
查看>>
226. Invert Binary Tree(Tree)
查看>>
328. Odd Even Linked List(链表)
查看>>
617. Merge Two Binary Trees(Tree)
查看>>
700. Search in a Binary Search Tree(Tree)
查看>>
515. Find Largest Value in Each Tree Row(Tree)
查看>>
897. Increasing Order Search Tree(Tree)
查看>>
199. Binary Tree Right Side View(Tree)
查看>>
230. Kth Smallest Element in a BST(Tree)
查看>>
求字符串的最长回文串-----Manacher's Algorithm 马拉车算法
查看>>
回溯法常用的解题模板和常见题型
查看>>
深入分析Java I/O 的工作机制
查看>>
动态规划的套路----左神
查看>>
KMP算法简解
查看>>
左神算法课进阶版总结
查看>>