Spring Cloud构建微服务架构服务消费Feign

Spring Cloud Feign

Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。咱们只须要经过建立接口并用注解来配置它既可完成对Web服务接口的绑定。它具有可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。html

下面,咱们经过一个例子来展示Feign如何方便的声明对eureka-client服务的定义和调用。web

下面的例子,咱们将利用以前构建的eureka-server做为服务注册中心、eureka-client做为服务提供者做为基础。而基于Spring Cloud Ribbon实现的消费者,咱们能够根据eureka-consumer实现的内容进行简单改在就能完成,具体步骤以下:spring

  • 根据eureka-consumer复制一个服务消费者工程,命名为:eureka-consumer-feign。在pom.xml中增长下面的依赖:
 
    
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
...
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.cloud</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-cloud-starter-feign</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>
</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
  • 修改应用主类。经过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能:
 
    
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
 
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
  • 建立一个Feign的客户端接口定义。使用@FeignClient注解来指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就能够来绑定服务提供方的REST接口,好比下面就是绑定eureka-client服务的/dc接口的例子:
 
    
@FeignClient("eureka-client")
public interface DcClient {
 
@GetMapping("/dc")
String consumer();
 
}
  • 修改Controller。经过定义的feign客户端来调用服务提供方的接口:
 
    
@RestController
public class DcController {
 
@Autowired
DcClient dcClient;
 
@GetMapping("/consumer")
public String dc() {
return dcClient.consumer();
}
 
}

经过Spring Cloud Feign来实现服务调用的方式更加简单了,经过@FeignClient定义的接口来统一的生命咱们须要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用便可。因为Feign是基于Ribbon实现的,因此它自带了客户端负载均衡功能,也能够经过Ribbon的IRule进行策略扩展。另外,Feign还整合的Hystrix来实现服务的容错保护,在Dalston版本中,Feign的Hystrix默认是关闭的。待后文介绍Hystrix带领你们入门以后,咱们再结合介绍Feign中的Hystrix以及配置方式。架构

在完成了上面你的代码编写以后,读者能够将eureka-server、eureka-client、eureka-consumer-feign都启动起来,来跟踪观察eureka-consumer-feign服务是如何消费eureka-client服务的/dc接口的,而且也能够经过启动多个eureka-client服务来观察其负载均衡的效果。app

Spring <wbr>Cloud构建微服务架构服务消费Feign

 

从如今开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,但愿能够帮助更多的好学者。你们来一块儿探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源负载均衡