springboot-cloud-4-feign

feign

application.properties

spring.application.name = feign-master
server.port=8003
eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka

application

@SpringBootApplication
@EnableDiscoveryClient  // 開啓eureka消費端
@EnableFeignClients
public class FeignMasterApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignMasterApplication.class, args);
    }
}

service

@FeignClient("client")   //feign 註解調用client服務
public interface FeignService {

    @RequestMapping("/clientService1")
    String clientService1();

    @RequestMapping("/clientService2")
    String clientService2(@RequestParam("key")String key);

    @RequestMapping("/clientService3")
    String clientService3(@RequestBody TestQuery query);
}

controller

@RestController
public class FeignController {

    @Autowired
    private FeignService feignService ;

    @RequestMapping("/feignService1")
    public String feignService1() {
       return feignService.clientService1();
    }

    @RequestMapping("/feignService2")
    public String feignService2() {
       return feignService.clientService2("hello");
    }
    @RequestMapping("/feignService3")
    public String feignService3() {
        TestQuery testQuery = new TestQuery();
        testQuery.setKey("hello");
       return feignService.clientService3(testQuery);
    }
}

query

/**
 * Created by guoyao on 2017/8/14.
 */
public class TestQuery {

    @Getter @Setter
    private  String key ;
}

ribbon參數

###hystrix配置(需要大於ribbon超時時間)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds= 5000

#####ribbon (全局)配置 ribbon.key = value
#####開啓重試;
spring.cloud.loadbalancer.retry.enable = true
#####請求連接的超時時間
ribbon.ConnectTimeout = 1500
#####請求處理的超時時間
ribbon.ReadTimeout =1500
#####對所有操作請求都重試
ribbon.OkToRetryOnAllOperations = true
####切換實例的重試次數
ribbon.MaxAutoRetriesNextServer = 2
####對當前實例的重試次數
ribbon.MaxAutoRetries = 3
####(服務子配置)<client>.ribbon.key = value
client改造接口
    @GetMapping("/clientService1")
    public String clientService1() {
        try {
            log.info("clientService1 sleeptime = " + 1000);
            Thread.sleep(1000);
        } catch (Exception e) {
            log.error(" thread sleep error",e);
        }
        log.info("  this is eureka client helloService ");
        return " hello ,i am eureka helloService";
    }

    @RequestMapping("/clientService2")
    public String clientService2(@RequestParam("key")String key){
        try {
            int sleepTime=new Random().nextInt(100) + 1450;
            log.info("clientService2 sleeptime = " + sleepTime);
            Thread.sleep(sleepTime);   // ribbon  定義超時時間爲1500 測試策略
        } catch (Exception e) {
            log.error(" thread sleep error",e);
        }
        return  key;
    }


    @RequestMapping("/clientService3")
    public String clientService3(@RequestBody TestQuery query){
        try {
            log.info(" clientService3 sleepTime = " + 3000);
            Thread.sleep(3000);
        } catch (Exception e) {
            log.error(" thread sleep error",e);
        }
        return  "query  : " + query.getKey() ;
    }
輸出
// ribbon參數設置ok
2017-08-14 19:05:00.669  INFO 13388 --- [nio-8888-exec-3] test.ygy.controller.HelloController      : clientService1 sleeptime = 1000
2017-08-14 19:05:01.669  INFO 13388 --- [nio-8888-exec-3] test.ygy.controller.HelloController      :   this is eureka client helloService 
2017-08-14 19:05:06.463  INFO 13388 --- [nio-8888-exec-4] test.ygy.controller.HelloController      : clientService2 sleeptime = 1464
2017-08-14 19:05:13.079  INFO 13388 --- [nio-8888-exec-6] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:14.572  INFO 13388 --- [nio-8888-exec-7] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:16.074  INFO 13388 --- [nio-8888-exec-8] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:17.587  INFO 13388 --- [nio-8888-exec-9] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:19.081  INFO 13388 --- [io-8888-exec-10] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:20.583  INFO 13388 --- [nio-8888-exec-1] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:22.085  INFO 13388 --- [nio-8888-exec-2] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:23.590  INFO 13388 --- [nio-8888-exec-3] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:25.095  INFO 13388 --- [nio-8888-exec-4] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:26.599  INFO 13388 --- [nio-8888-exec-5] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:28.102  INFO 13388 --- [nio-8888-exec-6] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:29.606  INFO 13388 --- [nio-8888-exec-7] test.ygy.controller.HelloController      :  clientService3 sleepTime = 3000
2017-08-14 19:05:56.868  INFO 13388 --- [nio-8888-exec-8] test.ygy.controller.HelloController      : clientService2 sleeptime = 1547
2017-08-14 19:05:58.369  INFO 13388 --- [nio-8888-exec-9] test.ygy.controller.HelloController      : clientService2 sleeptime = 1528
2017-08-14 19:05:59.877  INFO 13388 --- [io-8888-exec-10] test.ygy.controller.HelloController      : clientService2 sleeptime = 1458

配置關閉hystrix

config
@Configuration
public class DisableHystrixConfig {

    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();

    }
}
application.properties
####hystrix  配置
### ## 全局配置  熔斷   超時時間
#####hystrix.command.default.execution.isolation.thread.timeoutinMilliseconds= 1600
hystrix.command.clientService1.execution.isolation.thread.timeoutinMilliseconds= 1600
hystrix.command.clientService2.execution.isolation.thread.timeoutinMilliseconds= 1525
hystrix.command.clientService3.execution.isolation.thread.timeoutinMilliseconds= 1600
service
@FeignClient(name="client", configuration = DisableHystrixConfig.class,fallback =FeignServiceImpl.class)
impl
@Component
public class FeignServiceImpl implements FeignService {

    @Override
    public String clientService1(){
        return  "error clientService1" ;
    }

    @Override
    public String clientService2(@RequestParam("key")String key){
        return " error clientService2 ";
    }

    @Override
    public String clientService3(@RequestBody TestQuery query){
        return " error clientService3 ";
    }
}
輸出
Load balancer does not have available server for client: client

開啓hystrix

service
@FeignClient(name="client",fallback =FeignServiceImpl.class)   //feign 註解調用client服務
輸出
error clientService3
發佈了72 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章