SpringCloud微服務實戰——搭建企業級開發框架(九):使用Nacos發現、配置和管理微服務

語言: CN / TW / HK

Nacos是一個更易於構建雲原生應用的動態服務發現、配置管理和服務管理平臺,Nacos 致力於幫助您發現、配置和管理微服務。Nacos 提供了一組簡單易用的特性集,幫助您快速實現動態服務發現、服務配置、服務元資料及流量管理。

1、跟之前新建SpringBoot自定義擴充套件一樣,我們在GitEgg_Platform中新建gitegg-platform-cloud子工程,此工程主要用於Spring Cloud相關功能的自定義及擴充套件。

2、在GitEgg_Platform中的gitegg-platform-bom子工程新增SpringCloud Alibaba的依賴

<!--?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>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.3.3.RELEASE</version>
        <relativepath>
    </relativepath></parent>

    <modelversion>4.0.0</modelversion>

    <groupid>com.gitegg.platform</groupid>
    <artifactid>gitegg-platform-bom</artifactid>
    <name>${project.artifactId}</name>
    <version>${gitegg.project.version}</version>
    <packaging>pom</packaging>

    <properties>
        <!-- jdk版本1.8 -->
        <java.version>1.8</java.version>
        <!-- maven-compiler-plugin外掛版本,Java程式碼編譯 -->
        <maven.plugin.version>3.8.1</maven.plugin.version>
        <!-- maven編譯時指定編碼UTF-8 -->
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <!-- 專案統一字符集編碼UTF-8 -->
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <!-- 專案統一字符集編碼UTF-8 -->
        <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>

        <!-- GitEgg專案統一設定版本號 -->
        <gitegg.project.version>1.0-SNAPSHOT</gitegg.project.version>

        <!-- mysql資料庫驅動 -->
        <mysql.connector.version>8.0.17</mysql.connector.version>
        <!-- postgresql資料庫驅動 -->
        <postgresql.connector.version>9.1-901.jdbc4</postgresql.connector.version>
        <!-- 資料庫連線池Druid -->
        <druid.version>1.1.23</druid.version>
        <!-- Mybatis Plus增強工具 -->
        <mybatis.plus.version>3.4.0</mybatis.plus.version>
        <!-- Knife4j Swagger2文件 -->
        <knife4j.version>3.0.1</knife4j.version>
        <!-- Spring Cloud Alibaba -->
        <spring.cloud.alibaba>2.2.3.RELEASE</spring.cloud.alibaba>

    </properties>

    <dependencymanagement>
        <dependencies>
            <!-- gitegg資料庫驅動及連線池 -->
            <dependency>
                <groupid>com.gitegg.platform</groupid>
                <artifactid>gitegg-platform-db</artifactid>
                <version>${gitegg.project.version}</version>
            </dependency>
            <!-- gitegg mybatis-plus -->
            <dependency>
                <groupid>com.gitegg.platform</groupid>
                <artifactid>gitegg-platform-mybatis</artifactid>
                <version>${gitegg.project.version}</version>
            </dependency>
            <!-- gitegg swagger2-knife4j -->
            <dependency>
                <groupid>com.gitegg.platform</groupid>
                <artifactid>gitegg-platform-swagger</artifactid>
                <version>${gitegg.project.version}</version>
            </dependency>
            <!-- gitegg boot自定義擴充套件 -->
            <dependency>
                <groupid>com.gitegg.platform</groupid>
                <artifactid>gitegg-platform-boot</artifactid>
                <version>${gitegg.project.version}</version>
            </dependency>
            <!-- gitegg cloud自定義擴充套件 -->
            <dependency>
                <groupid>com.gitegg.platform</groupid>
                <artifactid>gitegg-platform-cloud</artifactid>
                <version>${gitegg.project.version}</version>
            </dependency>
            <!-- mysql資料庫驅動 -->
            <dependency>
                <groupid>mysql</groupid>
                <artifactid>mysql-connector-java</artifactid>
                <version>${mysql.connector.version}</version>
            </dependency>
            <!-- postgresql資料庫驅動 -->
            <dependency>
                <groupid>postgresql</groupid>
                <artifactid>postgresql</artifactid>
                <version>${postgresql.connector.version}</version>
            </dependency>
            <!-- 資料庫連線池 -->
            <dependency>
                <groupid>com.alibaba</groupid>
                <artifactid>druid-spring-boot-starter</artifactid>
                <version>${druid.version}</version>
            </dependency>
            <!-- Mybatis Plus增強工具 -->
            <dependency>
                <groupid>com.baomidou</groupid>
                <artifactid>mybatis-plus-boot-starter</artifactid>
                <version>${mybatis.plus.version}</version>
            </dependency>
            <!-- Swagger2 knife4j bom方式引入 -->
            <dependency>
                <groupid>com.github.xiaoymin</groupid>
                <artifactid>knife4j-dependencies</artifactid>
                <version>${knife4j.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Spring Cloud Alibaba -->
            <dependency>
                <groupid>com.alibaba.cloud</groupid>
                <artifactid>spring-cloud-alibaba-dependencies</artifactid>
                <version>${spring.cloud.alibaba}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencymanagement>


</project>

3、在gitegg-platform-cloud工程中引入spring-cloud-starter-alibaba-nacos-discovery

<!--?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>GitEgg-Platform</artifactid>
        <groupid>com.gitegg.platform</groupid>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelversion>4.0.0</modelversion>

    <artifactid>gitegg-platform-cloud</artifactid>
    <name>${project.artifactId}</name>
    <version>${project.parent.version}</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Nacos 服務註冊發現-->
        <dependency>
            <groupid>com.alibaba.cloud</groupid>
            <artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid>
        </dependency>
    </dependencies>
</project>

4、GitEgg_Platform工程重新執行install,在GitEgg_Cloud的子工程gitegg-service中引入gitegg-platform-cloud

<!--?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>GitEgg-Cloud</artifactid>
        <groupid>com.gitegg.cloud</groupid>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelversion>4.0.0</modelversion>

    <artifactid>gitegg-service</artifactid>
    <packaging>pom</packaging>
    <modules>
        <module>gitegg-service-base</module>
        <module>gitegg-service-bigdata</module>
        <module>gitegg-service-system</module>
    </modules>

    <dependencies>
        <!-- gitegg Spring Boot自定義及擴充套件 -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-boot</artifactid>
        </dependency>
        <!-- gitegg Spring Cloud自定義及擴充套件 -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-cloud</artifactid>
        </dependency>
        <!-- gitegg資料庫驅動及連線池 -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-db</artifactid>
        </dependency>
        <!-- gitegg mybatis-plus -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-mybatis</artifactid>
        </dependency>
        <!-- gitegg swagger2-knife4j -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-swagger</artifactid>
        </dependency>
        <!-- spring boot web核心包 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <!-- spring boot 健康監控 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-actuator</artifactid>
        </dependency>
    </dependencies>

</project>

5、修改application.yml檔案,新增nacos配置:

server:
  port: 8001
spring:
  application:
    name: gitegg-service-system
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://127.0.0.1/gitegg_cloud?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
    username: root
    password: root
    initialSize: 1
    minIdle: 3
    maxActive: 20
    # 配置獲取連線等待超時的時間
    maxWait: 60000
    # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一個連線在池中最小生存的時間,單位是毫秒
    minEvictableIdleTimeMillis: 30000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 開啟PSCache,並且指定每個連線上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆
    filters: config,stat,slf4j
    # 通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;
    # 合併多個DruidDataSource的監控資料
    useGlobalDataSourceStat: true
mybatis-plus:
      mapper-locations: classpath*:/com/gitegg/*/*/mapper/*Mapper.xml
      typeAliasesPackage: com.gitegg.*.*.entity
      global-config:
        #主鍵型別  0:"資料庫ID自增", 1:"使用者輸入ID",2:"全域性唯一ID (數字型別唯一ID)", 3:"全域性唯一ID UUID";
        id-type: 2
        #欄位策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
        field-strategy: 2
        #駝峰下劃線轉換
        db-column-underline: true
        #重新整理mapper 除錯神器
        refresh-mapper: true
        #資料庫大寫下劃線轉換
        #capital-mode: true
        #邏輯刪除配置
        logic-delete-value: 1
        logic-not-delete-value: 0
      configuration:
        map-underscore-to-camel-case: true
        cache-enabled: false

6、修改GitEggSystemApplication.java添加註解@EnableDiscoveryClient,然後執行GitEggSystemApplication:

package com.gitegg.service.system;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;

/**
 * gitegg-system 啟動類
 */
@EnableDiscoveryClient
@ComponentScan(basePackages = "com.gitegg")
@MapperScan("com.gitegg.*.*.mapper")
@SpringBootApplication
public class GitEggSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(GitEggSystemApplication.class,args);
    }

}

7、在瀏覽器中開啟nacos的地址,點選左側選單的服務列表,可以檢視到服務已經註冊到nacos

本文原始碼在 https://gitee.com/wmz1930/GitEgg 的chapter-09分支。