springcloud整合ribbon实现服务负载均衡
1.项目目录:
创建项目ribbon作为父类
2.代码实现:
父类依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>8</java.version>
<spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>
<spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建module项目ribbon-client
添加依赖
<parent>
<groupId>com.cxh</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>ribbon-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
yml配置
server:
port: 8001
spring:
application:
name: ribbon-client #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册
控制器
@Slf4j
@RestController
public class ClientController {
@Value("${server.port}")
private String port;
@GetMapping("/add")
public String add(Integer a, Integer b){
Integer c = a + b;
log.info("端口计算:" + port + ":" + c);
return "端口计算:" + port + ":" + c;
}
}
启动类添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}
创建module项目ribbon-client2, 这个跟ribbon-client是一样的,只是yml配置改了个端口,其他代码一样,用于测试负载均衡
server:
port: 8002
spring:
application:
name: ribbon-client #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册
创建module项目ribbon-consumer
添加依赖
<parent>
<groupId>com.cxh</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
yml配置
server:
port: 8003
spring:
application:
name: ribbon-consumer #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册
控制器
@Slf4j
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/add")
public String add() {
String result = restTemplate.getForEntity("http://ribbon-client/add?a=10&b=20", String.class).getBody();
log.info("返回结果:" + result);
return result;
}
}
启动类添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
3.实现效果:
启动nacos后,再启动ribbon-client, ribbon-client2, ribbon-consumer项目,打开浏览器http://localhost:8003/add
第一次请求:
端口计算:8001:30
第二次请求:
端口计算:8002:30