文章目录
1.sa-token1.37集成(基于token)
1.文档网址
https://sa-token.cc/v/v1.37.0/doc.html#/
2.sun-club-auth-application-controller引入依赖
<!-- Sa-Token 权限认证,在线文档:https://sa-token.cc -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.37.0</version>
</dependency>
3.application.yml
sa-token:
# token 名称(同时也是 cookie 名称)
token-name: satoken
# token 有效期(单位:秒) 默认30天,-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
active-timeout: -1
# 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token)
is-share: true
# token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik)
token-style: random-32
# 是否输出操作日志
is-log: true
token-prefix: jichi
4.sun-club-auth-application-controller测试的controller
1.UserController.java
package com.sunxiansheng.auth.application.controller;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Description:
* @Author sun
* @Create 2024/6/2 17:25
* @Version 1.0
*/
@RestController
@RequestMapping("/user/")
public class UserController {
// 测试登录,浏览器访问: http://localhost:8081/user/doLogin?username=zhang&password=123456
@RequestMapping("doLogin")
public SaResult doLogin(String username, String password) {
// 此处仅作模拟示例,真实项目需要从数据库中查询数据进行比对
if("zhang".equals(username) && "123456".equals(password)) {
StpUtil.login(10001);
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
// 第3步,返回给前端
return SaResult.data(tokenInfo);
}
return SaResult.error("登录失败");
}
// 查询登录状态,浏览器访问: http://localhost:8081/user/isLogin
@RequestMapping("isLogin")
public String isLogin() {
return "当前会话是否登录:" + StpUtil.isLogin();
}
}
2.启动测试
1.登录,得到satoken
2.验证登录时在Header中携带token,注意,需要携带在application.yml中配置的前缀 jichi
2.网关统一鉴权环境搭建
1.OSS微服务注册到Nacos
1.调整依赖 pom.xml
1.properties
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
2.dependencyManagement 指定版本
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
3.由于使用了dependencyManagement指定版本,那么就不需要dependency来指定版本了,删除
4.由于指定了版本,所有用到springboot、cloud、cloudalibaba的都不需要指定版本
5.添加maven打包的常规配置
<!-- maven打包常规配置 -->
<build>
<finalName>${project.artifactId}</finalName>
<!--打包成jar包时的名字-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
6.cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
7.引入Nacos的服务发现依赖
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
8.完整的依赖
<!-- maven的配置 -->
<!-- 解决java: -source 1.5 中不支持 diamond 运算符 问题 -->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 与log4j2冲突 -->
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- minio依赖 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<!-- Nacos的配置中心 -->
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<!-- log4j2,如果使用了这个日志,springframework需要排除spring-boot-starter-logging -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<!-- 统一管理配置,以后所有的boot、cloud、alibaba都不需要指定版本了 -->
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
<!-- 配置阿里云仓库 -->
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- maven打包常规配置 -->
<build>
<finalName>${project.artifactId}</finalName>
<!--打包成jar包时的名字-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.bootstrap.yml 配置服务发现
3.启动OSS服务测试,注册成功
2.GateWay搭建以及路由配置
1.依赖配置(注意:gateway不能引入springboot-web)
<!-- maven的配置 -->
<!-- 解决java: -source 1.5 中不支持 diamond 运算符 问题 -->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<!-- 统一管理配置,以后所有的boot、cloud、alibaba都不需要指定版本了 -->
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
<!-- minio依赖 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- Nacos -->
<dependency>
<!-- Nacos的配置中心 -->
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- GateWay -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<!-- 配置阿里云仓库 -->
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- maven打包常规配置 -->
<build>
<finalName>${project.artifactId}</finalName>
<!--打包成jar包时的名字-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.application.yml
请求oss模块的方式:GateWay的ip+端口号/oss/上下文路径+资源路径
server:
port: 5000
spring:
cloud:
gateway:
routes:
# 请求到oss模块的规则是:GateWay的ip+端口号/oss/上下文路径+资源路径,而/oss的前缀会在真正转发的时候被去掉 也就是真正请求的完整路径是:GateWay的ip+端口号/上下文路径+资源路径
- id: oss # 路由ID,唯一即可
uri: lb://sub-club-oss # 转发到的后端服务的服务名,需要与Nacos中注册的服务名一致
predicates:
- Path=/oss/** # 断言,表示请求路径匹配/oss/**的请求将会被转发到后端服务
filters:
- StripPrefix=1 # 表示在将请求转发到后端服务之前,去掉路径中的第一个前缀,如果请求路径是/oss/file/upload,经过这个过滤器处理后,路径将变为/file/upload
3.bootstrap.yml
spring:
application:
name: sub-club-gateway # 服务名称
profiles:
active: dev # 激活的环境
cloud:
nacos:
discovery:
enabled: true # 启用服务发现
server-addr: # Nacos地址
4.编写启动类 GatewayApplication.java
package com.sunxiansheng.club.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* Description: 网关启动类
* @Author sun
* @Create 2024/5/31 9:20
* @Version 1.0
*/
@SpringBootApplication
@ComponentScan("com.sunxiansheng")
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
5.启动报错,连接的是localhost:8848
1.报错信息
[NACOS SocketTimeoutException httpGet] currentServerAddr:http://localhost:8848, err : connect timed out
2.原因
我并没有用到Nacos的配置中心,但是却引入了配置中心的依赖,所以会导致这个问题,注释掉即可
3.重启测试,注册成功
6.通过GateWay来访问OSS模块
1.请求方式
GateWay的ip+端口号/oss/上下文路径+资源路径
2.请求,发现503服务不可用
http://localhost:5000/oss/listBuckets
3.原因是网关使用了lb的方式,进行负载均衡,但是没引入负载均衡器依赖
<!-- 负载均衡器,网关使用lb方式时引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
4.再次请求,成功响应
3.鉴权微服务及刷题微服务注册到Nacos
1.sun-club-auth 模块
1.引入Nacos依赖
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<!-- Nacos的配置中心 -->
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.指定版本
<!-- maven的配置 -->
<!-- 解决java: -source 1.5 中不支持 diamond 运算符 问题 -->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<!-- 统一管理配置,以后所有的boot、cloud、alibaba都不需要指定版本了 -->
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
3.完整pom.xml
<!-- maven的配置 -->
<!-- 解决java: -source 1.5 中不支持 diamond 运算符 问题 -->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入sun-club-auth-application-controller的依赖,则启动这个模块,就能找到 -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-auth-application-controller</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入sun-club-infra -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-auth-infra</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 公共模块 -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-auth-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<!-- Nacos的配置中心 -->
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<!-- 统一管理配置,以后所有的boot、cloud、alibaba都不需要指定版本了 -->
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
<!-- maven打包常规配置 -->
<build>
<finalName>${project.artifactId}</finalName>
<!--打包成jar包时的名字-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 指定打包插件的版本 -->
<version>2.3.0.RELEASE</version>
<executions>
<execution>
<goals>
<!-- 将所有的包都打到这个模块中 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.bootstrap.yml
spring:
application:
name: sub-club-auth # 服务名称
profiles:
active: dev # 激活的环境
cloud:
nacos:
config:
server-addr: # Nacos地址
prefix: ${spring.application.name} # 配置前缀为服务名,sub-club-oss-dev为配置文件名
group: DEFAULT_GROUP # 配置分组
namespace: # 命名空间,如果在public命名空间则不需要配置
file-extension: yaml
discovery:
enabled: true # 启用服务发现
server-addr: # Nacos地址
5.启动测试,注册成功
2.sun-club-subject 模块
1.引入Nacos依赖
<!-- maven的配置 -->
<!-- 解决java: -source 1.5 中不支持 diamond 运算符 问题 -->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入sun-club-application-controller的依赖,则启动这个模块,就能找到 -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-application-controller</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入sun-club-infra -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-infra</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入sun-club-common -->
<dependency>
<groupId>com.sun.club</groupId>
<artifactId>sun-club-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Nacos的配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- cloudalibaba2021版本不会加载bootstrap所以需要手动引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<!-- 统一管理配置,以后所有的boot、cloud、alibaba都不需要指定版本了 -->
<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.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>
<!-- maven打包常规配置 -->
<build>
<finalName>${project.artifactId}</finalName>
<!--打包成jar包时的名字-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 指定打包插件的版本 -->
<version>2.3.0.RELEASE</version>
<executions>
<execution>
<goals>
<!-- 将所有的包都打到这个模块中 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.bootstrap.yml
spring:
application:
name: sub-club-subject # 服务名称
profiles:
active: dev # 激活的环境
cloud:
nacos:
config:
server-addr: # Nacos地址
prefix: ${spring.application.name} # 配置前缀为服务名,sub-club-subject-dev为配置文件名
group: DEFAULT_GROUP # 配置分组
namespace: # 命名空间,如果在public命名空间则不需要配置
file-extension: yaml
discovery:
enabled: true # 启用服务发现
server-addr: # Nacos地址
3.启动测试,注册成功
4.鉴权微服务及刷题微服务网关配置
1.application.yml 配置网关
# 请求到auth模块的规则是:GateWay的ip+端口号/auth/上下文路径+资源路径,而/auth的前缀会在真正转发的时候被去掉 也就是真正请求的完整路径是:GateWay的ip+端口号/上下文路径+资源路径
- id: auth
uri: lb://sub-club-auth
predicates:
- Path=/auth/**
filters:
- StripPrefix=1
# 请求到subject模块的规则是:GateWay的ip+端口号/subject/上下文路径+资源路径,而/subject的前缀会在真正转发的时候被去掉 也就是真正请求的完整路径是:GateWay的ip+端口号/上下文路径+资源路径
- id: subject
uri: lb://sub-club-subject
predicates:
- Path=/subject/**
filters:
- StripPrefix=1