您的位置:首页 > 编程语言 > Java开发

SpringCloud之声明式服务调用Spring Cloud Feign实例

2018-01-11 16:06 1301 查看

一、Feign简介

Feign是一个声明式的web服务客户端,它使得写web服务变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,包括Feign 注解和JAX-RS注解。Feign同时支持可插拔的编码器和解码器。spring cloud对Spring mvc添加了支持,同时在spring web中次用相同的HttpMessageConverter。当我们使用feign的时候,spring cloud 整和了Ribbon和eureka去提供负载均衡。

它基于Netflix Feign实现,整合了Spring Cloud Ribbon与Spring Cloud Hystrix,除了提供这两者的强大功能之外,它还提供了一种声明式的Web服务客户端定义方式。

二、前期准备

一个服务注册中心,EUREKASERVER,端口为5555;
HELLOSERVER工程跑了三个实例,端口分别为5556、5557、5558,分别向服务注册中心注册;

三、实例

(1)pom.xml,在 pom.xml中引入spring-cloud-starter-eureka和spring-cloud-starter-feign依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>(2)application.yml
同Ribbon实现的服务消费者一样,需要在application.yml中指定服务注册中心,并定义自身的服务名为service-feign, 为了方便本地调试与之前的Ribbon消费者区分,端口使用5565。

server:
port: 5565

spring:
application:
name: service-feign

eureka:
client:
serviceUrl:
defaultZone: http://localhost:5555/eureka/(3)入口类 @EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudfeignApplication {
//通过@EnableFeignClients注解开启Spring Cloud Feign的支待功能。
public static void main(String[] args) {
SpringApplication.run(SpringcloudfeignApplication.class, args);
}
}(4)定义HelloService接口
@FeignClient("helloserver") //是helloserver中的服务注册名称,不区分大小写
public interface HelloService {
@RequestMapping("/hello")
String hello();
}helloserver对应的服务如下图:



(5)入口类,HelloController来实现对Feign客户端的调用

@RestController
public class HelloController {

@Autowired
HelloService helloService;

@RequestMapping(value = "/feign-hello", method = RequestMethod.GET)
public String hello(){
return helloService.hello();
}

}(6)服务注册中心http://localhost:5555/



(7)访问:http://localhost:5565/feign-hello







(8)同样实现了与Ribbon一样的负载均衡的效果。

四、Ribbon与Feign的区别

Ribbon

Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon。

Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> 启动类:
@EnableDiscoveryClient //发现服务能力
@SpringBootApplication
public class SpringcloudribbonApplication {

public static void main(String[] args) {
SpringApplication.run(SpringcloudribbonApplication.class, args);
}

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
} 总结:

第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;
第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了三种策略:轮询、断路器和根据响应时间加权。
没有像Feign那样做服务配置。

Feign

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

@EnableFeignClients //开启feign
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudfeignApplication {

public static void main(String[] args) {
SpringApplication.run(SpringcloudfeignApplication.class, args);
4000

}
}使用@FeignClient("helloserver")注解来绑定该接口对应compute-service服务
@FeignClient("helloserver") //是helloserver中的服务注册名称,不区分大小写
public interface HelloService {
@RequestMapping("/hello")
String hello();
}通过Spring MVC的注解来配置helloserver服务下的具体实现,注意:定义的url 必须与提供服务的url一致,包括请求方式、参数名。
总结:

我们使用Feign提供的注解编写HTTP接口的客户端代码非常简单, 只需要声明一个Java接口加上少量注解即可完成。
Feign会帮我们处理好一切. 根据我们的接口声明, Feign会在Spring容器启动之后, 将生成的代理类注入, 所以我们不需要写HTTP调用的实现代码就能完成REST接口的调用。
Feign服务客户端定义的请求url必须与服务提供者url一致。
Feign服务客户端中的接口名、返回对象可以任意定义。但对象中的属性类型和属性名必须一致,与两个对象中的属性顺序和数量无关。
启动 Eureka注册中心、服务提供者、Feign服务客户端,然后 Eureka注册中心挂掉时,Feign服务客户端消费服务是不受影响的。


新手一枚,欢迎拍砖~ ~ ~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息