往期相关推荐:
网关路由规则和nacos配置中心实战:
准备引入相关依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud-alibaba.version>0.2.1.RELEASE</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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
上面这些依赖是gateway服务和member服务实践共同需要的,此外需要注意SpringBoot的版本,小编实践过程中,gateway集成nacos依赖启动失败,原因:springBoot引用的版本是2.5.5,需要降级为2.3.9.RELEASE,所以如果小伙伴整合nacos时,同样遇到问题,思考一下是否版本问题。
<!-- gateway依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
server:
port: 8082
spring:
application:
name: gateway-demo
cloud:
nacos:
config:
file-extension: yaml
server-addr: XX.XX.XX.XX:8848
profiles:
active: dev
注意:nacos服务地址替换成自己服务器IP地址;
{spring.cloud.nacos.config.prefix}- {spring.profiles.active}.
spring:
application:
name: gateway-demo # 服务名称
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: xx.xx.xx.xx:8848 # 注册中心地址 # 默认是没有密码的
config:
server-addr: xx.xx.xx.xx:8848 # config地址
file-extension: yaml # 指定配置文件类型
# 网关的配置
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: member-server
uri: lb://member-server
predicates:
- Path=/query-demo/**
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayDemoApplication.class, args);
}
}
@EnableDiscoveryClient
注解标识服务发现;
member服务按照准备部分引入相关依赖后,则直接添加配置文件;
server:
port: 8083
spring:
application:
name: member-server
cloud:
nacos:
config:
file-extension: yaml
server-addr: xx.xx.xx.xx:8848
@RestController
public class MemberController {
@RequestMapping("/query-demo")
public String queryDemo(String name) {
return "https://blog.csdn.net/"+ name + "/article/details/120475609";
}
}
主启动类上添加@EnableDiscoveryClient
注解标识服务发现;
member-server.yaml
,如下spring:
application:
name: member-server # 服务名称
cloud:
nacos:
discovery:
server-addr: xx.xx.xx.xx:8848 # 注册中心地址 # 默认是没有密码的
config:
server-addr: xx.xx.xx.xx:8848 # config地址
file-extension: yaml # 指定配置文件类型
分别将网关服务8082,member服务8083端口启动,通过浏览器访问gateway将请求转发到member服务;
nacos服务列表中已经注册以上两个服务:
请求:http://localhost:8082/query-demo?name=xuanlu1
响应结果:https://blog.csdn.net/xuanlu1/article/details/120475609