[microservice ~ original solution] spring cloud - distributed configuration component

Peach blossom key God 2022-06-24 08:03:34 阅读数:403

microserviceoriginalsolutionspringcloud

 Insert picture description here

Here is 【 Seconds understand · Cloud native 】, Pay attention to my learning cloud and don't get lost
If it helps you , Give the blogger a free praise to show encouragement
You are welcome to comment on the collection ️

Column introduction

【 Seconds understand · Cloud native 】 At present, it mainly updates micro services , Learn together and progress together .

Introduction to this issue

This paper mainly introduces Spring Cloud —— Distributed configuration component


In a distributed microservice system , Almost all services cannot run without
Profile support , These profiles are usually managed by individual services
The reason is , With properties or yml The format is saved in the class path of each micro service
Under diameter , for example application.properties or application.yml
etc. .
This management method of scattering configuration files in various services , Exist to
Next question :

  • Difficult to manage : Configuration files are scattered in various microservices , Difficult to manage The reason is .
  • Low security : Configuration follow source code saved in the code base , Easy to make Configuration leakage .
  • Poor timeliness : After the configuration in the microservice is modified , Service must be restarted , Otherwise, it will not take effect .
  • Obvious limitations : Can't support dynamic tuning , For example, log switch 、 work Can switch .

To solve these problems , We usually use the configuration center to configure
Unified management . There are many open source configuration centers on the market , For example, hundreds of
Degree Disconf、 Taobao's diamond、360 Of QConf、 Ctrip
Of Apollo And so on are all solutions to such problems .Spring Cloud It's also self-evident
Distributed configuration center , That's it Spring Cloud Config.

Spring Cloud Config

Spring Cloud Config By Spring Cloud Team developed items
Objective , It can provide centralized external services for various microservices in the microservice architecture
Department configuration support .
In short, it's ,Spring Cloud Config You can integrate various micro services
The configuration files of are centrally stored in an external storage warehouse or system ( example
Such as Git 、SVN etc. ) in , Unified management of configuration , To support each
The running of microservices .
Spring Cloud Config It consists of the following two parts :

  • Config Server: Also known as the distributed configuration center , It's a Microservice applications that run independently , It is used to connect the configuration warehouse and provide the client with configuration information 、 Access to encrypted and decrypted information mouth .
  • Config Client: It refers to the microservices in the microservice architecture , They use Config Server Manage the configuration , And from ConfigSever Get and load configuration information in .

Spring Cloud Config By default Git Store configuration information , therefore
Use Spirng Cloud Config The built configuration server is naturally supported
Maintain version management of microservice configuration . We can use Git client
Tools make it easy to manage and access configuration content . except Git Outside ,
Spring Cloud Config It also provides support for other storage methods ,
for example SVN、 Localized file system, etc .

Spring Cloud Config working principle

Spring Cloud Config The working principle is as follows .
 Insert picture description here
Spring Cloud Config The workflow is as follows :

  • The development or operation and maintenance personnel submit the configuration file to the remote Git Warehouse .
  • Config Server side ( Distributed configuration center ) Be responsible for connecting and configuring the warehouse
    Git, Also on Config The client exposes the interface to get the configuration .
  • Config Client pass Config The exposed interface of the server ,
    Pull the configuration in the configuration warehouse .
  • Config The client gets the configuration information , To support the operation of the service .

Spring Cloud Config Characteristics

Spring Cloud Config It has the following characteristics :

  • Spring Cloud Config from Spring Cloud Team development , can So it is Spring My son , Can and Spring Ecosystem of Seamless integration .
  • Spring Cloud Config Centralize the configuration files of all microservices Stored in an external storage warehouse or system ( for example Git) in , Unified management .
  • Spring Cloud Config The configuration center will configure to REST Interface Form is exposed to various micro services , To facilitate the access of various micro services .
  • Microservices can be through Spring Cloud Config Report to the configuration center Pull their own configuration information .
  • When the configuration changes , Microservices can sense the configuration without restarting Change of setting , And automatically obtain and apply the latest configuration .
  • An application may have multiple environments , For example, development (dev) Environmental Science 、 test (test) Environmental Science 、 production (prod) The environment, etc. , Developer Members can pass Spring Cloud Config For different environments Set for management , And it can ensure that the application is still completed after the environment is migrated The whole configuration supports its normal operation .

