💡Nacas可以用于实现Spring Cloud Gateway中网关动态路由功能,也可以基于Nacos来实现对后端服务的负载均衡,前者利用Nacos配置中心功能,后者利用Nacos服务注册功能。
接下来我们来看下Gateway集成Nacos实现负载均衡的架构图
一. 环境准备
1. 版本环境
Jdk: <java.version>1.8</java.version>
SpringBoot: <version>2.4.2</version>
SpringCloud: <spring.cloud.version>2020.0.1</spring.cloud.version>
SpringCloudAlibaba: <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
2. 服务环境
spring-cloud-user-center : 独立用户服务(2个)
spring-cloud-gateway: 独立的网关服务
二. 实战
创建用户服务
为了演示请求负载,将spring-cloud-user-center部署两份分别开放8005和8006端口
引入相关依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
yml配置
server:
port: 8085
spring:
application:
name: spring-cloud-user-center
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
ip: 127.0.0.1
management:
endpoints:
web:
exposure:
include: "*"
编写测试类
测试类1
/**
* @author gf
* @date 2023/2/14
*/
@Slf4j
@RestController
public class UserController {
@RequestMapping("/user")
public String user(){
log.info("hello Mr gateway,this is user server");
return "hello Mr gateway,this is user server";
}
}
重复以上1,2步骤重新创建一个服务,记得将配置文件中的端口号区分一下,然后
spring.application.name不变,依旧是spring-cloud-user-center。
测试类2
我们这里为了演示方便只是将之前的订单服务改造了一下,可以参考以下链接
SpringBoot整合Gateway+Nacos
@Slf4j
@RestController
public class OrderController {
@RequestMapping("/user")
public String getOrder(){
log.info("hello Mr gateway,this is order server");
return "hello Mr gateway,this is order server";
}
}
启动类
由于我们要将服务注册倒注册中心,所以我们在以内nacos依赖后要在启动类上添加@EnableDiscoveryClient注解
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudUserCenterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudUserCenterApplication.class, args);
}
}
2.创建网关服务
引入相关依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.scg</groupId>
<artifactId>spring-cloud-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2020.0.1</spring.cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.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.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--得不加上这个配置,不然会报503-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置yml文件
server:
port: 8082
spring:
application:
name: gateway_server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
#开启从注册中心动态创建路由的功能,利用微服务名进行路由
enabled: true
#开启小写验证,默认feign根据服务名查找都是用的全大写
lowerCaseServiceId: true
routes:
- id: spring-cloud-user-center
uri: lb://spring-cloud-user-center
# 断言,路径相匹配的进行路由
predicates:
- Path=/user-center/**
filters:
- StripPrefix=1
management:
endpoints:
web:
exposure:
include: "*"
测试
nacos可视化界面请参考:SpringCloud Alibaba 之 Nacos注册中心实战
同时启动三台服务,我们可以看到这三者服役已经注册到了nacos注册中心中
按照我们的配置,访问http://localhost:8082/user-center/user,查看接口访问情况文章来源:https://www.toymoban.com/news/detail-494772.html
可以看到,调用了两次,两次请求的服务不同文章来源地址https://www.toymoban.com/news/detail-494772.html
到了这里,关于Spring Cloud Gateway集成Nacos实现负载均衡的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!