一、Nacos 简介
Nacos(Naming and Configuration Service)致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
详情查看Nacos 官方文档[1]
二、Nacos 安装
1、Nacos 依赖
Nacos 基于 java 开发的,运行依赖于 java 环境。
依赖 64 bit JDK 1.8+,前往官网下载 JDK[2]
2、Nacos 安装
下载编译后压缩包,最新稳定版本[3]
- unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz cd nacos/bin
三、Nacos 部署
1、单实例部署
单实例部署不适合生产环境,单点故障是致命的。
Linux 单实例非集群模式启动命令
- startup.sh -m standalone
Linux 单实例非集群模式关闭命令
- shutdown.sh
访问 nacos 管理页面,初始化用户名密码均为 nacos
2、集群部署
1、集群架构
- 高可用 Nginx 集群
- Nacos 集群(至少三个实例)
- 高可用数据库集群(取代 Nacos 内嵌数据库)
2、本地虚拟机模拟集群部署
本地环境准备
在本地 PC 机上利用 VMware workstation 虚拟出如上表所示的几台机器,其中 Nginx 和 MySQL 都是采用的单实例,仅做练习使用。
搭建步骤
初始化 nacos 必须的数据库表并配置
找到 Nacos 安装目录下提供的数据库脚本文件
在 MySQL 实例创建 nacos_config 库并导入脚本
修改修改 Nacos 配置文件,指向 MySQL 实例,替换其内嵌数据库
- #*************** 切换Nacos内嵌数据库平台为MySQL ***************#
- spring.datasource.platform=mysql
- db.num=1
- db.url.0=jdbc:mysql://192.168.15.141:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
- db.user=root
- db.password=123456
说明:三台 nacos 实例都需要切换 MySQL 平台,均需执行以上操作
nacos 集群配置
复制 cluster.conf 文件
Nacos 集群配置,修改 cluster.conf 文件
- [root@localhost conf]# vim ./cluster.conf
- #it is ip
- #example
- 192.168.15.145
- 192.168.15.147
- 192.168.15.148
说明:三台 nacos 实例都需要做以上集群配置,至此关于 nacos 的配置结束了,可以尝试以集群模式启动三个 nacos 实例了
以集群模式分别启动三个 nacos 实例
尝试访问 nacos 管理页,测试三个实例是否正常
说明:如果三个实例以集群模式正常启动,那么分别访问三个实例的管理页就是展示以上登录页了。如果不能访问,则可能防火墙未开放 nacos 服务的端口,可执行如下命令。
- [root@localhost bin]# firewall-cmd --add-port=8848/tcp --permanent
- success
- [root@localhost bin]# firewall-cmd --reload
- success
- [root@localhost bin]# firewall-cmd --list-all
- public (active)
- target: default
- icmp-block-inversion: no
- interfaces: ens33
- sources:
- services: ssh dhcpv6-client
- ports: 27017/tcp 8848/tcp
- protocols:
- masquerade: no
- forward-ports:
- source-ports:
- icmp-blocks:
- rich rules:
- [root@localhost bin]#
Nginx 配置
- Nginx 安装参考,Nginx 源码安装[4]
- 修改 Nginx 配置文件 nginx.conf
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- #nacos集群负载均衡
- upstream nacos-cluster {
- server 192.168.15.145:8848;
- server 192.168.15.147:8848;
- server 192.168.15.148:8848;
- }
- server {
- listen 80;
- server_name 192.168.15.146;
- location / {
- #root html;
- #index index.html index.htm;
- proxy_pass http://nacos-cluster;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
启动 Nginx
- /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
微服务配置
微服务父 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.atguigu.springcloud</groupId>
- <artifactId>cloud2020</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>pom</packaging>
- <!-- 模块 -->
- <modules>
- <module>cloud-alibaba-nacos-config-client-3377</module>
- </modules>
- <!-- 统一管理jar版本 -->
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- <junit.version>4.12</junit.version>
- <log4j.version>1.2.17</log4j.version>
- <lombok.version>1.16.18</lombok.version>
- <mysql.version>5.1.47</mysql.version>
- <druid.version>1.1.16</druid.version>
- <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
- </properties>
- <!-- 统一依赖管理 -->
- <dependencyManagement>
- <dependencies>
- <!-- spring boot 2.2.2 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot</artifactId>
- <version>2.2.2.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- spring cloud Hoxton.SR1 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Hoxton.SR1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- spring cloud alibaba 2.1.0.RELEASE -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>2.1.0.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- mysql连接器 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>${mysql.version}</version>
- </dependency>
- <!--druid 数据源-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>${druid.version}</version>
- </dependency>
- <!-- mybatis 整合 spring -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>${mybatis.spring.boot.version}</version>
- </dependency>
- <!-- junit -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- </dependency>
- <!-- log4j -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <!-- lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${lombok.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <fork>true</fork>
- <addResources>true</addResources>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
微服务 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>cloud2020</artifactId>
- <groupId>com.atguigu.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>cloud-alibaba-nacos-config-client-3377</artifactId>
- <dependencies>
- <!-- nacos config -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- </dependency>
- <!-- nacos discovery -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <!-- web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- actuator -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <!-- test -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- devtools -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <!-- lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
- </project>
微服务 bootstrap.yml 配置
- server:
- port: 3377
- spring:
- application:
- name: nacos-config-client
- cloud:
- nacos:
- discovery:
- #server-addr: my.nacos.com:8848
- #nacos集群配置(Nginx)
- server-addr: 192.168.15.146:80
- config:
- #server-addr: my.nacos.com:8848
- #nacos集群配置(Nginx)
- server-addr: 192.168.15.146:80
- #指定yaml格式的配置
- file-extension: yaml
- #指定分组
- group: DEV_GROUP
- #指定命名空间ID
- namespace: my_nacos_namespace
微服务启动类配置
- package com.atguigu.springcloud;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- @SpringBootApplication
- @EnableDiscoveryClient
- public class NacosConfigClientMain3377 {
- public static void main(String[] args) {
- SpringApplication.run(NacosConfigClientMain3377.class, args);
- }
- }
微服务 Controller 读取 nacos 配置
- package com.atguigu.springcloud.controller;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @Slf4j
- @RefreshScope //支持Nacos的动态刷新功能
- public class ConfigClientController {
- @Value("${config.info}")
- private String configInfo;
- @GetMapping("/config/info")
- public String getConfigInfo() {
- return configInfo;
- }
- }
在 nacos 管理页上维护一个配置
本地启动微服务并访问
参考
[1]Nacos 官方文档: https://nacos.io/zh-cn/docs/what-is-nacos.html
[2]前往官网下载 JDK: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
[3]最新稳定版本: https://github.com/alibaba/nacos/releases[4]Nginx 源码安装: https://segmentfault.com/a/1190000022860417