SpringCloud LoadBalancerClient implements load balancing and custom load strategies (one or more)
The previous article has introduced the principle of LoadBalancerClient load strategy realization LoadBalancerClient load strategy realization principle , and the introduction of the overall load process realization. This chapter mainly implements how to use LoadBalancerClient to achieve load balancing.
1. Preparation
a. 商品服务 作为被消费者服务(也就是被调用的服务),启动3个节点服务端口号为9091,9092,9093,用作负载均衡测试。
b. 订单服务 作为消费者服务(也就是调用者服务),启动一个服务,实现LoadBalanerClient负载均衡。
Take a look at the simple code of the commodity service :
mainly to print out the service port number to facilitate the test of the load function.
Order service
Now we implement the order service code, implement load through LoadBalancerClient
1. Specify the service, and automatically obtain an instance of the service through LoadBalancerClient.
@Autowired
LoadBalancerClient loadBalancedClient // 注入LoadBalancerClient
ServiceInstance instance = loadBalancedClient.choose("服务id"); // 获取服务中一个实例
- Get instance information, get the specified request address
String ippath = instance.getHost(); // 获取实例的ip
int port = instance.getPort(); // 获取实例的端口号
String url = String.format("http://$s:$s/product/getproductinfo",ippath,port"); // 格式化得到完整的请求地址
- Request remote service address through RestTemplate
RestTemplate restenplate = new RestTemplate(); // 初始化RestTemplate 类
Map<String,Object> datamap =new HasHMap<>();
datamap = restenplate .getForObject(url,Map.class); // 访问远程地址,并接受返回值
The completed code is as follows: The
above is our test set of code:
test
- Access link http://order service ip: order service port number/order/getproductinfo_loadbalanceclient (multiple visits to observe the print results)
2. Analyze the print results.
I have visited several times. The print results are as shown in the figure above. The access policy is (9092->9093->9091) polling. According to what we said in the previous article, LoadBalancerClient implements load, and the default load strategy is polling.
Custom load balancing strategy
Method 1: Properties use configuration form (here is the springboot project)
#自定义配置负载均衡策略
productserver.ribbon.NFLoadBalancerRuleClassName= com.netflix.loadbalancer.RandomRule
Productserver is the service address that needs load, and com.netflix.loadbalancer.RandomRule is the service strategy. Here we configure it as a random strategy
Check the access result again: (random access)
Method two config configuration file configuration
- Startup class plus specified load balancing strategy
@RibbonClient(name = "productserver", configuration = RiddonRuleConfig_Productserver1.class)
// name指定服务地址,configuration 指定扫描的配置类地址
- Custom configuration file class RiddonRuleConfig_Productserver
result: (polling strategy)
Customize multiple service load strategies
If there are requirements in the business, such as two commodity services (product1, product2), the order service needs to follow different load strategies when accessing these two services. How can this be achieved?
In fact, the implementation is the same as the above, but it needs to be changed a little.
Mode one properties use configuration form.
#自定义配置负载均衡策略
productserver.ribbon.NFLoadBalancerRuleClassName= com.netflix.loadbalancer.RandomRule
product2server.ribbon.NFLoadBalancerRuleClassName= com.netflix.loadbalancer.RoundRobinRule
Multiple service strategies can be configured here, productserver is the service address of product1, and product2server is the service address of product2, followed by different strategies.
Method two config configuration file configuration
Different from the above single service configuration, the annotations on the first startup class are different. Here @RibbonClients is used, the second adds @ComponentScan to shield the automatic scanning annotations, and the third strategy configuration class needs to add @ExcludeFromComponentScan (custom annotations).
The above two new things are configured to prevent multiple services from sharing @RibbonClients configuration, and the details are not clear. . . .
- @RibbonClients content
@RibbonClients(value = {
@RibbonClient(name = "productserver",configuration = RiddonRuleConfig_Productserver1.class),
@RibbonClient(name = "product2server",configuration = RiddonRuleConfig_Productserver2.class)})
2.@ComponentScan content
@ComponentScan(excludeFilters= {@ComponentScan.Filter(type= FilterType.ANNOTATION,value=ExcludeFromComponentScan.class)})
3. Strategy configuration class
4. Custom @ExcludeFromComponentScan annotation
The above is the entire content of this article.