build Config Server side

  1. stay gitee Create a file named springcloud-config The warehouse of
    (Repository) And get the address of the warehouse
  2. Create submodules under the parent project ws-config And add Spring
    Cloud Config The server depends on
<dependency> <groupId>org.springframework.boot</group Id> <artifactId>spring-boot-starter- web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</grou pId> <artifactId>spring-cloud-starter- netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</grou pId> <artifactId>spring-cloud-config- server</artifactId> </dependency>
  1. To configure applycation.yml
server:
port: 1001
spring:
application:
name: spring-cloud-config-center # service name 
cloud:
config:
server:
git:# Code cloud (gitee) Address uri: https://gitee.com/nieps/springcloud- config.git
uri: https://gitee.com/nieps/springcloud-config.git# Warehouse, 
search-paths: - springcloud-config
force-pull: true # If Git Warehouse is open warehouse , It is not necessary to fill in Account name and password , If it is a private warehouse, it needs to be filled in # username: ******** # password: ******** # Branch name 
label: master
eureka:
client:
service-url:
defaultZone: http://admin:[email protected]:8761/eureka # Register the service to Eureka
  1. On the main boot class , Use @EnableConfigServer Annotation open
    Qi Spring Cloud Config Configure central functions
@SpringBootApplication
@EnableConfigServer
public class App {

public static void main( String[] args ){

SpringApplication.run(App.class,args);
}
}
  1. Create a new one config-dev.yml The file of , And upload it to
    springcloud-config Warehouse master Under branch , Such as
erp:
db:
username: root
password: 1357
  1. Start registry and configuration services , visit http://localhost:1
    001/master/config-dev.yml
     Insert picture description here
    Spring Cloud Config Specifies a set of configuration file access rules
    be , as follows :
/{
application}/{
profile}[/{
label}]
/{
application}-{
profile}.yml
/{
label}/{
application}-{
profile}.yml
/{
application}-{
profile}.properties
/{
label}/{
application}- {
profile}.properties

The parameters in the access rule are described as follows .
{application}: apply name , That is, the name of the configuration file ,
for example config-dev.
{profile}: Environment name , A project usually has development
(dev) edition 、 test (test) Environment version 、 production
(prod) Environment version , The configuration file is represented by
application-{profile}.yml In the form of , example
Such as application-dev.yml、applicationtest.yml、application-prod.yml etc. .
{label}:Git Branch name , The default is master Branch , When
When accessing the configuration file under the default Branch , This parameter can save
A little , The second access method .
Pass this set of rules , We directly check the configuration file on the browser
Visit .

build Config client

  1. Increase dependency under microservices
<!--Spring Cloud Config Client dependency --> <dependency> <groupId>org.springframework.cloud</grou pId> <artifactId>spring-cloud- starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</grou pId> <artifactId>spring-cloud-starter- bootstrap</artifactId> </dependency>

Spring Cloud The new version will default to Bootstrap Ban , Need to be
To put spring-cloud-starter-bootstrap Dependency is introduced into
engineering

  1. New configuration file bootstrap.yml Add the following configuration

bootstrap.yml It's system level , Loading priority is higher than
application.yml , Be responsible for loading configuration from outside and parsing

spring:
cloud:
config:
label: master # Branch name 
name: config # Profile name ,config- dev.yml Medium config 
profile: dev # Environment name config-dev.yml Medium dev# Don't forget to add http:// Otherwise you cannot read 
uri: http://localhost:1001 #Spring Cloud Config Server side ( Configuration center ) Address 

3. Read configuration file

@RestController
public class ConfigClientController {

@Value("${erp.db.username}")
private String username;
@Value("${erp.db.password}")
private String password;
@GetMapping(value = "/getConfig")
public String getConfig() {

return " database information :" + username + " <br/>password:" + password + "<br/>" ;
}
}

Access control class test http://localhost/getConfig
 Insert picture description here

 Insert picture description here

copyright:author[Peach blossom key God],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240219460942.html