Peach blossom key God 2022-06-24 08:03:34 阅读数:403
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 ️
【 Seconds understand · Cloud native 】 At present, it mainly updates micro services , Learn together and progress together .
This paper mainly introduces Spring Cloud —— Distributed configuration component
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 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 :
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 The working principle is as follows .
Spring Cloud Config The workflow is as follows :
Spring Cloud Config It has the following characteristics :
<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>
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
@SpringBootApplication
@EnableConfigServer
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class,args);
}
}
erp:
db:
username: root
password: 1357
/{
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 .
<!--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
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
copyright:author[Peach blossom key God],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240219460942.html