SpringCloud the Configuration Center Config (high availability)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41402200/article/details/91175252

Foreword

  SpringCloud micro-service leaders, the best floor plan.

  Spring Cloud Config is a configuration management solutions to solve the distributed system, which includes server and client in two parts.

  to a remote server configuration information (the default Git repository), and provides interfaces out form;

  The client reads the configuration file server provides an interface to facilitate application to initialize itself.

  If the configuration center there is a problem, it will lead to disastrous consequences, so the configuration center will do cluster in a production environment to ensure high availability.

  Here is the actual configuration of multiple high-availability configuration center (designated with a Git remote repository) registered with the registry.

Source

  GitHub Address: https://github.com/intomylife/SpringCloud

surroundings

  • JDK 1.8.0 +
  • Maven 3.0 +
  • SpringBoot 2.0.3
  • SpringCloud Finchley.RELEASE

development tools

  • IntelliJ IDEA

text

commons project

commons project - POM file

<?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.zwc</groupId>
    <artifactId>springcloud-config-eureka-commons</artifactId>
    <version>1.0</version>

    <!-- 工程名称和描述 -->
    <name>springcloud-config-eureka-commons</name>
    <description>公用工程</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
        <!-- 编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- jdk -->
        <java.version>1.8</java.version>

        <!-- SpringBoot -->
        <platform-bom.version>Cairo-SR3</platform-bom.version>

        <!-- SpringCloud -->
        <spring-cloud-dependencies.version>Finchley.RELEASE</spring-cloud-dependencies.version>
    </properties>

    <!-- 加入依赖 -->
    <dependencies>

    </dependencies>

    <!-- 依赖 jar 包版本管理的管理器 -->
    <!-- 如果 dependencies 里的 dependency 自己没有声明 version 元素,那么 maven 就此处来找版本声明。 -->
    <!-- 如果有,就会继承它;如果没有就会报错,告诉你没有版本信息 -->
    <!-- 优先级:如果 dependencies 里的 dependency 已经声明了版本信息,就不会生效此处的版本信息了 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot -->
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>${platform-bom.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Some common configuration dependent

commons project - Project Structure

 

Profiles

  Create a Git repository, which is to store configuration files, folder name: config-repo; here simulate different environments, so were constructed

  dev (development), uat (test) and Online (online) three different profiles; folder exists in the root directory of the project

- springcloud-config-eureka
 
  - config-repo
 
    - system-dev.properties
 
    - system-online.properties
 
    - system-uat.properties
 
  + springcloud-config-eureka-commons
  
  + springcloud-config-eureka-service

 

service project

  ① There are four modules in this project: a registration center, two server and a client

  ② two server other than the port inconsistent, other codes are basically the same

 

registry-service (Registry)

registry-service - POM file

<?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>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-registry-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-registry-service</name>
    <description>注册中心</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- 服务注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Main added spring-cloud-starter-netflix-eureka-server-dependent

registry-service - application.yml profile

# 端口
server:
  port: 8761

# 应用名称
spring:
  application:
    name: eureka-server

eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    # 是否向注册中心注册自己
    registerWithEureka: false
    # 是否向注册中心获取注册信息
    fetchRegistry: false
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • This uses the default port of 8761, of course, can change, but the discovery of registered address of the calling service center side of the port to be consistent with it

registry-service - start class

package com.zwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudConfigEurekaRegistryServiceApplication {

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

}
  • Add @EnableEurekaServer annotation startup class indicates that this project is a registered center

registry-server - to start the project

  1. Access project started successfully  http: // localhost: 8761 /  to see the eureka-server main page

 

server (for configuration information remotely)

server - POM file

  Note: There are two server, but the code is substantially uniform except the port, which would include all here a

<?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>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-serverfirst-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-serverfirst-service</name>
    <description>config server</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- config server 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!-- 提供者消费者 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Add spring-cloud-config-server-dependent: config server
  • Join spring-cloud-starter-netflix-eureka-client-dependent: the provision and registration services

server - application.yml profile

# 端口
server:
  port: 8000

spring:
  application:
    # 应用名称
    name: config-eureka-server
  cloud:
    config:
      server:
        git:
          # 仓库地址
          uri: https://github.com/intomylife/SpringCloud
          # 对应 {label} 部分,即 Git 的分支
          label: master
          # 仓库文件夹名称,多个以逗号分隔
          search-paths: springcloud-config-eureka/config-repo
          # git 仓库用户名(公开库可以不用填写)
          username:
          # git 仓库密码(公开库可以不用填写)
          password:

eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • Note that this can only be written to the repository name uri
  • If the configuration file in the repository multi-layer directory, search-paths began to write from the root directory
  • Note here configure registry address port port above 8761 is configured registry project

server - startup class

package com.zwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringcloudConfigEurekaServerfirstServiceApplication {

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

}
  • Add @EnableConfigServer notes indicate on the configuration center
  • Add @EnableEurekaClient comment indicates that this project can provide service to the registry

server - to start the project

  1. After starting the project successfully access: HTTP: // localhost: 8761 / (registry) can see the services have been registered came

  2. Access Address: HTTP: // localhost: 8000 / config-repo / dev (for complete configuration information)

  3. Output Content:

{
    "name":"config-repo",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[

    ]
}

  4. Visit address: HTTP: // localhost: 8000 / System / dev (for complete configuration information)

  5. Output Content:

{
    "name":"system",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-dev.properties",
            "source":{
                "hello":"I'm dev."
            }
        }
    ]
}

  6. Access Address: HTTP: // localhost: 8000 / system-dev.properties (Gets the specified configuration file contents)

  7. outputting content: 'hello: I'm dev.'

  8. Start another server (springcloud-config-eureka-serversecond-service)

  9. After the project started successfully access: HTTP: // localhost: 8761 / (registry) can see the services have been registered came

 10. Access Address: HTTP: // localhost: 8001 / config-repo / UAT (for complete configuration information)

 11. Output Content:

{
    "name":"config-repo",
    "profiles":[
        "uat"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[

    ]
}

 12. Access Address: HTTP: // localhost: 8001 / System / UAT (for complete configuration information)

 13. Output Content:

{
    "name":"system",
    "profiles":[
        "uat"
    ],
    "label":null,
    "version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-uat.properties",
            "source":{
                "hello":"I'm uat."
            }
        }
    ]
}

 14. The access address: HTTP: // localhost: 8001 / system-uat.properties (Gets the specified configuration file contents)

 15. outputting content: 'hello: I'm uat.'

 16. prove two server have been successfully retrieved from the remote repository configuration information

  Note: The interface to access the following rules:

/{application}/{profile}[/{label}]
 
/{application}-{profile}.yml
 
/{label}/{application}-{profile}.yml
 
/{application}-{profile}.properties
 
/{label}/{application}-{profile}.properties

 

Client (server reads configuration information in the registry)

client - POM file

<?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>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-eureka-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-eureka-client-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-eureka-client-service</name>
    <description>config client</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-eureka-commons</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- config 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- 提供者消费者 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Add spring-cloud-starter-config-dependent: config client
  • Join spring-cloud-starter-netflix-eureka-client-dependent: the provision and registration services

client - application.yml profile

# 端口
server:
  port: 8002

spring:
  application:
    # 应用名称
    name: config-eureka-client

client - bootstrap.yml configuration file (note)

spring:
  cloud:
    config:
      # 对应 {label} 部分,即 Git 的分支
      label: master
      # 对应 {application} 部分
      name: system
      # 对应 {profile} 部分
      profile: dev
      discovery:
        # 开启 Config 服务发现与注册
        enabled: true
        # 指定 server
        service-id: config-eureka-server

eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • bootstrap.yml configuration file is loaded prior to application.yml profile
  • Spring Cloud Config-related properties must be configured in the bootstrap.yml
  • Here it is not directly address the specified server, but the server application name (spring.application.name)
  • Read dev (development) environment configuration information from the file server's registry
  • Note here configure registry address port port above 8761 is configured registry project

client - controller distal Controller

package com.zwc.client.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName HelloController
 * @Desc TODO   读取 git 配置文件
 * @Date 2019/6/2 16:54
 * @Version 1.0
 */
@RestController
public class HelloController {

    @Value("${hello}")
    private String hello;

    /*
     * @ClassName HelloController
     * @Desc TODO   读取 git 配置文件
     * @Date 2019/6/2 16:56
     * @Version 1.0
     */
    @RequestMapping("/hello")
    public String hello() {
        return this.hello;
    }

}
  • @Value annotations can be used directly from the server to read the registry information corresponding to the configuration

client - start class

package com.zwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringcloudConfigEurekaClientServiceApplication {

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

}
  • Add @EnableEurekaClient comment indicates that this project can provide service to the registry

client - start the project

  1. After starting the project successfully access: HTTP: // localhost: 8761 / (registry) can see the services have been registered came

  2. 访问地址:http://localhost:8002/hello

  3. 输出内容:'I'm dev.'

  4. 证明 client 已经成功从注册中心的 server 中读取到了配置信息

  5. 此时当 client 已经启动完毕后,配置文件就已经全部读取到了,所以即使停止其中一个 server 或者停止

      全部 server,client 依然可以读取到配置文件。此处高可用应该是指在 client 启动时能保证有 server 可

      用

 

service 工程 - 项目结构

 

把多工程项目使用 IntelliJ IDEA  打开

  1. 把项目从 GitHub 中下载到你的本地
  2. 打开 IntelliJ IDEA 
  3. 点击 File -> Open
  4. 打开你下载到本地的项目目录
  5. springcloud-config-eureka -> springcloud-config-eureka-service(选择打开此工程)
  6. 打开 service 工程后
  7. 再次点击 File -> Project Structrue
  8. 选择 Modules,点击 '+' 符号
  9. 点击 Import  Module
  10. 还是打开你下载到本地的项目目录
  11. springcloud-config-eureka -> springcloud-config-eureka-commons -> pom.xml
  12. 点击 OK
  13. 点击 Next,Finish
  14. 点击 Apply,OK

 


 

希望能够帮助到你

over

 

 

 

Guess you like

Origin blog.csdn.net/qq_41402200/article/details/91175252