环境搭建
创建父工程 RuoYi-Cloud
使用 idea 创建 maven 结构的父工程,其 gav坐标为
<groupId>com.ruoyi</groupId><artifactId>ruoyi</artifactId><version>1.0.0</version>
删除src目录
父工程一般都不需要写代码,因此 src 目录可以删除。
父工程 pom.xml 文件
主要做了哪些事情?
①、
②、全局定义了 SpringBoot、SpringCloud、以及SpringCloud Alibaba的版本号
③、覆盖了 alibaba-nacos 自带的nacos-client 版本号(1.4.1),将它改为了2.0.3。因为我们 nacos 服务器选择的也是 2.0.3 版本
<?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.ruoyi</groupId><artifactId>ruoyi</artifactId><version>1.0.0</version><packaging>pom</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-boot.version>2.5.3</spring-boot.version><spring-cloud.version>2020.0.3</spring-cloud.version><spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version><alibaba.nacos.version>2.0.3</alibaba.nacos.version></properties><dependencies><!-- bootstrap 启动器 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency></dependencies><dependencyManagement><dependencies><!-- SpringBoot 依赖配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringCloud 微服务 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringCloud Alibaba 微服务 --><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><!-- Alibaba Nacos 配置 --><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>${alibaba.nacos.version}</version></dependency></dependencies></dependencyManagement></project>
创建网关模块 ruoyi-gateway
在父工程下创建网关模块(ruoyi-gateway),其 gav 坐标为:
<parent><artifactId>ruoyi</artifactId><groupId>com.ruoyi</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-gateway</artifactId>
注意了,ruoyi-gateway 作为 ruoyi 的子模块,因此在 父工程中会多出
<modules><module>ruoyi-gateway</module></modules>
ruoyi-gateway 模块的 pom.xml 文件
做了哪些事情?
①、作为网关需要引入 spring-cloud-starter-gateway 依赖
②、我们需要把网关程序注册到 nacos, 因此需要引入 spring-cloud-starter-alibaba-nacos-discovery
③、我们需要实现动态配置功能,因此将 nacos作为配置中心,网关程序启动时需要读取 在nacos中的配置,
因此引入 spring-cloud-starter-alibaba-nacos-config 依赖
<?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><groupId>com.ruoyi</groupId><artifactId>ruoyi</artifactId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-gateway</artifactId><dependencies><!-- SpringCloud Gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- SpringCloud Alibaba Nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- SpringCloud Alibaba Nacos Config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency></dependencies></project>
创建 bootstrap.yml 文件
做了哪些事情?
①、凡是微服务都必须定义的2个东西。“端口号” 和 “应用名”
②、服务注册的地址,即告诉网关程序服务往哪里注册
③、配置中心地址,即告诉网关程序去哪里获取配置信息
server:port: 8080spring:application:name: ruoyi-gatewayprofiles:active: devcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848config:# 配置中心地址server-addr: 127.0.0.1:8848# 配置文件格式file-extension: yml# 共享配置shared-configs:- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
配置中心
打开 nacos 服务 -> 登陆到nacos控制台 -> 在配置管理中新建配置 ruoyi-gateway-dev.yml
现在我们还没有用到配置,所以该文件可以空着内容不写。
main方法
注意了,在较新版本 @EnableDiscoveryClient 注解不写也同样能完成微服务注册
package com.ruoyi.gateway;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })//@EnableDiscoveryClient 这个注解可以不需要写了public class RuoYiGatewayApplication {public static void main(String[] args) {SpringApplication.run(RuoYiGatewayApplication.class, args);}}
网关程序注册到nacos
运行 RuoYiGatewayApplication 的main 方法
创建认证中心模块 ruoyi-auth
在父工程下创建网关模块(ruoyi-auth),其 gav 坐标为:
<parent><artifactId>ruoyi</artifactId><groupId>com.ruoyi</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-auth</artifactId>
ruoyi-auth 模块的 pom.xml 文件
<?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>ruoyi</artifactId><groupId>com.ruoyi</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ruoyi-auth</artifactId><dependencies><!-- SpringBoot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>
创建 bootstrap.yml 文件
主要做了哪些事情?
①、定义了端口和应用名
server:port: 9200spring:application:name: ruoyi-authprofiles:# 环境配置active: dev
主启动类
package com.ruoyi.auth;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class RuoYiAuthApplication {public static void main(String[] args) {SpringApplication.run(RuoYiAuthApplication.class, args);}}
编写测试Controller - HelloController
package com.ruoyi.auth.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {@Value("${spring.application.name}")private String appName;@Value("${server.port}")private String port;@RequestMapping("/auth/hello")public String hello() {return appName + " 项目,端口号:" + port;}}
测试
运行 RuoYiAuthApplication 类的 main 方法。
网关转发
网关作为整个微服务的入口,一般都有如下几个用途!
①、请求转发。 即路由功能
微服务项目一般都是前后端分离的,那么前端发过来的请求不是请求到具体的微服务上,而是统一的经过 网关程序进行转发。这样做可以屏蔽到具体微服务地址
②、过滤
所有的请求都会经过网关,因此我们可以使用过滤功能,对请求进行加工和处理
简单的转发
在 ruoyi-gateway-dev.yml 配置文件中,配置转发规则。
如下表示如果发送 http://localhost:8080/auth/hello 请求,网关中的路径断言规则中 /auth/** 匹配到了,所以会
将请求转发到 http://localhost:9200
spring:cloud:gateway:discovery:routes:- id: ruoyi-authuri: http://localhost:9200predicates:- Path=/auth/**
转发路径去掉前缀
比如 http://localhost:8080/auth/hello 会转发时需要去掉 /auth 前缀。怎么弄?
①、修改 @RequestMapping(“/auth/hello”) 为 @RequestMapping(“/hello”)
②、在 ruoyi-gateway-dev.yml 文件中添加过滤器 StripPrefix 。
spring:cloud:gateway:discovery:routes:- id: ruoyi-authuri: http://localhost:9200predicates:- Path=/auth/**filters:- StripPrefix=1 # 去掉前缀
③、重启 网关和认证中心的程序,然后发送 http://localhost:8080/auth/hello 请求,观察效果是否和直接访问
http://localhost:9200/hello 效果一样?
网关的负载均衡转发
之前我们在路由转发的时候,路径都是写死的。例如:uri: http://localhost:9200 ,这样无法转发到多台微服务上
将认证中心模块(ruoyi-auth)注册到 nacos。
①、ruoyi-auth 模块引入依赖
<!-- SpringCloud Alibaba Nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
②、编写 bootstrap.yml 配置,使其注册到 nacos
server:port: 9200spring:application:name: ruoyi-authprofiles:# 环境配置active: devcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848
网关模块 ruoyi-gateway
①、引入依赖, 如果跳过这一步,在最后运行网关程序的时候,会报出 type=Service Unavailable, status=503 错误
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency>
②、修改 ruoyi-gateway 在nacos中的配置,添加上负载均衡功能
spring:cloud:gateway:discovery:locator:lowerCaseServiceId: trueenabled: trueroutes:- id: ruoyi-auth#uri: http://localhost:9200#uri: lb://ruoyi-authpredicates:- Path=/auth/**filters:- StripPrefix=1 # 去掉前缀
测试
重启网关和认证中心程序

