Copyright: without the author's permission is not allowed for any commercial purposes https://blog.csdn.net/weixin_38231448/article/details/91039864
作者:jiangzz
电话:15652034180
微信:jiangzz_wx
微信公众账号:jiangzz_wy
Load balancing: Spring Cloud Ribbon
Spring Cloud Ribbon is a load-balancing tool Http and TCP-based customer service side, it is the Netflix Ribbon-based implementation. By automatically configured such that items can be automatically SpringCloud to RestTemplate add interceptors, similar to achieve a load balancing effect.
Getting Started
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiangzz</groupId>
<artifactId>cloud02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- application.properties
USER-SERVICE.ribbon.listOfServers=localhost:8080,localhost:9090
USER-SERVICE.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
- SpringRibbonApplication
@SpringBootApplication
public class SpringRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRibbonApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- User
public class User implements Serializable {
private Integer id;
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthDay;
private Boolean sex;
private Double salary;
}
- Rest Template Tests
@SpringBootTest(classes = SpringRibbonApplication.class)
@RunWith(SpringRunner.class)
public class RestTemplateTests {
@Autowired
private RestTemplate restTemplate;
@Test
public void testQueryUserById(){
String url="http://USER-SERVICE/manager/user/1";
User user = restTemplate.getForObject(url, User.class);
System.out.println(user);
}
@Test
public void testQueryUserByPage(){
String url="http://USER-SERVICE/manager/user/1/10";
List<User> users = restTemplate.getForObject(url, List.class);
for (int i = 0; i < users.size(); i++) {
System.out.println(users.get(i));
}
}
@Test
public void testSaveUser(){
String url="http://USER-SERVICE/manager/user";
User user = restTemplate.postForObject(url, new User("李小四", new Date(), true, 15000.0), User.class);
System.out.println(user);
}
@Test
public void testUpdateUser(){
String url="http://USER-SERVICE/manager/user";
User user = new User("李小四", new Date(), true, 18000.0);
user.setId(3);
restTemplate.put(url,user);
}
@Test
public void testDeleteUser(){
String url="http://USER-SERVICE/manager/user/1,2,3";
restTemplate.delete(url);
}
}
In addition to this from the outside, SpringCloud provides a configuration file-based configuration
- SpringRibbonApplication
@SpringBootApplication
@RibbonClient(name = "USER-SERVICE",configuration = {UserSerivceRibbonConfigure.class})
public class SpringRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRibbonApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- UserSerivceRibbonConfigure
@Configuration
public class UserSerivceRibbonConfigure {
@Bean
public ServerList<Server> ribbonServerList(){
Server server1 = new Server("localhost", 8080);
server1.setZone("beijing");
Server server2 = new Server("localhost", 9090);
server2.setZone("shanghai");
return new StaticServerList<Server>(server1,server2);
}
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
@Bean
public ZonePreferenceServerListFilter ribbonServerListFilter(){
ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
filter.setZone("beijing");
return filter;
}
}
Users can query the list of services directly LoadBalancerClient
@Autowired
private LoadBalancerClient loadBalancer;
@Test
public void testChoose(){
for (Integer i=0;i<10;i++){
ServiceInstance instance = loadBalancer.choose("stores");
URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
System.out.println(storesUri);
}
}