1、项目搭建
首先,我们需要搭建一个 SpringCloud 微服务项目,后续的文章将基于此项目作为学习演示使用
- 1、创建 cloud-learn 目录,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.biz</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>spring cloud 学习项目</description>
<modules>
<module>cloud-gateway</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<spring-boot.version>2.7.7</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 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>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- bootstrap 启动器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
父模块主要定义了 spring-cloud、spring-cloud-alibaba、spring-boot 的版本信息
- 2、创建子模块 cloud-gateway,pom.xml 文件内容如下:
<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>
<parent>
<groupId>com.biz</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>cloud-gateway</artifactId>
<description>网关模块</description>
<dependencies>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 3、项目启动类
package com.biz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* 网关启动程序
*
* @author ruoyi
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 网关启动成功 ლ(´ڡ`ლ)゙");
}
}
整体项目目录如下:
2、注册 Nacos
src/resources 目录下,新建 bootstrap.yml 文件,内容如下:
spring:
application:
# 应用名称
name: cloud-gateway
cloud:
nacos:
discovery:
# 注册中心地址
server-addr: 127.0.0.1:8848
启动项目,打开 nacos 控制台查看【服务列表】
3、注册鉴权
上面我们在注册进入 nacos 时,只需要填写 nacos 的地址即可注册,这样非常不安全,所以我们需要在注册这一步加上鉴权操作
- 1、修改 nacos 配置文件
修改 nacos/conf/application.properties 文件
nacos.core.auth.enabled=false
改为
nacos.core.auth.enabled=true
修改完配置信息,不需要重启 nacos 服务,我们的微服务已经无法注册进去了
- 2、修改微服务配置文件
spring:
application:
# 应用名称
name: cloud-gateway
cloud:
nacos:
username: nacos
password: nacos
discovery:
# 注册中心地址
server-addr: 127.0.0.1:8848
需要加上 nacos 的用户密码,则可以注册成功
4、命名空间
- 1、创建命名空间
public 为默认保留命名空间,我们如果使用命名空间来区分开发环境和生产环境,如图,创建了两个命名空间
dev,开发环境命名空间
prod,生产环境命名空间
- 2、注册命名空间
spring:
application:
# 应用名称
name: cloud-gateway
cloud:
nacos:
username: nacos
password: nacos
discovery:
# 注册中心地址
server-addr: 127.0.0.1:8848
namespace: 482e3612-4bf5-4d77-96ef-e963c6e7a4bb
我们需要复制命名空间 ID 进行配置,效果展示
我们注册的服务就进入了 dev 命名空间了
可以配合权限管理控制不同的用户操作不同的资源
如您在阅读中发现不足,欢迎留言!!!