Spring Cloud Feign整合Hystrix后断路器使用

一 会员模块

1 控制器中新增方法

package org.crazyit.cloud;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Member call(@PathVariable Integer id) {
        Member p = new Member();
        p.setId(id);
        p.setName("angus");
        return p;
    }
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }
    //该方法用于测试断路去,该方法会执行1秒
    @RequestMapping(value = "/toHello", method = RequestMethod.GET)
    public String toHello() throws Exception {
        Thread.sleep(1000);
        return "timeout hello";
    }
}

二 销售模块

1 配置application.properties

server:
  port: 8081
spring:
  application:
    name: spring-hy-sale
feign:
  hystrix:
    enabled: true
hystrix:
  command:
    HelloClient#toHello():   #针对toHello方法生效
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 500  #配置500ms超时
      circuitBreaker:
        requestVolumeThreshold: 3   #断路器10s请求阈值改为3次
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2 新建接口类

package org.crazyit.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "spring-hy-member", fallback = HelloClientFallback.class)
public interface HelloClient {

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    public String hello();
    
    //该方法用于测试断路器
    @RequestMapping(method = RequestMethod.GET, value = "/toHello")
    public String toHello();
}

3 新建回退类

package org.crazyit.cloud.feign;
importorg.springframework.stereotype.Component;
@Component
public class HelloClientFallback implements HelloClient {
      public String hello() {
            return "fallback hello";
      }

      //该方法用于测试断路器
      public String toHello() {
            return "fallback timeout hello";
      }
}

4 新建控制器

package org.crazyit.cloud.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommandKey;

@RestController
public class FeignController {
    
    @Autowired
    private HelloClient helloClient;

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    public String hello() {
        return helloClient.hello();
    }
    
    //该方法用于测试断路器
    @RequestMapping(method = RequestMethod.GET, value = "/toHello")
    public String toHello() {
        String result = helloClient.toHello();
        HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
                .getInstance(HystrixCommandKey.Factory
                        .asKey("HelloClient#toHello()"));    
        System.out.println("断路器状态:" + breaker.isOpen());
        return result;
    }
}

5 新建测试类

package org.crazyit.cloud;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TestMain {

    public static void main(String[] args) throws Exception {
        final CloseableHttpClient httpclient = HttpClients.createDefault();
        final String url = "http://localhost:8081/toHello";
        
        for(int i = 0; i < 6; i++) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        HttpGet httpget = new HttpGet(url);
                        HttpResponse response = httpclient.execute(httpget);
                        System.out.println("#########");
                        System.out.println(EntityUtils.toString(response.getEntity()));
                    } catch (Exception e ) {
                        e.printStackTrace();
                    }
                }                
            };
            t.start();
        }
        Thread.sleep(15000);
    }

}

三 测试

1 启动会员模块

2 启动销售模块

3 运行测试类

控制台输出

#########

fallback timeout hello

#########

fallback timeout hello

#########

fallback timeout hello

#########

fallback timeout hello

#########

fallback timeout hello

#########

fallback timeout hello

断路器状态:false

断路器状态:false

断路器状态:false

断路器状态:false

断路器状态:true

断路器状态:true

你可能感兴趣的:(微服务,断路器)