Gateway使用Nacos注册中心的服务名和路由规则进行调用API(测试源码)
Gateway使用Nacos注册中心的服务名和路由规则进行调用API(测试源码)
Gateway使用Nacos注册中心的服务名和路由规则进行调用API(测试源码)
问题背景
一开始使用gateway的时候,使用的是它的路由规则,后面微服务越来越多了,添加路由配置也蛮麻烦的,所有想通过注册中心使用gateway根据服务名自动匹配路由模式,结果途中遇到了,匹配不成功问题,所有一直在查找问题所在
- 默认有JDK环境
- 经过一番查找之后,发现问题就是springboot和spingcloud的版本兼容性问题,但是启动项目并不报错
- nacos-discovery依赖有两个仓库,一个是org.springframework.cloud的,一个是com.alibaba.cloud的,两个依赖和其他的依赖也有兼容性的问题
- 以下代码版本调试都以通过,可以自己创建工程粘贴复制,可以直接下载源码
- nacos安装启动可以参照另一篇文章
代码步骤
1 gateway微服务
1.1 创建gateway项目module工程,引入pom的公共依赖
<?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.sakura</groupId>
<artifactId>springcloud_gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>gateway</module>
<module>service</module>
</modules>
<packaging>pom</packaging>
<name>springcloud_gateway</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2 引入gateway自己的pom依赖
<?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_gateway</artifactId>
<groupId>com.sakura</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos:用于服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
1.3 修改application配置文件,注释掉是路由规则的模式
server:
port: 8222 #服务端口
spring:
application:
name: springcloud-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务地址
gateway:
discovery:
locator:
enabled: true #使用服务发现路由
lower-case-service-id: true
# routes:
# - id: SERVICE-ONE #设置路由id(理论上是可以随便写的)
# uri: lb://service-one #设置路由的url lb://nacos服务注册名称
# predicates:
# - Path=/one/** #路径匹配规则
1.4 gateway配置类
package com.sakura.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
1.5 gateway启动类,添加@EnableDiscoveryClient注解
package com.sakura.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GateApplication {
public static void main(String[] args) {
SpringApplication.run(GateApplication.class,args);
}
}
2 service微服务
2.1 可以包含多个微服务,这里我只创建了一个以做测试,引入pom公共依赖
<?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_gateway</artifactId>
<groupId>com.sakura</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service</artifactId>
<packaging>pom</packaging>
<modules>
<module>service_one</module>
</modules>
<dependencies>
<!--服务注册-->
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.2 引入 service-one 的 pom 依赖
<?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>service</artifactId>
<groupId>com.sakura</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service_one</artifactId>
<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
</project>
2.3 添加application配置文件
server:
port: 8001
spring:
application:
name: service-one #服务名
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务注册
2.4 service-one启动类,添加@EnableDiscoveryClient注解
package com.sakura;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
public class OneApplication {
public static void main(String[] args) {
SpringApplication.run(OneApplication.class,args);
}
}
2.5 添加controller控制类
package com.sakura.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/one")
public class OneController {
@GetMapping("getOne")
public String getOne(){
System.out.println("getOne");
return "调用端口号为8001的Api接口";
}
}
2.6 整个项目目录
测试流程
1 通过注册中心 nacos 使用自动匹配服务名模式
server:
port: 8222 #服务端口
spring:
application:
name: springcloud-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务地址
gateway:
discovery:
locator:
enabled: true #使用服务发现路由
lower-case-service-id: true
1.1 启动 nacos 服务端,启动 gateway 微服务
1.2 启动 service-one 微服务
1.3 查看 nacos 注册中心,账号密码都为:nacos
1.4 使用service-one微服务名调用API
1.5 service-one服务日志打印
2 使用路由规则调用service-one微服务
2.1 关闭gateway的注册中心
server:
port: 8222 #服务端口
spring:
application:
name: springcloud-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务地址
enabled: false
gateway:
discovery:
locator:
enabled: false #使用服务发现路由
lower-case-service-id: false
routes:
- id: SERVICE-ONE #设置路由id(理论上是可以随便写的)
uri: http://localhost:8001 #设置路由的url lb://nacos服务注册名称 lb://service-one
predicates:
- Path=/one/** #路径匹配规则
2.2 关闭service-one的注册中心
server:
port: 8001
spring:
application:
name: service-one #服务名
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务注册
enabled: false
2.3 启动gateway和service-one服务,查看注册中心,没有了
2.4 使用gateway网关路由service-one微服务
3 可以同时使用微服务名以及路由规则两种方式
3.1 gateway配置更改
server:
port: 8222 #服务端口
spring:
application:
name: springcloud-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务地址
enabled: true
gateway:
discovery:
locator:
enabled: true #使用服务发现路由
lower-case-service-id: true
routes:
- id: SERVICE-ONE #设置路由id(理论上是可以随便写的)
uri: lb://service-one #设置路由的url lb://nacos服务注册名称 lb://service-one http://localhost:8001
predicates:
- Path=/one/** #路径匹配规则
3.2 service-one配置更改
server:
port: 8001
spring:
application:
name: service-one #服务名
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务注册
enabled: true
3.3 启动gateway和service-one服务,查看注册中心
3.4 使用微服务名调用
3.5 使用路由规则调用
心得
- 版本兼容导致微服务名调用方式不能使用,但是运行程序没有报错,找得甚是辛苦
- 基本路由方式就是这样,还有一些高级用法,等下次解说
作为程序员第 17 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Lyric: 我牵着你的手经过
更多推荐
所有评论(0)