配置
方法一:配置文件配置
eureka 客户端(服务发现方) bootstrap.properties 配置
这里,服务注册方的服务名是 micro-order
#搭配eureka服务端使用,从eureka服务端获得服务地址
ribbon.eureka.enabled=true
#不搭配eureka服务端使用,指定调用的节点
#micro-order.ribbon.listOfServers=localhost:8001
spring.cloud.loadbalancer.retry.enabled=true
#单位ms ,请求连接超时时间
#micro-order.ribbon.ConnectTimeout=1000
#单位ms ,请求处理的超时时间
#micro-order.ribbon.ReadTimeout=2000
#micro-order.ribbon.OkToRetryOnAllOperations=true
#切换实例的重试次数
#micro-order.ribbon.MaxAutoRetriesNextServer=2
#对当前实例的重试次数 当Eureka中可以找到服务,但是服务连不上时将会重试
#micro-order.ribbon.MaxAutoRetries=2
#micro-order.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
#一般不配置,配置了每次调用都会先ping
#micro-order.ribbon.NFLoadBalancerPingClassName=com.netflix.loadbalancer.PingUrl
更多配置,看 RibbonClientConfiguration 源码
方法二:配置类配置
这个类放在能被 @ComponentScan 扫描到的位置
@Configuration
@RibbonClients(value = {
@RibbonClient(name = "micro-order",configuration = RibbonLoadBalanceMicroOrderConfig.class)
})
public class LoadBalanceConfig {
}
这个类不要放在能被 @ComponentScan 扫描到的位置,不然就成了公用配置
@Configuration
public class RibbonLoadBalanceMicroOrderConfig {
private String name = "micro-order";
@Bean
@ConditionalOnClass
public IClientConfig defaultClientConfigImpl() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadProperties(name);
config.set(CommonClientConfigKey.MaxAutoRetries,2);
config.set(CommonClientConfigKey.MaxAutoRetriesNextServer,2);
config.set(CommonClientConfigKey.ConnectTimeout,2000);
config.set(CommonClientConfigKey.ReadTimeout,4000);
config.set(CommonClientConfigKey.OkToRetryOnAllOperations,true);
return config;
}
/*
* 判断服务是否存活,不配置,增加访问时间
* */
//@Bean
public IPing iPing() {
//这个实现类会去调用服务来判断服务是否存活
PingUrl pingUrl = new PingUrl();
pingUrl.setPingAppendString("/user/queryContent");
return pingUrl;
}
@Bean
public IRule ribbonRule() {
//线性轮询
//return new RoundRobinRule();
//可以重试的轮询
//return new RetryRule();
//根据运行情况来计算权重
//return new WeightedResponseTimeRule();
//过滤掉故障实例,选择请求数最小的实例
//return new BestAvailableRule();
return new RandomRule();
}
}
启动类配置
无论是方法一还是方法二,都需要在 SpringBoot 启动类里,添加这么个东西:
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
使用
示例:
@Service
public class UserServiceImpl implements UserService {
public static String SERVIER_NAME = "micro-order";
@Autowired
private RestTemplate restTemplate;
@Override
public String queryContents() {
//get 这个会选择一个服务列表里的服务调用
String results = restTemplate.getForObject("http://"
+ SERVIER_NAME + "/queryUser", String.class);
return results;
}
}
Get请求
getForEntity:
getForEntity(String url, Class<T> responseType)
getForEntity(String url, Class<T> responseType, Object... uriVariables)
getForEntity( String url, Class<T> responseType, Map<String, ? uriVariables)
getForEntity(URI url, Class<T> responseType)
注意:此方法返回的是一个包装对象 ResponseEntity<T>其中T为 responseType传入类型,想拿到返回类型需要使用这个包装类对象的 getBody()方法
getForobject:
getForObject(String url, Class<T> responseType)
getForobject(String url, Class<T> responseType, Object... uriVariables)
getForObject(String url, Class<T> responseType, Map<String,?> uriVariables )
getForobject(URI url, Class<T> responseType)
注意:此方法返回的对象类型为 responsetype传入类型
Post请求
postForEntity:
postForEntity(String url,Object request, Class<T> responseType, Object... uriVariables)
postForEntity(String url, Object request, Class<T> responseType, Map<String,?> uriVariables )
postForEntity(URI url, Object request, Class<T> responseType)
注意:此方法返回的是一个包装对象 ResponseEntity<T>其中T为 responseType传入类型,想拿到返回类型需要使用这个包装类对象的 getBody()方法
postForobject:
postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
postForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables )
postForObject(URI url, Object request, Class<T> responseType)
注意:此方法返回的对象类型为 responseType传入类型
postForLocation:
postForLocation(String url, Object request, Object... uriVariables)
postForLocation(String url, Object request, Map<String,?> uriVariables )
postForLocation(URI url, Object request)
注意:此方法返回的是新资源的URI,相比 getForEntity、 getForobject、 postForEntity、 postForobject方法不同的是这个方法中无需指定返回类型,因为返回类型就是URl。