Spring Cloud微服务(7)之Feign服务之间调用
2017-08-01 16:09
746 查看
1.简介
微服务架构服务实例众多,服务与服务之间如何调用,Spring Cloud提供了解决方案:伪装者 Feign。
Feign 是 Spring Cloud 的一个组件,也是一个WebService客户端,用于服务之间的调动。
2. 如何使用
第一步:服务之间调用
本例需要创建三个工程:
eureka-server 注册中心服务(项目创建参照第三节网关)
product-service 产品服务(最基础的客户端服务,提供一个rest接口即可。前面第三节已创建,可参考,不需要做任何修改)
order-service-feignclient 订单服务(测试服务调用)
order-service-feignclient 工程目录如下图:
(1)pom.xml增加依赖
(2)增加接口和 @FeignClient 注解
value设置要调用服务的服务名。Feign会根据服务名去注册中心查找服务url
(3)启动类增加 @EnableFeignClient开启Feign功能,新增Rest接口用于调用产品服务。
(4)启动服务验证
启动成功后请求 /product/hello,结果成功调用产品服务并返回结果。
第二步:增加 Hystrix 断路熔断机制
(1)增加 Feign接口实现类,实现方法作为断路器的回调方法。
(3)启动服务验证。
启动成功后将 product-sevice 服务关闭,再次请求 /product/hello,结果发现报异常。
发现断路器并没有起作用,而是直接抛异常。
带着疑惑查了下官方API,官方文档中大致意思是feign默认是启用hystrix的,然而测试的结果是并没有。但发现了一段配置
这个配置意思很明确了吧,将false改为true,试试又不犯法,结果。。。
对,就是你认为的那样,成功了!
还有一个地方要补充下:
FigenClient已经包含了Hystrix,所有启动类不需要再开启 @EnableCircuitBreaker。
微服务架构服务实例众多,服务与服务之间如何调用,Spring Cloud提供了解决方案:伪装者 Feign。
Feign 是 Spring Cloud 的一个组件,也是一个WebService客户端,用于服务之间的调动。
2. 如何使用
第一步:服务之间调用
本例需要创建三个工程:
eureka-server 注册中心服务(项目创建参照第三节网关)
product-service 产品服务(最基础的客户端服务,提供一个rest接口即可。前面第三节已创建,可参考,不需要做任何修改)
order-service-feignclient 订单服务(测试服务调用)
order-service-feignclient 工程目录如下图:
(1)pom.xml增加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
(2)增加接口和 @FeignClient 注解
package com.hole.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by helei on 2017/7/16. */ @FeignClient(value = "product-service",fallback = ProductServiceFeignDaoHystrix.class) public interface ProductServiceFeignDao { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello(); }
@FeignClient(value = "product-service"
value设置要调用服务的服务名。Feign会根据服务名去注册中心查找服务url
(3)启动类增加 @EnableFeignClient开启Feign功能,新增Rest接口用于调用产品服务。
package com.hole; import com.hole.feign.ProductServiceFeignDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @EnableDiscoveryClient @SpringBootApplication @RestController @EnableFeignClients public class OrderServiceFeignclientApplication { @Autowired private ProductServiceFeignDao productServiceFeignDao; public static void main(String[] args) { SpringApplication.run(OrderServiceFeignclientApplication.class, args); } @RequestMapping(value = "/product/hello",method = RequestMethod.GET) public String getHelloFromProductService(){ return productServiceFeignDao.hello(); } }
(4)启动服务验证
启动成功后请求 /product/hello,结果成功调用产品服务并返回结果。
第二步:增加 Hystrix 断路熔断机制
(1)增加 Feign接口实现类,实现方法作为断路器的回调方法。
package com.hole.feign; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; /** * Created by helei on 2017/7/16. */ @Component public class ProductServiceFeignDaoHystrix implements ProductServiceFeignDao { @Override public String hello() { return "服务不见鸟,稍后再试!"; } }(2)Feign接口的 @FeignClient注解增加 fallback参数,指向接口的实现类。
package com.hole.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by helei on 2017/7/16. */ @FeignClient(value = "product-service",fallback = ProductServiceFeignDaoHystrix.class) public interface ProductServiceFeignDao { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello(); }
(3)启动服务验证。
启动成功后将 product-sevice 服务关闭,再次请求 /product/hello,结果发现报异常。
发现断路器并没有起作用,而是直接抛异常。
带着疑惑查了下官方API,官方文档中大致意思是feign默认是启用hystrix的,然而测试的结果是并没有。但发现了一段配置
feign.hystrix.enabled=false
这个配置意思很明确了吧,将false改为true,试试又不犯法,结果。。。
对,就是你认为的那样,成功了!
还有一个地方要补充下:
FigenClient已经包含了Hystrix,所有启动类不需要再开启 @EnableCircuitBreaker。
相关文章推荐
- 使用Spring cloud Feign在后台服务之间调用传递Multipart无法传递的问题
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 笔记:Spring Cloud Feign 声明式服务调用
- spring cloud feign实现远程调用服务传输文件的方法
- 关于Dalston.SR3版本的spring cloud,用feign客户端调用服务hystrix不能生效的问题
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
- 2、springcloud微服务:基于Feign的服务调用
- spring cloud feign 调用服务注意问题
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- spring cloud中微服务之间的调用以及eureka的自我保护机制详解
- 【Spring Cloud】--声明式服务调用Feign
- Spring Cloud中如何保证各个微服务之间调用的安全性
- SpringCloud使用Feign进行服务调用
- Spring Cloud Feign 声明式服务调用
- spring cloud Feign(声明式服务调用)
- SpringCloud : 两个微服务进程之间通信(远程调用)
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- Spring Cloud中如何保证各个微服务之间调用的安全性(下篇)
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务