注册中心/配置中心-Nacos

0
字数 2k
阅读时间 4 分钟

Nacos简直太棒了!
阿里 🐂🍺!
注册服务速度比 Eureka 快不说,还添加了注册服务的增删改管理(Eureka 必须手动发送 DELETE 请求到指定服务地址才能删除注册服务),配置中心还解决了 Spring Cloud Config 本地跑服务修改配置无法自动刷新的问题(因为配置在外网 git 仓库,除非使用内网穿透),而且 Eureka 转 Nacos 极其简单,复杂的服务迁移过程也不会超过五分钟。
简直太强了!!!
微不足道的缺点:没有注册鉴权功能,也就是说任何一个人只要知道你的 Nacos 注册地址,都可以把他的服务注册进去白嫖(不过 Nacos 一般部署在内网不会对外开放,所以问题不大)。

这里用上一章《授权中心-Oauth2+JWT》的代码做替换示范(包含注册中心和配置中心)。

Nacos

Nacos 官网对 Nacos 介绍如下:

Welcome to Nacos!
Nacos is committed to help you discover, configure, and manage your microservices. It provides a set of simple and useful features enabling you to realize dynamic service discovery, service configuration, service metadata and traffic management.
Nacos makes it easier and faster to construct, deliver and manage your microservices platform. It is the infrastructure that supports a service-centered modern application architecture with a microservices or cloud-native approach.

欢迎来到 Nacos 的世界!
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

简单来讲官方对 Nacos 定位就是服务发现、配置及其管理

配置 Nacos 持久化配置列表到 Mysql 并启动

上 Github 下载最新版 Nacos 编译 zip 包,下载完成后解压到任意位置。
找到 nacos 根目录下的\conf\application.properties.example 找到其中的 mysql 配置示例:

db.num=2
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.url.1=jdbc:mysql://11.163.152.9:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos_devtest
db.password=nacos

修改为:

#使用数据库类型为mysql(nacos1.1.4之后不添加这行配置的话默认走内置内存数据库)
spring.datasource.platform=mysql
#数据库数量
db.num=1
#数据库地址(多个的话按示例所示增加数字)
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
#数据库账号
db.user=root
#数据库密码
db.password=root

并粘贴到\conf\application.properties 文件最下面。
然后在 Mysql 新建名为 nacos 的库,并找到\conf\nacos-mysql.sql 文件,用 Navicat 等工具导入 nacos 库里。
双击\bin\startup.cmd 启动 Nacos。

删除 eureka 项目

在父项目 pom 里删除 eureka 的 module,然后再删除整个 eureka 子项目,并将父项目 pom 的 dependencyManagement 声明 spring-cloud-alibaba-dependencies 的依赖:

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>pom</packaging>

<groupId>xyz.liuzhuoming</groupId>
<artifactId>spring-cloud-oauth2-demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>0.9.0.RELEASE</spring-cloud-alibaba.version>
</properties>

<modules>
<module>auth</module>
<module>client</module>
<module>gateway</module>
</modules>

<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.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

修改 gateway 项目

首先删除 application.yml,并在 pom 文件删除 eureka-client 的依赖并添加 nacos-discovery 和 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xyz.liuzhuoming</groupId>
<artifactId>spring-cloud-oauth2-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<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-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

然后在 resources 下面新建 bootstrap.yml:

server:
port: 5432

spring:
application:
#config会根据application.name,profiles.active,nacos.config.file-extension匹配配置文件的Data Id,匹配关系是{name}-{active}.{file-extension},没有配置active则匹配{name}.{file-extension}
name: gateway
cloud:
nacos:
discovery:
#Nacos注册中心地址
server-addr: localhost:8848
config:
#Nacos配置中心地址
server-addr: ${spring.cloud.nacos.discovery.server-addr}
#Nacos配置文件类型(默认为properties,我们修改为yml)
file-extension: yml

修改 auth 项目

和 gateway 项目类似,删除 application.yml,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xyz.liuzhuoming</groupId>
<artifactId>spring-cloud-oauth2-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>auth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>auth</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<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-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>

添加 bootstrap.yml:

server:
port: 6543

spring:
application:
name: auth
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
main:
allow-bean-definition-overriding: true

修改 client 项目

和 gateway 项目类似,删除 application.yml,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xyz.liuzhuoming</groupId>
<artifactId>spring-cloud-oauth2-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

