springcloud practice-ribbon to achieve load balancing
This time the code is based on the following two
SpringCloud service split practice
springcloud-Eureka cluster actual combat
1. Modify the database and corresponding entity classes
Add the following code to the entity class of the springcloud-api module
private String db;
2. Modify the springcloud-consumer-user-80 module
1. Modify pom.xml
The complete code is as follows, mainly adding ribbon and eureka dependencies
<?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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-user-80</artifactId>
<dependencies>
<!-- ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- 我们需要拿到实体类 ,所以配置api module -->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
2. Modify the configuration
The complete code is as follows, mainly adding Eureka configuration
server:
port: 80
spring:
application:
name: springcloud-consumer-user
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
username: root
password: 520China
# Eureka配置
eureka:
client:
register-with-eureka: false # 不向Eureka注册自己
service-url:
defaultZone: http://ybg.eureka1.com:7001/eureka/,http://ybg.eureka2.com:7002/eureka/ # 向哪个地方获取Eureka服务
3. Modify the configuration class
Modify the ConfigBean configuration class and add the annotation @LoadBalanced that ribbon supports load balancing
package com.hzxy.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;
@Configuration
public class ConfigBean {
// @Configuration -- spring applicationContext.xml
// 配置负载均衡实现RestTemplate
@Bean
@LoadBalanced //ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4. Modify the controller
Change the previous ip+port to the current service name
package com.hzxy.springcloud.controller;
import com.hzxy.springcloud.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class UserController {
@Resource
private RestTemplate restTemplate; //提供多种便捷访问远程http服务方法,简单的Restful服务模板
// private static final String REST_URL = "http://localhost:8001/hello";
//ribbon 我们这里的地址,应该是一个变量,通过服务名来访问
private static final String REST_URL = "http://springcloud-provider-user/hello";
@RequestMapping("/hi")
public List<User> Hi(){
return restTemplate.getForObject(REST_URL,List.class);
}
}
5. Modify the startup class
Add an Eureka annotation, @EnableEurekaClient turns it into an Eureka client
package com.hzxy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
// ribbon 和 Eureka 整合以后,客户端可以直接调用,不用关心ip地址和端口号
@SpringBootApplication
@EnableEurekaClient //自动配置
public class Consumer_80 {
public static void main(String[] args) {
SpringApplication.run(Consumer_80.class,args);
}
}
3. Copy two more springcloud-provider-user-8001 modules and change the ports to 8002, 8003
1. Create two databases separately, the same as the original database, but the db field is different
As shown
2. New two module ports are changed to 8002, 8003
As shown
3. Copy all the code of 8001 and modify the configuration
Modify the following three places, indicating that the port and database are
ready to be copied.
4. Test
As shown in the figure, we start two registration centers, three service providers and one consumer
visit http://ybg.eureka1.com:7001/, http://ybg.eureka1.com:7002/, you can see, The three services are registered in the two registration centers.
Visit http://ybg.eureka1.com/hi for multiple times. You can see that the db field is different, which means that you are visiting different service providers, and the load balancing
test is successful!