当前位置:网站首页>Config learning notes component

Config learning notes component

2022-04-23 15:45:00 Wind is not greedy

1. brief introduction

config( To configure ) Also known as Unified configuration center , seeing the name of a thing one thinks of its function , It is the unified management of configuration , The advantage of unified configuration management is that the same service configuration is consistent when deploying service applications in large-scale clusters in the future , If you want to modify the configuration in the future, you only need to modify it uniformly and synchronize it all , There is no need to manually maintain one service by one .

Spring Cloud Config yes Spring Cloud The earliest configuration center in the family , Although it was later released Consul It can replace the function of configuration center , however Config It still applies to Spring Cloud project , Function can be realized through simple configuration .

The configuration file is more familiar to us , In especial Spring Boot project , In addition to introducing the corresponding maven Out of bag , The rest of the work is to improve the configuration file , for example mysql、redis 、security Related configuration . In addition to the basic configuration of project operation , There are also configurations that have something to do with our business , For example, seven cattle storage 、 SMS related 、 Mail related , Or some business switches .

For some simple projects , We usually put the relevant configuration in a separate configuration file , With properties perhaps yml The format appears , The more convenient way is to put it directly into application.properties or application.yml in . But there's an obvious problem with this approach , That's it , When the configuration is modified , Service must be restarted , Otherwise, the configuration will not take effect .

At present, there are many open-source configuration centers , For example, Ctrip Apollo、 Ant gold disconf etc. , contrast Spring Cloud Config, These configuration centers are more powerful . If you are interested, you can try it .

https://www.cnblogs.com/fengzheng/p/11242128.html

flow chart

Unified configuration center component flow chart

image-20200721180134903

Another flowchart :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-0fJilxUi-1650548834040)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/6yfmk0.jpg)]

First step : Upload files to GitLab;

The second step :GitLab adopt Webhook call ConfigServer;

Third 、 The four step :ConfigServer Pull configuration to local warehouse ( Cache a copy );

Step five :ConfigServer adopt Stream Trigger each ConfigClient;

Step six :ConfigClient Get the configuration and update .
————————————————
Copyright notice : This paper is about CSDN Blogger 「wangfei0904306」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/wangfei0904306/article/details/79420122

Be careful ConfigServer It's fine too / We need to cluster

2. Use

Dependency relationship :configclient --> configserver --> Distal git Warehouse

0. Distal git Warehouse

stay Github Create a remote warehouse

1. ConfigServer

ConfigServer It's also a service , Unified configuration center service .

Create a new springboot Project as ConfigServer

  • Entrance class

    • @SpringBootApplication
    • @EnableConfigServer
  • rely on

    • springboot-web
    • consul
    • actuator
    • config-server
  • To configure

    server.port=8848
    spring.application.name=CONFIGSERVER
    
    # consul server  Service registration address 
    spring.cloud.consul.host=localhost
    spring.cloud.consul.port=8500
    spring.cloud.consul.discovery.service-name=${spring.application.name}
    
    #  Remote warehouse address 
    spring.cloud.config.server.git.uri=https://github.com/zewei94yomi/springcloud_config_tutorial.git		#  Specify the location of the warehouse url
    spring.cloud.config.server.git.default-label=main		#  Specify the branch to access 
    #spring.cloud.config.server.git.username=        Private repository access user name 
    #spring.cloud.config.server.git.password=		 Private warehouse access password 
    

About how to pull files

2. ConfigClient

Create a new springboot Project as ConfigClient:

  • Entrance class
  • rely on
    • config
  • To configure

If a certain client Now there's a need :DemoController According to the in the configuration file name To output , And different environments ( Development environment and production environment ) The value in is different , When uploading these different configuration files to the remote git Warehouse time , How to make configserver Pull ?

@RestController
public class DemoController {
    

    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

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

    @GetMapping("/demo")
    public String demo() {
    
        log.info("Demo OK!");
        return "Demo OK! " + name;
    }
}

Public profile configclient.properties

server.port=8990
spring.application.name=CONFIGCLIENT

Development environment profile configclient-dev.properties

