Spring Cloud Gateway + Nacos 实现路由转发
Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关,Spring CloudGateway旨在为微服务架构提供简单、有效和统一的API路由管理方式,Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix Zuul,其不仅提供统一的路由
Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关,Spring CloudGateway旨在为微服务架构提供简单、有效和统一的API路由管理方式,Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix Zuul,其不仅提供统一的路由方式,并且还基于Filer链的方式提供了网关基本的功能,例如:安全、监控/埋点、限流等。
步骤:
- 创建父工程
springcloud_gateway
,并添加上相关的依赖
<?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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.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>
- 在父工程下面创建一个
service
模块,并添加添加上相关依赖
<?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>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service</artifactId>
<packaging>pom</packaging>
<modules>
<module>service_one</module>
<module>service_two</module>
</modules>
<dependencies>
<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 同时在service模块中添加
service-one
和service-two
两个子模块,两个子模块的端口分别属于8001和8002,在该两个模块中添加相关配置
1)分别在两个子模块中的resource目录中配置相关application.yml
的信息
server:
port: 8001
spring:
application:
name: service-one #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册
server:
port: 8002
spring:
application:
name: service-two #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册
2)分别在两个子模块中创建controller类用于接下来的测试
@RestController
@RequestMapping("/one")
public class OneController {
@GetMapping("getOne")
public String getOne(){
return "调用端口号为8001的Api接口";
}
}
@RestController
@RequestMapping("/two")
public class TwoController {
@GetMapping("getTwo")
public String getTwo(){
return "调用端口号为8002的Api接口";
}
}
3)在启动类上添加上@EnableDiscoveryClient
注解,用于服务的注册与发现
@SpringBootApplication
@EnableDiscoveryClient
public class OneApplication {
public static void main(String[] args) {
SpringApplication.run(OneApplication.class,args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class TwoApplication {
public static void main(String[] args) {
SpringApplication.run(TwoApplication.class,args);
}
}
- 在父工程下再创建gateway模块,用于配置GateWay的路由转发
1)导入相关依赖
<?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>
</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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2)在resource目录下创建application.yml中编写相关配置信息(这里配置的8222端口后,只需通过该端口就可以访问到8001端口和8002端口的Api接口服务了)
server:
port: 8222 #服务端口
spring:
application:
name: springcloud-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
gateway:
discovery:
locator:
enabled: true #使用服务发现路由
routes:
- id: SERVICE-ONE #设置路由id(理论上是可以随便写的)
uri: lb://service-one #设置路由的url lb://nacos服务注册名称
predicates:
- Path=/one/** #路径匹配规则
- id: SERVICE-TWO
uri: lb://service-two
predicates:
- Path=/two/** #路径匹配规则
3)在config包下创建CorsConfig.java
,用于解决跨域问题
@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);
}
}
4)在GateApplication中添加相关注解
@SpringBootApplication
@EnableDiscoveryClient
public class GateApplication {
public static void main(String[] args) {
SpringApplication.run(GateApplication.class,args);
}
}
以上基本就完成了基本代码的编写
接着启动Nacos,然后开启gateway
、service-one
、service-two
的服务
在Nacos控制台的服务列表中看到对应的服务名就表示nacos开启了服务发现与注册
然后在浏览器中访问http://localhost:8222/one/getOne、http://localhost:8222/two/getTwo发现可以访问成功就说明成功实现了路由转发
具体代码已上传到码云:https://gitee.com/culture36/spring-cloud_-gate-way
更多推荐
所有评论(0)