Full project address: Micro-service-plus

Series entry:

  • Take you to the background with SpringCloud micro-service (entrance)

preface

It mainly introduces how to switch multi-environment with one key, and how to install and use NACOS

Profile Switching between multiple environments

Generally speaking, our environment is divided into dev,test,prod, and multi-environment switching helps us to quickly switch the environment we want for development and construction, reducing the modification of configuration files. Improve code quality and development efficiency.

The configuration file

The root directory/pom. XML

<! Unified configuration test development production environment -->
<profiles>
  <profile>// Development environment<id>dev</id>
    <properties>
      <activatedProperties>dev</activatedProperties>
      <baseLinuxIp>8.135.1.141</baseLinuxIp>
      <nacosIpPort>${baseLinuxIp}:8848</nacosIpPort>
      <nacosWeight>1000</nacosWeight>
      <nacosDiscoveryIp>127.0.0.1</nacosDiscoveryIp>
      <nacosNamespace>df87d91b-b1ab-4720-8e1c-5a147edc6ddc</nacosNamespace>
      <nacosGroup>nacos-group-${activatedProperties}</nacosGroup>
      <nacosUsername>jzfai</nacosUsername>
      <nacosPassword>123456</nacosPassword>
    </properties>
  </profile>
  <profile>// Test the environment<id>test</id>
    <! -- add this, default select -->
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <! The following properties are used for variables in the bootstrap.yml file that can be configured via @activatedProperties@ -->
    <properties>
      <activatedProperties>test</activatedProperties>
      <baseLinuxIp>8.135.1.141</baseLinuxIp>
      <nacosIpPort>${baseLinuxIp}:8848</nacosIpPort>
      <nacosWeight>1000</nacosWeight>
      <nacosDiscoveryIp>127.0.0.1</nacosDiscoveryIp>
      <nacosNamespace>df87d91b-b1ab-4720-8e1c-5a147edc6ddc</nacosNamespace>
      <nacosGroup>nacos-group-${activatedProperties}</nacosGroup>
      <nacosUsername>jzfai</nacosUsername>
      <nacosPassword>123456</nacosPassword>
    </properties>
  </profile>// Production environment<profile>
    <id>prod</id>
    <properties>
      <activatedProperties>prod</activatedProperties>
      <baseLinuxIp>8.135.1.141</baseLinuxIp>
      <nacosIpPort>${baseLinuxIp}:8848</nacosIpPort>
      <nacosWeight>1000</nacosWeight>
      <nacosDiscoveryIp>127.0.0.1</nacosDiscoveryIp>
      <nacosNamespace>df87d91b-b1ab-4720-8e1c-5a147edc6ddc</nacosNamespace>
      <nacosGroup>nacos-group-${activatedProperties}</nacosGroup>
      <nacosUsername>jzfai</nacosUsername>
      <nacosPassword>123456</nacosPassword>
    </properties>
  </profile>
</profiles>
Copy the code

How to use

Note: Please refresh the dependency and directory after changing the environment each time, otherwise there may be a problem that @x@ cannot be found

After switching the environment, use bootstrap.yml or application.yml

Such as: activatedProperties Configured environment variables
spring:
  application:
    name: @project.artifactId@
  profiles:
    active: @activatedProperties@
Copy the code

That’s the end of the multi-environment switch

Nacos installation and use

Nacos is used as the configuration center and registry, and Erruka can be used as the registry depending on your company’s business needs. This section only introduces nacOS

Docker installation naocs

Upload the nacOS folder to the /docker file on Linux

[root@iZwz9izs4qf3b81quqwp61Z nacos]# ls
bin  conf  data  logs  nacos
[root@iZwz9izs4qf3b81quqwp61Z nacos]# pwd
/docker/nacos
Copy the code

Run commands to install

Docker run \ - e - d PREFER_HOST_MODE MODE = = IP \ - e standalone \ \ - m 0.8 g - v/docker/nacos/data: / home/nacos/data \ - v /docker/nacos/conf:/home/nacos/conf \ -v /docker/nacos/bin:/home/nacos/bin \ -v /docker/nacos/logs:/home/nacos/logs \ -p 8848.8848 \ --network host \ --name nacOS-1.3.1_8848 \ --restart=always \ nacOS/nacOS-server :1.3.1Copy the code

After the installation is complete, use IP address :8848 for access. The default user name and password are jzfai:123456

Nacos is used in SpringBoot

Introduction of depend on

ty-common/pom.xml

<! --nacos-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code

Configured to use

ty-example/bootstrap.yml

spring:
  application:
    name: @project.artifactId@
  profiles:
    active: ""
  cloud:
    nacos:
      // The service gets the configuration
      config:
        // Enable naOCS configuration
        enabled: true
        namespace: @nacosNamespace@
        group: @nacosGroup@
        username: @nacosUsername@
        password: @nacosPassword@
        server-addr: @nacosIpPort@
        // Will be obtained by default Prefix-spring.profiles. acitve-file-extension file, for example ty-example.yml
        prefix: @project.artifactId@
        file-extension: yml
        Config file for sharing
        shared-configs:
           Get the common environment configuration (dev, test,prod may be different)
          - data-id: application-main.yml
            group: @nacosGroup@
            refresh: true
        Additional configuration files
        extension-configs:
          Get database configuration
          - data-id: application-db.yml
            group: @nacosGroup@
            refresh: true
          - data-id: application-rabbit.yml
            group: @nacosGroup@
            refresh: true
           Get the Redis configuration
          - data-id: application-redis.yml
            group: @nacosGroup@
            refresh: true
          Get seATA configuration
          - data-id: application-seata.yml
            group: @nacosGroup@
            refresh: true
      // Configure service registration
      discovery:
        // Namespace
        namespace: @nacosNamespace@
        / / group
        group: @nacosGroup@
        username: @nacosUsername@
        password: @nacosPassword@
        // The registered address of the service
        server-addr: @nacosIpPort@
        weight: @nacosWeight@
        // IP registered to nacOS
        ip: @nacosDiscoveryIp@
Copy the code

What is the configuration sequence and priority for shared-configs, extension-configs, and config

Read configuration order: config->extension-configs->shared-configs Priority: config->extension-configs->shared-configs

The configuration before the priority overrides the later configuration