name=zhangsan

Production environment profile configclient-pro.properties

name=lisi

Upload these three configuration files to github Remote warehouse

IeFhQO

You can use configserver Pull the configuration file from the remote warehouse , Note the rules for pulling files :

- label/name-profiles.yml|properties|json
	`label    Go to that branch to get   By default master Branch 
	`name     Represents the name of the specific configuration file to be read 
	`profile  Represents the read profile environment 

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MGQ2OcGX-1650548834041)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/image-20200722105313716.png)]

Note that the supported file formats are : properties, json and yml Format , And the file name must have "-"!

If the corresponding file is found , The configuration file will be merged with the main configuration file , A configuration file as a whole , And save it to the local temporary folder .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-4otovlyN-1650548834041)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/w2VGvn.png)]

thus ,configserver You can independently go to the remote configuration center to pull the configuration , Next we need to let configclient Go to configserver Get configuration . however config Of client Don't know config server The address of , Therefore, it is still necessary to specify in the configuration file config server The address of .

config client Configuration file for :

  1. Registry information
  2. config server Information ( service ID)
  3. Profile information
#  Configure the registry ( This must not be placed in the remote configuration center , Must be placed locally ,
#  Otherwise, you can't get... Through the registry config server)
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

#  Open the unified configuration center service 
spring.cloud.config.discovery.enabled=true
#  Specify the unique service ID of the unified configuration service center 
spring.cloud.config.discovery.service-id=CONFIGSERVER

#  Which configuration file to get :1. Determine the branch  2. Make sure the file name  3. Determine the environment 
spring.cloud.config.label=main
spring.cloud.config.name=configclient
spring.cloud.config.profile=pro

thus , We seem to have finished config client Basic configuration , And set the relevant addresses and information of the service registration center and the unified configuration center , Try to start client You'll find a mistake :

image-20200722102847187

Error reason :

Currently used in the project is application.properties Start project , Use this configuration file in springboot The project will not wait for remote configuration pull during project startup , Start... Directly according to the content in the configuration file , So when you need a registry , Service port and other information , The remote configuration has not been pulled , So report a mistake directly .

Solution :

You should wait for the remote configuration to be pulled when the project starts , After the remote configuration is successfully pulled, it can be started according to the remote configuration information , In order to fulfill the above requirements springboot The official offers a solution , When using the unified configuration center, the configuration file name of the micro service should be changed to bootstrap.(properties|yml),bootstrap.properties When starting a project as a configuration , The remote configuration will be pulled first , After the remote configuration is pulled successfully, start the current application according to the remote configuration .

image-20200722103435260

image-20200722103823678

Visit at this time :http://localhost:8990/demo:

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-5C3puKZl-1650548834044)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/E5eyGr.png)]

'lisi’ Successfully from the far end git The warehouse is always config server Pulled by config client get .

3. Configure the refresh

at present config Of client The end can already be controlled by config server Pull the configuration of the remote warehouse and then read the local configuration , But every time you modify the configuration of the remote warehouse, you need to restart config server/client To take effect .

In the production environment , There can be a lot of microservices , Every time the remote configuration is modified , It is not possible to restart all services , At this time, the service that modifies the configuration needs to be able to refresh the remote modified configuration , So you don't have to restart the service every time to take effect , Further improve the maintenance efficiency of micro service system . stay springcloud Manual refresh configuration and automatic refresh configuration are also provided in , Here we first use the manual configuration file refresh .

Manually refresh

  1. The default is off , We need to be in config client Add and refresh the exposed endpoint .

endpoint Namely springcloud Pages that have been developed for us , But it's not on by default , We have to start it manually when necessary ( expose ).

#  Turn on everything web Endpoint exposure 
#  Be careful :properties in * Without double quotation marks ;yaml in * It's in double quotation marks :"*"
management.endpoints.web.exposure.include=*
  1. Add the annotation of refresh configuration in the class that needs to refresh the code @RefreshScope
@RestController
@RefreshScope	//  Add the configuration refresh annotation to the class that needs to be refreshed 
public class DemoController {
    

    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

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

    @GetMapping("/demo")
    public String demo() {
    
        log.info("Demo OK!");
        return "Demo OK! " + name;
    }
}

After adding the note , All through the controller Request ( Then you may enter the business layer 、 The data layer …) Will start configuration refresh ; Others do not pass the controller And didn't use @RefreshScope Requests for annotations are not refreshed .

  1. Add... To the remote configuration name And start the test

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1jiJGKJE-1650548834045)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/vW13iK.png)]

Visit found , The configuration information can be obtained correctly

RsvNq5

At this point, modify the remote configuration information

6P77iX

Revisit , It is found that the configuration information has not been read

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-fHba0NbT-1650548834047)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/RsvNq5.png)]

reason : must Call the refresh configuration interface To refresh the configuration

With POST Method , Send a request to the that needs to refresh the configuration client, Manually call the refresh configuration interface :

curl -X POST http://localhost:8990/actuator/refresh

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-cN2Fv73Q-1650548834047)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/Bfd3hc.png)]

Visit again at this time demo, It is found that the new configuration information can be read

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WQbJxI62-1650548834048)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/6Ooh4j.png)]

Automatically refresh (Bus Components )

Although manual configuration refresh solves : There is no need to restart the service for profile update , But we still need to send it manually POST Request to call the interface to refresh the configuration ; In addition, every service that wants to update must send a request to the service , In the microservice framework, there are usually many services , This is very inconvenient . The ideal state should be : The remote configuration file has changed ,config server Detect the change and pull the latest configuration , Corresponding config client Update again .

Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions. AMQP and Kafka broker implementations are included with the project. Alternatively, any Spring Cloud Stream binder found on the classpath will work out of the box as a transport. -- From website

springcloudbus Using lightweight message broker to connect the nodes of distributed system . then , You can use it to broadcast state changes ( For example, configuration changes ) Or other management instructions .AMQP and Kafka broker( middleware ) The implementation is included in the project . perhaps , Any found on the classpath springcloudstream Binders can be used as transport .

Popular definition : bus be called springcloud in The message bus , It is mainly used in micro service system When the remote configuration update is realized, all clients are notified to refresh the configuration information in the form of broadcast , Avoid manually restarting the service

Schematic diagram

image-20200723150335451

config server And all config client All connected to MQ, Now just take the initiative to config server Send a POST Just ask .

On how to use RabbitMQ, Because you need to linux The operation is too troublesome. There is no practical operation here , It is recommended to use docker.

About how to build a cluster , Here I share what I'm learning zookeeper Helpful links :

  1. MAC VMwave Install multiple Linux(centos) Virtual machine tutorial , The tutorial demonstrates how to VMware Fusion Set up multiple centos Virtual machine and simple network configuration ( Sure ping Get the same ), Note that it involves clone The operation of the machine requires Fusion Pro edition . And then you can use SecureCRT To pass ssh Connect each virtual machine ( If the virtual machine is built locally, you must start each machine every time and then SecureCRT Come on ssh)
  2. Linux Environment building ( Basics 、 The network configuration 、 colony 、hadoop Pre course ) Turn off firewall , Time synchronization , Cluster building ,JDK,ssh Password free login , Rights management …

It is omitted here “ build RabbitMQ service ” The process of …

Here are the copied notes :

Automatic configuration refresh

# 1. Introduce... Into all projects bus rely on 
<!-- introduce bus rely on -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

image-20200723104333906

# 2. Configure the unified configuration center to connect to mq
spring.rabbitmq.host=localhost											# Connecting hosts 
spring.rabbitmq.port=5672														# Connect mq port 
spring.rabbitmq.username=user												# Connect mq user name 
spring.rabbitmq.password=password										# Connect mq password 
# 3. Add a connection to the remote configuration mq To configure 

image-20200723105645915

# 4. Start the unified configuration center service 
-  Normal start 

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ffFFMbgL-1650548834051)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/image-20200723111220198.png)]

# 5. Start the client service 
-  Join in bus After the component, the client starts and reports an error 
-  reason springcloud There will be no error if the default link is not to the remote server , But in use bus The connection to the remote service must be started when the message bus fails, and an error is reported 

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-PpnwHer6-1650548834051)(https://cdn.jsdelivr.net/gh/zewei94yomi/ImageLoader@master/uPic/image-20200723111312496.png)]

spring.cloud.config.fail-fast=true

image-20200723111754187

# 6. After the remote configuration is modified, the service is executed in the configuration center post Interface refresh configuration 
- curl -X POST http://localhost:7878/actuator/bus-refresh

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Nd5j3orP-1650548834053)(Coding/ Distributed systems /SpringCloud/ Source project /02springcloud/± º«/SpringCloud Microservice toolset v1.1.assets/image-20200723112316476.png)]

# 7. Through the above configuration, the unified refresh of the configuration is realized 

Specify the service refresh configuration

# 1. explain 
-  By default curl -X POST http://localhost:7878/actuator/bus-refresh Refreshing the configuration in this way is in the form of all broadcasts , That is, all micro services can receive the refresh configuration notification , But sometimes we only modify the configuration of a service , At this time, the notification of other services is redundant , Therefore, you need to specify a service to notify 

# 2. Specify the service refresh configuration implementation 
-  Specify the port to refresh a specific service : curl -X POST http://localhost:7878/actuator/bus-refresh/configclient:9090
-  Designated Services id Refresh the service cluster node : curl -X POST http://localhost:7878/actuator/bus-refresh/configclient
 	[ Be careful :][configclient Represents the unique identifier of the refresh service ]

Integrate webhook Automatic refresh

# 1. To configure webhooks
-  explain : git The warehouse provides a unique mechanism :  This mechanism is a listening mechanism      Monitoring is the event submitted by the warehouse  ...   Trigger corresponding event execution 
- javascript:  event    Event source  html label    event :  Trigger a specific action click  ...   Event handler : function 
-  add to webhooks
-  stay webhooks Add refresh configuration interface in 

-  Intranet penetrating websites : https://natapp.cn/

image-20200723120419412

image-20200723120947229

# 2. solve 400 Error problem 
-  Add a filter to the server of the configuration center to solve the problem (springcloud In a pit )
@Component
public class UrlFilter  implements Filter {
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
 
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
 
        String url = new String(httpServletRequest.getRequestURI());
 
        // Filter only /actuator/bus-refresh request 
        if (!url.endsWith("/bus-refresh")) {
    
            chain.doFilter(request, response);
            return;
        }
 
        // Get the original body
        String body = readAsChars(httpServletRequest);
 
        System.out.println("original body: "+ body);
 
        // Use HttpServletRequest Package original request to achieve modification post In request body The purpose of the content 
        CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);
 
        chain.doFilter(requestWrapper, response);
 
    }
 
    @Override
    public void destroy() {
    
 
    }
 
    private class CustometRequestWrapper extends HttpServletRequestWrapper {
    
        public CustometRequestWrapper(HttpServletRequest request) {
    
            super(request);
        }
 
        @Override
        public ServletInputStream getInputStream() throws IOException {
    
            byte[] bytes = new byte[0];
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 
            return new ServletInputStream() {
    
                @Override
                public boolean isFinished() {
    
                    return byteArrayInputStream.read() == -1 ? true:false;
                }
 
                @Override
                public boolean isReady() {
    
                    return false;
                }
 
                @Override
                public void setReadListener(ReadListener readListener) {
    
 
                }
 
                @Override
                public int read() throws IOException {
    
                    return byteArrayInputStream.read();
                }
            };
        }
    }
 
    public static String readAsChars(HttpServletRequest request)
    {
    
 
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try
        {
    
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null)
            {
    
                sb.append(str);
            }
            br.close();
        }
        catch (IOException e)
        {
    
            e.printStackTrace();
        }
        finally
        {
    
            if (null != br)
            {
    
                try
                {
    
                    br.close();
                }
                catch (IOException e)
                {
    
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}

image-20200723121203864


Nacos The configuration can be refreshed automatically

版权声明
本文为[Wind is not greedy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231543467276.html