乐趣区

关于前端:SpringCloud微服务实战搭建企业级开发框架九使用Nacos发现配置和管理微服务

    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 />
    </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 分支。

退出移动版