1.OpenFeign
openfeign和dubbo很像。 例如,消费者控制器可以调用提供程序的服务层方法,但不是。 似乎只能编写提供程序的控制器,也就是提供程序项目的控制器接口。 消费者调用此接口方法等效于调用提供程序控制器1.1Feign能干什么?
Feign是指方便创建java Http客户端。
在使用Ribbon RestTemplate时,利用RestTemplate对http请求的封装处理,形成模板化的调用方法。 但是,在实际开发中,可能存在多个对依赖服务的依赖调用,并且通常会多次调用一个借口,因此通常会为每个微服务封装客户端类以包装这些依赖服务的调用。 因此,Feign在此基础上进行了进一步的封装,帮助他定义和实现了依赖服务的接口。 在Feign实现中,只需在dao接口上创建和使用注释设置Mapper注释,现在在微服务接口上创建Feign注释,就可以设置到服务提供商的接口
1.2功能区与功能区集成
利用Ribbon维护Payment的服务列表信息,通过轮询实现了客户端的负载平衡。 与Ribbon不同,铜鼓feign定义了服务绑定接口,并以声明性的方式优雅轻松地调用服务1.3使用OpenFeign
新的用户模块. Feign附带负载平衡配置,因此不需要手动配置
1.3.1 cloud-consumer-feign-order 80http://www.Sina.com /
从属关系! --- open feign---- dependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-open feidign artifact er---eureka客户端--- dependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-Netflix-eureka -引入自己定义的api通用软件包并访问entity-- groupid com.kriss Wen.cloud/groud 可以用Payment支付artifactidversion $ { project.project } ependencydependencygroupidorg.spring framework.boot/groupidadarg artifact id/ependencydependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-actuator/arator ependencydependencygroupidorg.project lombok/groupidartifactidlombok/artifactidoptionaltrue/optional/ependencydependended
tId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>1.3.3编写配置文件
server: port: 80spring: application: name: cloud-consumer-feign-serviceeureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/1.3.4编写启动类
@SpringBootApplication@EnableFeignClients //开启使用feign客户端的功能public class CustomerFeignMain80 { public static void main(String[] args) { SpringApplication.run(CustomerFeignMain80.class,args); }}1.3.5编写Feign客户端
@FeignClient(value = "cloud-payment-service") //标识当前接口就是一个feign客户端,并且指定调用哪一个微服务public interface PaymentFeignService { @GetMapping("payment/{id}") public CommonResult<Payment> findPaymentById(@PathVariable("id") Long id);}}1.3.6编写Controller
@RestControllerpublic class CustomerFeignController { @Autowired PaymentFeignService paymentFeignService; @GetMapping("consumer/feign/payment/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){ return paymentFeignService.getPaymentById(id); }}//localhost/consumer/feign/payment/12.服务降级
2.1局部服务降级(在服务提供方设置)
一般服务降级放在消费端,即 消费者端 ,但是提供者端一样能使用。
首先提供者,即8001 先从自身找问题,设置自身调用超时的峰值,峰值内正常运行,超出峰值需要有兜底的方法处理,作服务降级fallback
(1)在8001服务提供方引入Hystrix依赖
(2)对 8001 的service进行配置(对容易超时的方法进行配置) :
@Servicepublic class PaymentService { /** * 可以正常访问的方法 * @param id * @return */ public String paymentInfo_Ok(Integer id){ return "线程池:" + Thread.currentThread().getName() + " ,paymentInfo_OK,id:" + id; } /** 超时访问的方法 */ @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = { //设置峰值,超过 3 秒,就会调用兜底方法,这个时间也可以由feign控制 @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") }) public String paymentInfo_Timeout(Integer id){ int interTime = 5; //int i = 10/0; try{ TimeUnit.SECONDS.sleep(interTime); }catch (Exception e){ e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + " ,paymentInfo_Timeout,id:" + id + "耗时" + interTime + "秒钟"; } // 定义服务出现异常之后,兜底的方法 public String timeoutHandler(Integer id){ return "服务异常,请重试......"; }}(3)在启动类上开启服务熔断
@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreaker //开启服务熔断public class HystrixPaymentApplication { public static void main(String[] args) { SpringApplication.run(HystrixPaymentApplication.class,args); }}(4)启动8001服务测试
(5)现在我们改动服务方里面的代码(模拟服务出现异常,而不是服务调用超时)
总结:
我们发现。只要是我们服务不可用了(调用超时、内部错误),都可以用降级来处理。