Spring Cloud -Hoxton.RELEASE(十一):注册中心/配置中心-Nacos


0

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

这里用上一章《Spring Cloud -Hoxton.RELEASE(十):授权中心-Spring Cloud 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配置成功。