Nacos常用的二大功能:
- 服务注册和发现
- 配置中心(可热更新)
Nacos安装
下载地址:https://github.com/alibaba/nacos/releases/tag/2.0.3
下载解压就可以使用
Nacos使用
1.单机启动
单机启动一般用于学习和测试,命令窗口cd到安装目录/nacos/bin,使用命令startup.cmd -m standalone,浏览器访问地址:http://127.0.0.1:8848/nacos/index.html,账号和密码都是nacos。
2.集群启动
生产环境一般使用集群模式启动,启动多台nacos,使用ngnix负载反向代理,并使用mysql作为保存方式,后面会详细介绍。
SpringBoot集成Nacos
版本号管理
<properties>
<java.version>1.8</java.version>
<mybatis.version>2.1.0</mybatis.version>
<mysql.connector.version>6.0.6</mysql.connector.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-netflix.version>2.2.9.RELEASE</spring-cloud-netflix.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>
依赖管理
<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-netflix-dependencies</artifactId>
<version>${spring-cloud-netflix.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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用bootstrap.yml配置需要依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- RestTemplate负载均衡需要依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- 服务发现需要依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心需要依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置文件
bootstrap.yml配置
spring:
application:
name: service-user #服务名称
profiles:
active: dev # 环境
cloud:
nacos:
config: # 配置中心
server-addr: 127.0.0.1:8848
file-extension: yaml
discovery: # 服务注册和发现
server-addr: 127.0.0.1:8848
application.yml配置
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://180.76.137.232:3306/demo?characterEncoding=utf-8&useSSL=false
username: root
password: admin@mysql
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.service.user.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
支持注册和发现
在启动类添加注解
@SpringBootApplication
@EnableDiscoveryClient // 服务注册和发现
@MapperScan("com.example.service.user.dao")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
Nacos集群
这里以单台mysql为例
1.安装mysql
2.新建nacos数据库
3.nacos数据库导入运行nacos的mysql文件,mysql路径:安装目录/nacos/conf/nacos-mysql.sql
4.安装目录/nacos/conf/cluster.conf.example 文件名去掉example,修改为 => 安装目录/nacos/conf/cluster.conf
5.修改cluster.conf内容(比如有3台nacos服务器:127.0.0.1:8847,127.0.0.1:8846,127.0.0.1:8845,因为我只用了一台电脑,所以copy3分nacos,修改端口号,分别启动):
#it is ip
#example
127.0.0.1:8847
127.0.0.1:8846
127.0.0.1:8845
6.修改application.properties内容:
找到下面内容,去掉#号注释
...
# 修改端口号
### Default web server port:
server.port=8848
...
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
# 修改使用mysql作为数据库
spring.datasource.platform=mysql
### Count of DB:
# 数据库个数
db.num=1
### Connect URL of DB:
# 数据库地址
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
# 数据库账号
db.user.0=nacos
# 数据库密码
db.password.0=nacos
7.安装ngnix
8.修改 安装目录/nginx-1.16.1/conf/nginx.conf 内容:
http {
...
# 集群
upstream cluster {
server 127.0.0.1:8847;
server 127.0.0.1:8846;
server 127.0.0.1:8845;
}
location / {
#root html; # 注意要注释掉
#index index.html index.htm; # 注意要注释掉
proxy_pass http://cluster; # 使用代理
}
...
}
9.cd到 安装目录/nginx-1.16.1,输入 start nginx.exe 启动Nginx
10.Nacos访问路径:http://localhost:80/nacos/index.html (Nginx默认访问端口80,可省略)
11.项目代码修改:
bootstrap.yml配置
spring:
application:
name: service-user #服务名称
profiles:
active: dev # 环境
cloud:
nacos:
config: # 配置中心
server-addr: 127.0.0.1:80 # 端口改为80
file-extension: yaml
discovery: # 服务注册和发现
server-addr: 127.0.0.1:80 # 端口改为80