博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第四篇: 熔断器(Ribbon+Feign)(Greenwich版本)
阅读量:4180 次
发布时间:2019-05-26

本文共 4651 字,大约阅读时间需要 15 分钟。

一、增加Ribbon的熔断功能

1.1、改造service-ribbon的pom.xml 增加hystrix依赖后<dependencies>节点如下所示:

org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix

1.2 在启动类ServiceRibbonApplication增加@EnableHystrix注解开启Hystrix

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;/** * 通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean * @author xiaobu */@EnableEurekaClient@EnableDiscoveryClient@SpringBootApplication@EnableHystrixpublic class ServiceRibbonApplication {    public static void main(String[] args) {        SpringApplication.run(ServiceRibbonApplication.class, args);    }    /***     * @author xiaobu     * @date 2018/11/6 11:32     * @return org.springframework.web.client.RestTemplate     * @descprition restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。     * @version 1.0     */    @Bean    @LoadBalanced    RestTemplate restTemplate(){        return new RestTemplate();    }}

1.3在ClientService上方法上增加@HystrixCommand注解表明该方法支持熔断功能

package com.example.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 11:34 * @descrption */@Servicepublic class ClientService {    @Autowired    RestTemplate restTemplate;    /***     * @author xiaobu     * @date 2018/11/6 11:42     * @param name 名字  @HystrixCommand 这个表明加上了熔断器的功能     * @return java.lang.String     * @descprition  直接用的程序名替代了具体的url地址,     * 在ribbon中它会根据服务名来选择具体的服务实例,     * 根据服务实例在请求的时候会用具体的url替换掉服务名     * @version 1.0     */    @HystrixCommand(fallbackMethod ="error" )    public String clientService(String name){        return restTemplate.getForObject("http://eureka-client/test?name=" + name, String.class);    }    /**     * @author xiaobu     * @date 2018/11/7 11:27     * @param name 名字     * @return java.lang.String     * @descprition  error要与  @HystrixCommand(fallbackMethod ="error" )的方法名要相对应     * @version 1.0     */    public String error(String name){        return "hi "+name+",this service is  unavailable";    }}

1.4 启动两个client实例。端口分别为8002和8003 关闭端口为8003的服务。访问

 8003服务则不可用则效果如下:

 

二、增加Feign的熔断功能。

2.1 feign的熔断功能默认是关闭的,所以在配置文件中打开。

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/spring.application.name=service-feignserver.port=8005#打开feign的熔断功能feign.hystrix.enabled=true

2.2 改造FeignService

package com.example.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 14:24 * @description V1.0 定义个feign接口 @FeignClient("服务名") 来确定调哪个服务 */@FeignClient(value = "eureka-client",fallback = FeignHystrixServiceImpl.class)public interface FeignService {    /**     * @author xiaobu     * @date 2018/11/6 14:34     * @param name 名字     * @return java.lang.String     * @descprition value为test则是调用 eureka-client的test的方法     * RequestMapping(value="/test",method = RequestMethod.GET)与GetMapping(value="/test")等价     * RequestParam.value() was empty on parameter 0 第一个参数不能为空     * @version 1.0     */    //@RequestMapping(value="/test",method = RequestMethod.GET)    @GetMapping(value="/test")    String testFromClient(@RequestParam(value = "name") String name);}

2.3增加实现类:

package com.example.service;import org.springframework.stereotype.Component;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/7 11:34 * @description V1.0 */@Componentpublic class FeignHystrixServiceImpl implements FeignService {    @Override    public String testFromClient(String name) {        return "sorry "+name+",this service is unavailable";    }}

2.4、访问出现如下效果。

OK。

转载地址:http://hugai.baihongyu.com/

你可能感兴趣的文章
vue1.0与2.0区别之生命周期
查看>>
vue2.0之非父子组件通信
查看>>
如何建立svn版本库并运行它
查看>>
如何合并svn分支到主干上
查看>>
libusb源码学习:list_entry
查看>>
libusb源码学习:几个函数加载的宏(windows)
查看>>
MCU_如何通过硬件VID 查找生产厂家
查看>>
NCNN部署例程 mxnet-gluoncv之simple_pose
查看>>
Ubuntu18.04查看显卡信息并安装NVDIA显卡驱动driver + Cuda + Cudnn
查看>>
电子元件二极管封装SMA,SMB,SMC的区别
查看>>
利用FFmpeg玩转Android视频录制与压缩(二)
查看>>
eclipse下生成Java类图和时序图,生成UML图
查看>>
M文件程序设计(matlab)
查看>>
matlab基础知识
查看>>
程序员的职业素养
查看>>
一道面试题深入了解java底层
查看>>
java下载附件
查看>>
cron表达式每个月最后一天
查看>>
Oracle中Like与Instr模糊查询性能大比拼
查看>>
Spring Boot入门===Hello World
查看>>