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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值