Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure Config in Spring Cloud

Shulou Source: shulou.com Published: 2022-06-02 06:58:54 11月27日 Update

This article is to share with you about how to configure Config in Spring Cloud, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. brief introduction

In the distributed system, due to the large number of services, in order to facilitate the unified management of service configuration files and real-time update, distributed configuration center components are needed. In Spring Cloud, there is a distributed configuration center component, spring cloud config, which allows configuration services to be placed in memory (that is, locally) of configuration services, as well as in remote Git repositories. In the spring cloud config component, there are two roles, one is config server, and the other is config client.

Second, construct Config Server

Create a spring-boot project named config-server with pom.xml:

Org.springframework.cloudspring-cloud-config-server

Org.springframework.cloudspring-cloud-starter-eureka

Add @ EnableConfigServer annotation to the entry Application class of the program to enable the configuration server. The code is as follows:

@ SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {public static void main (String [] args) {SpringApplication.run (ConfigServerApplication.class, args);}}

The following needs to be configured in the program's configuration file application.properties file:

Spring.application.name=config-serverserver.port=8888spring.cloud.config.server.git.uri= https://github.com/forezp/SpringcloudConfig/spring.cloud.config.server.git.searchPaths=respospring.cloud.config.label=masterspring.cloud.config.server.git.username=your usernamespring.cloud.config.server.git.password=your password

Spring.cloud.config.server.git.uri: configure git warehouse address

Spring.cloud.config.server.git.searchPaths: configure the warehouse path

Spring.cloud.config.label: branch of the configuration repository

Spring.cloud.config.server.git.username: the user name to access the git repository

Spring.cloud.config.server.git.password: user password to access the git repository

If the Git warehouse is a public warehouse, you do not need to enter a user name and password. If it is a private warehouse, this example is a public warehouse, so you can rest assured to use it.

There is a file in the remote warehouse https://github.com/forezp/SpringcloudConfig/ that has an attribute in the config-client-dev.properties file:

Foo = foo version 3

Launcher: accessing http://localhost:8888/foo/dev

{"name": "foo", "profiles": ["dev"], "label": "master", "version": "792ffc77c03f4b138d28e89b576900ac5e01a44b", "state": null, "propertySources": []}

Prove that the configuration service center can obtain configuration information from remote programs.

The mapping of http request address and resource file is as follows:

/ {application} / {profile} [/ {label}]

/ {application}-{profile} .yml

/ {label} / {application}-{profile} .yml

/ {application}-{profile} .properties

/ {label} / {application}-{profile} .properties

Third, build a config client

Recreate a springboot project named config-client with its pom file:

Org.springframework.cloudspring-cloud-starter-config

Org.springframework.bootspring-boot-starter-web

Its configuration file:

Spring.application.name=config-clientspring.cloud.config.label=masterspring.cloud.config.profile=devspring.cloud.config.uri= http://localhost:8888/server.port=8881

Spring.cloud.config.label indicates the branch of the remote warehouse

Spring.cloud.config.profile

Dev development environment configuration file

Test test environment

Pro formal environment

Spring.cloud.config.uri= http://localhost:8888/ indicates the URL of the configuration service center.

The entry class of the program, which writes an API interface "/ hi", returns the value of the foo variable read from the configuration center, as follows:

SpringBootApplication@RestControllerpublic class ConfigClientApplication {public static void main (String [] args) {SpringApplication.run (ConfigClientApplication.class, args);} @ Value ("${foo}") String foo; @ RequestMapping (value = "/ hi") public String hi () {return foo;}}

Open the URL to visit: http://localhost:8881/hi, the web page shows:

Foo version 3

This shows that config-client gets the properties of foo from config-server, while config-server reads them from git repository, as shown in the figure:

The above is how to configure Config in Spring Cloud. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Configuration warehouses files services programs distributed environment users components code entries branches addresses passwords properties more service centers user names knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker vpn Shulou Information Apple