在上一篇文章 SpringCloud(四) ribbon负载均衡 中用的默认的负载均衡,这一篇文章讲解怎么配置负载均衡,怎么为某个微服务指定负载均衡。
1. 通过代码配置ribbon client
写一个负载均衡规则的配置类。这个配置类如果在spring的管理下,就会变成全局的配置类,所以我把它放在了ComponentScan扫描范围外
package com.xhx.config;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xuhaixing
* 2018/9/3 20:09
*
* 不放在ComponentScan下,否则会变成全局的,如果对单个微服务进行控制,就放在扫描的外面
*
**/
@Configuration
public class FooConfiguration {
@Bean
public IRule ribbonRule(IClientConfig config){
return new RandomRule();
}
}
写了另一个配置类,指定哪个服务应用上面设置的规则。这个要在spring管理下
package com.xhx.springcloud.config;
import com.xhx.config.FooConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Configuration;
/**
* xuhaixing
* 2018/9/3 19:53
**/
@Configuration
@RibbonClient(name = "eureka-service2",configuration = FooConfiguration.class)
public class TestConfiguration {
}
注入RestTemplate
package com.xhx.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* xuhaixing
* 2018/6/3 16:24
*/
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
两个测试的请求,一个请求service1,默认的策略,,另一个请求service2,随机的策略
package com.xhx.springcloud.controller;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* xuhaixing
* 2018/6/3 16:27
*/
@RestController
public class RibbionController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "getName")
public String getName(String name){
return restTemplate.getForObject("http://eureka-service/hello/getName?name="+name,String.class);
}
@RequestMapping(value = "getName2")
public String getName2(String name){
return restTemplate.getForObject("http://eureka-service2/hello/getName?name="+name,String.class);
}
}
起了如下工程,两个service是服务的,一个client是测试用的客户端
这样就可以了
2. 通过配置文件配置
Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients by setting properties to be compatible with the Ribbon documentation.
This lets you change behavior at start up time in different environments.
The following list shows the supported properties>:
-
<clientName>.ribbon.NFLoadBalancerClassName
: Should implementILoadBalancer
-
<clientName>.ribbon.NFLoadBalancerRuleClassName
: Should implementIRule
-
<clientName>.ribbon.NFLoadBalancerPingClassName
: Should implementIPing
-
<clientName>.ribbon.NIWSServerListClassName
: Should implementServerList
-
<clientName>.ribbon.NIWSServerListFilterClassName
: Should implementServerListFilter
#通过配置文件方式配置自定义ribbon client
eureka-service:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
请求eureka-service这个微服务时,就会使用随机策略了
实时内容请关注微信公众号,公众号与博客同时更新:程序员星星