添加 bootstrap.yml:

spring:
application:
name: client
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml

配置 Nacos 的配置列表

浏览器访问http://localhost:8848/nacos/index.html,有时候访问不了,就看一下启动 Nacos 时候的日志:
01.jpg
红圈里的地址就是 Nacos 管理后台的地址。
进入配置列表页面,点击导入配置按钮导入配置文件(**点击下载**)。
配置正确的话列表显示如下:
02.jpg

测试

启动 auth,gateway,client 项目,用 Postman 请求http://localhost:5432/auth/oauth/token?username=user_2&password=123456&grant_type=password&scope=all,并在 Postman 的 Authorization 参数选择 Basic Auth 参数,并填写 oauth2 客户端 id 和密钥,请求之后返回结果:

{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NzE4MjU2NzUsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiYTpwb3N0IiwiYjpnZXQiLCJhOmdldCJdLCJqdGkiOiI4MWQ2NjhiYS0zNWJkLTQ5M2MtYThkZi0wZjQ1OTExNjI4YmYiLCJjbGllbnRfaWQiOiJjbGllbnRfMiIsInNjb3BlIjpbInNlcnZlciJdfQ.laJS02W_I5Q7Js5aiisToW0ejko8R9ayyDdZIPFMCUEFKZ2cU1mxswLpeX9eaKQZchlyqjtVnmUC2J1E9aDJww5RadTAM7Hifspxhde6dVmCVIoOkNJHi60UU55nsG6Ax2U1Wz1y3CKPgohOYY35MM_31_2fzhlmSnE8VbhrdQRFMXpsURhQBxT4deOU3IL4gO8tIKeU_oN4wyI07fKzM2Rqy8vCCbNxvhqBH9LcLCB-yilCEtol3rpimO36dvxjKmojpFTlwYLRYB4g3XibWn9bipJfwDLnBSCvUjtO4bM7rrFL0Uf3vns4KZ-a77yxN6s6MsLhuIUdc3UmqVK8xQ",
"token_type": "bearer",
"refresh_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbInNlcnZlciJdLCJhdGkiOiI4MWQ2NjhiYS0zNWJkLTQ5M2MtYThkZi0wZjQ1OTExNjI4YmYiLCJleHAiOjE1NzQ0MTU4NzUsImF1dGhvcml0aWVzIjpbImE6cG9zdCIsImI6Z2V0IiwiYTpnZXQiXSwianRpIjoiNzdmNDY0ZDEtNjM1Mi00NTU0LWEzOGUtMTVlNTVjNWUxNzU4IiwiY2xpZW50X2lkIjoiY2xpZW50XzIifQ.qtHi4lxwQ0ruGg4N9vLPix6Uy8rCLpMm2AxEOHO7XJSVP0y0JQXgN3Ceil_cg5H56sGDT50BGg1rR6tyXMTjZ2Vu-XowA-jUFywb0Uhlhiu1kCw90Vel9GFRIbhxcewIBzzXPz7M5z04ks-eA33EJW9W_aPs2411SMZfBgW3poOMzkbwORpBiwId6YJpckwdCuPhKkKF5NoN7x62vg7mcm-fKlCqUv3jVNhkZWQHflfqtM9IHn5V3-8kjCLrUByyI1K1RUjrDrDAT9SPpdeOcAlLZzJmDQ5_vvtEMuZlJ4RaHd8jQtR9QZ9dakm0ffENgkYIl8KMcAP6cLgm6rli7A",
"expires_in": 1799,
"scope": "server",
"jti": "81d668ba-35bd-493c-a8df-0f45911628bf"
}

成功获取到 token,说明 Nacos 配置成功。


系列文章 #Spring Cloud

(1)前言

(2)注册中心-Eureka

(3)服务间调用-Feign(🔒)

(4)路由中心-Gateway

(5)配置中心-Config

(6)配置中心-Bus

(7)监控中心-Admin

(8)监控中心-Sleuth+Zipkin(🔒)

(9)监控中心-Elasticsearch+Zipkin(🔒)

(10)监控中心-HystrixDashboard+Turbine(🔒)

(11)授权中心-Oauth2+JWT

(12)注册中心/配置中心-Nacos

(13)动态路由-Gateway

(14)授权中心-Oauth2+JWT补全


动态路由-Gateway 授权中心-Oauth2+JWT