Ribbon支持多种客户端负载均衡算法,如轮询,最小响应时间,随机等等.
这里简单使用一下随机的Ribbon客户端负载均衡算法
使用上一章的项目,在spring-cloud-consumer-01项目中添加一个配置类,在里面构建一个随机负载均衡的bean
配置类的方式配置父子均衡算法
spring-cloud-consumer-01
消费端添加了对随机负载均衡的支持
LoadBarentConfig.java
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
@Configuration
public class LoadBarentConfig {
/**
* 随机规则
* @param config
* @return
*/
@Bean
public IRule iRuleBean(){
return new RandomRule();
}
}
特别注意
LoadBarentConfig不能被spring扫描器扫描到
所以LoadBarentConfig.java不能和SpringCloudConsumer01Application.java文件在同一级别或者下一级子目录中
SpringCloudConsumer01Application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import com.example.config.LoadBarentConfig;
@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name="spring-cloud-provider-test",configuration={LoadBarentConfig.class})
public class SpringCloudConsumer01Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConsumer01Application.class, args);
}
}
UserConstroller.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserConstroller {
@Autowired
private RestTemplate restTemplate;
@ResponseBody
@GetMapping("/get")
public User get(String uid){
return restTemplate.getForObject("http://spring-cloud-provider/user/get?id="+uid, User.class);
}
@ResponseBody
@GetMapping("/random_get")
public User randomGet(String uid){
return restTemplate.getForObject("http://spring-cloud-provider-test/user/get?id="+uid, User.class);
}
}
spring-cloud-provider
这个服务包含spring-cloud-provider-01和spring-cloud-provider-02项目,这一组微服务提供的接口和前一节相同
spring-cloud-provider-test
这个服务包含spring-cloud-provider-03和spring-cloud-provider-04项目
spring-cloud-provider-03
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming3", 30));
umap.put("2", new User("2", "xiaohong3", 20));
umap.put("3", new User("3", "zhangsan3", 80));
umap.put("4", new User("4", "lisi3", 10));
umap.put("5", new User("5", "wangwu3", 22));
}
@GetMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
spring-cloud-provider-04
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming4", 30));
umap.put("2", new User("2", "xiaohong4", 20));
umap.put("3", new User("3", "zhangsan4", 80));
umap.put("4", new User("4", "lisi4", 10));
umap.put("5", new User("5", "wangwu4", 22));
}
@GetMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
验证
浏览器多次刷新http://192.168.88.101:9000/user/random_get?uid=1
内容随机显示
{"id":"1","userName":"xiaoming4","age":30,"birthday":null}
{"id":"1","userName":"xiaoming3","age":30,"birthday":null}
浏览器多次刷新http://192.168.88.101:9000/user/get?uid=1
内容轮询显示
{"id":"1","userName":"xiaoming","age":30,"birthday":null}
{"id":"1","userName":"xiaoming1","age":30,"birthday":null}
配置文件配置客户端负载均衡
推荐使用这种方式配置负载均衡
application.yaml
spring-cloud-provider-test:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule