Configuration Service: Spring Cloud Config (Chapter 6)

Copyright: without the author's permission is not allowed for any commercial purposes https://blog.csdn.net/weixin_38231448/article/details/91040186

作者:jiangzz 电话:15652034180 微信:jiangzz_wx 微信公众账号:jiangzz_wy

Spring Cloud Config is a new project Spring Cloud team created for distributed systems infrastructure and application services to provide centralized micro external configuration support, which is divided into server and client in two parts. Server called distributed distribution center, which is an independent micro-service applications, warehouse and configured to connect the client to provide access to configuration information, the encryption / decryption information access interface; client micro-architecture of each micro service service applications or infrastructure, resources and applications to manage their business-related content through the configuration specified configuration center, and obtaining and loading configuration information from a configuration center at boot time.
Here Insert Picture Description

Spring Cloud Config achieve the abstract mapping of server and client environment variables and configuration properties, so it is applicable not only to build Spring applications outside, also can be used in any application to run in other languages. Since the Spring Cloud Config achieve configuration center default using Git to store configuration information, use the configuration server Spring Cloud Config constructed natural to support version management for micro-service application configuration information, and can be easily managed through the Git client tools configuration and access content. Of course, it also provides support for other storage methods, such as SVN repository, localization file system.

Getting Started

Configure the server

  • pom.xml
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

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


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • application.propertoes
server.port = 8080
# 连接远程git读取配置信息
spring.cloud.config.server.git.uri = file:///D:/config
management.endpoints.web.exposure.include=*
  • SpringBootConfigServer
@SpringBootApplication
@EnableConfigServer
public class SpringBootConfigServer {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootConfigServer.class,args);
    }
}

In the D drive to create a local system git configuration file. This mode is used for local testing, under normal circumstances need to upload the configuration file to a remote git repository.

Administrator@PC-201610211330 MINGW64 /d/config (master)
$ ls -l
total 3
-rw-r--r-- 1 Administrator 197121 15 五月 31 10:01 SpringCloudConfigServer.properties
-rw-r--r-- 1 Administrator 197121 24 五月 31 10:02 SpringCloudConfigServer-dev.properties
-rw-r--r-- 1 Administrator 197121 20 五月 31 10:02 SpringCloudConfigServer-test.properties

Where:
SpringCloudConfigServer.properties

server.port=6060

SpringCloudConfigServer-dev.properties

server.port=8080

SpringCloudConfigServer-test.properties

server.port=7070

SpringCloud of configServer will start a web service to provide configuration information to other machines. Generally provides access to the following format

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

Which applicationrepresents the application name, profilerepresents the environmental parameters of operation, labelcan be understood as git branch option, the default is master
access address: HTTP: // localhost: 8080 / SpringCloudConfigServer / the Test / master

{
  "name": "SpringCloudConfigServer",
  "profiles": [
    "dev"
  ],
  "label": "master",
  "version": "6f95be091e7ba209ab17a9cb6a181bb9e045ea2d",
  "state": null,
  "propertySources": [
    {
      "name": "file:///D:/config//SpringCloudConfigServer-dev.properties",
      "source": {
        "server.port": "7070"
      }
    },
    {
      "name": "file:///D:/config//SpringCloudConfigServer.properties",
      "source": {
        "server.port": "6060"
      }
    }
  ]
}

Configuring the Client

  • pom.xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

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

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • ConfigClientApplication
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class,args);
    }
}
  • application.properties
server.servlet.context-path= /SpringCloudConfigClient
  • bootstrap.properties
# 指定连接的配置服务名字
spring.application.name=app1

spring.cloud.config.profile=test
spring.cloud.config.label=master
spring.cloud.config.name= SpringCloudConfigServer

spring.cloud.config.uri=http://localhost:8080
# 开启所有的健康检查
management.endpoints.web.exposure.include=*

Start the web application to view the results

 Fetching config from server at : http://localhost:8080
 ...
 Tomcat started on port(s): 7070 (http) with context path '/SpringCloudConfigClient'
 ...

Note : The above properties must be configured in bootstrap.properties, the configuration information to such configuration server is loaded correctly.

Remote git repository

Only you need to modify the configuration of the server as follows application.properties

server.port = 8080
# 连接远程git读取配置信息
spring.cloud.config.server.git.uri = https://github.com/jiangzz/applicationconfig.git
management.endpoints.web.exposure.include=*

Applicationconfig create a project on a remote server git, git and then uploaded to the server in the above test files

$ git remote add origin https://github.com/jiangzz/applicationconfig.git
$ git push --set-upstream origin master

Pattern Matching & multi-warehouse

Spring Cloud Config also by matching the pattern of the application and profile to support more complex business scenarios. This matching is achieved by a comma-separated format {application}/profilenamed wildcards:

server.port = 8080
# 连接远程git读取配置信息
spring.cloud.config.server.git.uri = https://github.com/jiangzz/applicationconfig.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.refresh-rate=5

spring.cloud.config.server.git.repos.aa.search-paths=app1
spring.cloud.config.server.git.repos.aa.clone-on-start=true
spring.cloud.config.server.git.repos.aa.pattern=app*
spring.cloud.config.server.git.repos.aa.uri=https://github.com/jiangzz/applicationconfig.git
spring.cloud.config.server.git.repos.aa.refresh-rate=5

management.endpoints.web.exposure.include=*

The client refresh RefreshScope

@RefreshScope
@RestController
public class TestController {
    @Value("${server.port}")
    private int port;
    @RequestMapping
    public int test(){
        return port;
    }
}

Perform the following shell

bin>curl.exe -X POST http://localhost:7070/SpringCloudConfigClient/actuator/refresh

Download window version of curl connection https://curl.haxx.se/windows/dl-7.65.0_1/curl-7.65.0_1-win64-mingw.zip

Eureka server configuration and integration

Add eureka in the original pom file

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add in Eureka rely application.properties

spring.application.name=CONFIG-SERVICE
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10

eureka.client.register-with-eureka=true
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

View Eureka registry can see just registered up service
Here Insert Picture Description

Configure the client integration and Eureka

Add eureka in the original pom file

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add Eureka rely on bootstrap.properties

# 指定连接的配置服务名字
spring.application.name=app1

spring.cloud.config.profile=test
spring.cloud.config.label=master
spring.cloud.config.name= SpringCloudConfigServer

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=CONFIG-SERVICE

# 开启所有的健康检查
management.endpoints.web.exposure.include=*

## 配置Eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

More exciting content focus

Micro-channel public account

Guess you like

Origin blog.csdn.net/weixin_38231448/article/details/91040186