1

一、Nacos简介

Nacos(Naming and Configuration Service)致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现服务配置、服务元数据及流量管理。

二、Nacos安装

1、Nacos依赖

Nacos基于java开发的,运行依赖于java环境。

2、Nacos安装

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

image.png

2、集群部署

1、集群架构

nacos集群架构图.png

  • 高可用Nginx集群
  • Nacos集群(至少三个实例)
  • 高可用数据库集群(取代Nacos内嵌数据库)

2、本地虚拟机模拟集群部署

本地环境准备
系统版本 机器IP 部署应用 应用版本
CentOS 7.6 192.168.15.146 Nginx 1.18.0
CentOS 7.6 192.168.15.145 Nacos 1.2.1
CentOS 7.6 192.168.15.147 Nacos 1.2.1
CentOS 7.6 192.168.15.148 Nacos 1.2.1
CentOS 7.6 192.168.15.141 MySQL 5.7.24

在本地PC机上利用VMware workstation虚拟出如上表所示的几台机器,其中Nginx和MySQL都是采用的单实例,仅做练习使用。

搭建步骤
初始化nacos必须的数据库表并配置
  • 找到Nacos安装目录下提供的数据库脚本文件

nacos数据库脚本.png

  • 在MySQL实例创建nacos_config库并导入脚本

nacos依赖的数据库.png

  • 修改修改Nacos配置文件,指向MySQL实例,替换其内嵌数据库

Nacos配置文件位置.png

#*************** 切换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文件

复制cluster.conf文件.png

  • 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实例.png
集群模式启动nacos实例成功.png

  • 尝试访问nacos管理页,测试三个实例是否正常

image.png

说明:如果三个实例以集群模式正常启动,那么分别访问三个实例的管理页就是展示以上登录页了。如果不能访问,则可能防火墙未开放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配置
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管理页上维护一个配置

image.png
image.png

  • 本地启动微服务并访问

image.png
image.png


neojayway
52 声望10 粉丝

学无止境,每天进步一点点