Spring cloud 2020.0.4 series Config

Keywords: Java Spring Boot Distribution Spring Cloud Microservices

1. General

As the old saying goes: if one road doesn't work, go the other way. If plan A can't be implemented, follow Plan B. It's always useful to prepare more plans.

 

Well, let's get down to business. Today, let's talk about the distributed configuration center Config of spring cloud. There are many service nodes in the microservice architecture. If you modify a configuration, you have to change every service, which is a disaster.

Therefore, we need to use the Config component to help us implement the modification, which can take effect in all relevant services.

Config supports storing configurations in local files, databases, SVN, Git, etc. Let's take Git as an example to talk about config.

 

Without much gossip, go straight to the code.

 

2. Git preparation  

2.1 register an account on GitHub's official website

Official website address: https://github.com/

 

2.2 create a   repository

Create a repository to place configuration files, such as my config repo

 

 

2.3 creating   repository new file

1) New   my-config-client-dev.yml configuration file, as follows:

info:
  profile: dev
       
name: zhuifengren
desc: hello world dev

 

2) New   my-config-client-prd.yml configuration file, as follows:

info:
  profile: prd
        
name: zhangsan
desc: hello world prd

 

3) File format description

{application}-{profile}.yml 

 

3. Setup of config server

3.1 main dependence

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.2</version>
        </dependency>

 

3.2 configure in application.yml

server:
  port: 40000
spring:
  application:
    name: my-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/w84422/my-config-repo.git   # git address
          force-pull: true  # Force pull resource file
          default-label: main   # Default pull branch

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Address of Eureka Server

 

3.3 add notes for startup class

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class MyConfigServerApplication {

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

 

3.4 start the Config Server service and verify it

1) Start service  

 

2) Call http://localhost:40000/my -Verify the config client / dev / main interface

The interface format is: http://ConfigServer IP: port / {application}/{profile}/{label}

label: branch of file stored in Git

 

3) Call   http://localhost:40000/main/my -Verify the config-client-dev.yml interface

The interface format is: http://ConfigServer IP: port / {label}/{application}-{profile}.yml(.json,. properties)

 

4. Setup of config client

4.1 main dependence  

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.2</version>
        </dependency>

 

4.2 configure in bootstrap.yml

server:
  port: 41000

spring:
  application:
    name: my-config-client
  cloud:
    config:     
      profile: dev  # Pull the profile of the file
      label: main   # Pulled branch
      name: my-config-client  # Specify the application of the pull configuration file. The value of spring.application.name is used by default
      discovery:
        enabled: true
        service-id: my-config-server    # Service name of config service

myDesc: ${desc}

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Address of Eureka Server

 

Note: config should be configured in bootstrap.yml.

 

4.3 add notes for startup class

@SpringBootApplication
@EnableDiscoveryClient
public class MyConfigClientApplication {

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

 

4.4 add Controller class for experiment

@RestController
public class MyConfigClientController {

    @Value("${info.profile}")
    private String profile;
    @Value("${name}")
    private String name;
    @Value("${myDesc}")
    private String desc;

    @RequestMapping("/info")
    public String getInfo() {

        String info = "profile: " + profile + "<br>";
        info += "name: " + name + "<br>";
        info += "desc: " + desc;

        return info;
    }
}

 

4.5 start the Config Client service and verify it

1) Start service

 

2) Call Controller interface verification, http://localhost:41000/info

 

5. Use asymmetric secret key to encrypt configuration attributes

5.1 general

Sometimes, it is unsafe to store some sensitive information in Git, so we need encryption to store them. We can use symmetric encryption and asymmetric encryption. Here is an introduction to asymmetric encryption.

 

5.2 JDK version requirements

You need to download the version above JDK 8u161. Otherwise, you need to download the JCE patch, which should be rarely used now   JDK   The version below 8u161 is. It is recommended to upgrade the JDK.

 

5.3 use   The built-in keytool command in JDK generates a certificate  

keytool -genkeypair -alias config-server -keyalg RSA -keystore c:/config-server.keystore

Remember the password in the generation process, which will be used later. I set it to 123456 for the time being

 

5.4 copy the generated certificate to the Config Server   In resources

 

5.5 configure in bootstrap.yml of Config Server

encrypt:
  key-store:
    location: config-server.keystore
    alias: config-server
    password: 123456
    secret: 123456

 

5.6 restart   Config Server service

 

5.7 calling   The interface of Config Server service to encrypt and decrypt text

1) Encryption interface: POST   http://localhost:40000/encrypt

Encrypted text, written in body

2) Decryption interface: POST   http://localhost:40000/decrypt

Decrypt the text and write it in the body

 

5.8 encrypt the text "Hello dev"

 

5.9 copy encrypted string to Git

 

 

Note: for the encrypted string, in Git's configuration file, single quotation marks shall be added and the beginning of the string shall be added   {cipher}

 

5.10 restart the Config Client and call the Controller interface test

Call Controller interface verification, http://localhost:41000/info

 

6. Dynamic refresh of configuration file

6.1 general

You should also notice that every time you modify the configuration in Git, you have to restart the Config Client. The experience is too bad.

Fortunately, Springboot helps us think of and solve this problem. By calling an interface, we can realize the dynamic refresh of the configuration file.

 

6.2 import dependency of config client project

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

 

six point three   Add configuration to bootstrap.yml file of Config Client project

management:
  endpoints:
    web:
      exposure:
        include: 'health,refresh'
  endpoint:
    health:
      show-details: always

 

6.4 on classes that need to dynamically get attributes from the configuration, add  @ RefreshScope annotation

For the experimental Controller class just written, you need to add the @ RefreshScope annotation

@RefreshScope
@RestController
public class MyConfigClientController {

    @Value("${info.profile}")
    private String profile;
    @Value("${name}")
    private String name;
    @Value("${myDesc}")
    private String desc;

 

6.5 restart   Config Client and call the interface for configuration dynamic refresh

1) Restart

2) Call   http://localhost:41000/actuator/refresh Interface to dynamically refresh the configuration

 

7. Overview

Today, I talked about the Config component of spring cloud. I hope it can be helpful to your work.

Welcome to like, comment, forward and pay attention:)

Pay attention to those who follow the wind to talk about Java and update Java dry goods every day.

 

8. official account

Fans talk about Java. Welcome to pay attention

 

Posted by Scud on Wed, 03 Nov 2021 00:11:19 -0700