Ribbon的配置
1.注解配置
@RibbonClient(name = "xxx",configuration = XxxRibbonConfig.class) public class XxxRibbonConfig { String listOfServers = "http://192.168.99.100:8080,http://192.168.99.101:8080"; @Bean public ServerList<Server> ribbonServerList() { List<Server> list = Lists.newArrayList(); if (!Strings.isNullOrEmpty(listOfServers)) { for (String s: listOfServers.split(",")) { list.add(new Server(s.trim())); } } return new StaticServerList<Server>(list); } }
2.启动类配置
@SpringCloudApplication @RibbonClients(value = { @RibbonClient(name = "xxx",configuration = XxxRibbonConfig.class), @RibbonClient(name = "demo",configuration = DemoRibbonConfig.class) }) public class DemoServiceApplication { public static void main(String[] args) { SpringApplication.run(DemoServiceApplication.class, args); } }
3.属性配置
service-hi: ribbon: ReadTimeout: 1000 SocketTimeout: 1000 ConnectTimeout: 1000 MaxAutoRetries: 0 MaxAutoRetriesNextServer: 1 eureka: enabled: true
4.设置resttemplate的超时时间(这个超时时间不知道是否与熔断有关)
package com.client; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RibbonConf { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ //return new RestTemplate(); SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(10000); simpleClientHttpRequestFactory.setReadTimeout(10000); return new RestTemplate(simpleClientHttpRequestFactory); } }