hystrix

 

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
	<version>1.4.7.RELEASE</version>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-netflix-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-hystrix</artifactId>
	<version>9.7.0</version>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-netflix-core</artifactId>
	<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-commons</artifactId>
	<version>1.3.6.RELEASE</version>
</dependency>


@Configuration
public class FeignConfiguration {
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return HystrixFeign.builder();
    }

    @Bean
    public MyFeignClientFallbackOne fb() {
        return new MyFeignClientFallbackOne();
    }

public class MyFeignClientFallbackOne  implements MyFeignClient {
    @Override
    public TestReponse myFeignTest(Map<String, Object> map) {
        TestReponse re = new TestReponse();
        re.setName("xxxxxx111");
        return re;
    }

@FeignClient(value="myClient",url="http://localhost:9093",fallback = MyFeignClientFallbackOne.class,configuration = FeignConfiguration.class)
public interface MyFeignClient {
    @RequestMapping(value="/myService",method = RequestMethod.GET)
    TestReponse myFeignTest(Map<String,Object> map);//


测试
    @RequestMapping(value = "/mTestHttp2")//,method = RequestMethod.GET
    @ResponseBody
    public TestReponse mTestHttp2(TestRequest request){
        Map<String,Object> params = new HashMap<>();
        params.put("name","mynme");
        params.put("age","myagess");
        TestReponse res =  client.myFeignTest(params);
        System.out.println(res);

 @RequestMapping(value = "/myService2")//,method = RequestMethod.GET
    @ResponseBody
    public TestReponse myService(@RequestBody TestRequest request){
        System.out.println(" server "+request);
        TestReponse res = new TestReponse();
        return res;
    }

feign.hystrix.enabled= true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 15000
hystrix.threadpool.default.coreSize: 1
hystrix.threadpool.default.maximumSize: 1
hystrix.threadpool.default.maxQueueSize: 1


获取熔断信息精细化处理
    @Bean
    public MyFeignClientFallback fbf() {
        return new MyFeignClientFallback();
    }


@FeignClient(value="myClient",url="http://localhost:9093", fallbackFactory=MyFeignClientFallback.class,configuration = FeignConfiguration.class)
public interface MyFeignClient {

public class MyFeignClientFallback  implements FallbackFactory<MyFeignClient> {
    @Override
    public MyFeignClient create(Throwable throwable) {
        return new MyFeignClient() {
            @Override
            public TestReponse myFeignTest(Map<String, Object> map) {
                System.out.println("熔断信息:"+throwable.toString());
                System.out.println("熔断信息:"+throwable.getMessage());
                TestReponse re = new TestReponse();
                re.setName("1111111111");
                return re;
            }
            @Override
            public TestReponse myFeignTest2(String name, String age) {
                return null;
            }


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version><!--1.3.7 2.0.0 2.0.4-->
<relativePath/>
</parent>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>


两个线程循环掉,一个线程会熔断掉

熔断信息:java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@6dd0313 rejected from java.util.concurrent.ThreadPoolExecutor@36e19049[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 1]
Thread-9thready:TestReponse{name='1111111111', age='null'}

hystrix.threadpool.default.coreSize: 2 则不会熔断掉

  

 

上一篇:SpringCloud:Hystrix(服务熔断、服务降级)


下一篇:spring整合feign和hystrix实现RPC熔断降级和监控