Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring cloud config Integration gitlab how to build a distributed configuration Center

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to build a distributed configuration center for spring cloud config integration gitlab". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "spring cloud config integration gitlab how to build a distributed configuration center" can help you solve your doubts.

Premise: create a new configuration file configserver-dev.properties under the project in gitlab

1. Configure Server

1. Add dependencies

Org.springframework.cloud spring-cloud-config-server

2. Enable support in the Application main class

@ EnableConfigServer

3. Configure the application.yml file

Server: port: 8888 spring: application: name: config cloud: config: server: git: uri: https://gitlab.xxx.com/xxxxx/xxxxx.git # configure the address of the gitlab repository. Note that the relative address under the address of search-paths: / config-repo # gitlab warehouse must end with .git. Multiple addresses can be configured, used, and divided. Username: account of your username # gitlab warehouse password: your password # gitlab warehouse password

Note: if the configuration file is placed in the root directory of the Git repository, there is no need to use the searchPaths parameter. In this case, the configuration file is in the config-repo directory, so use the searchPaths parameter to prompt the Config server to search for the config-repo subdirectory.

4. Start server and type http://localhost:8888/configserver/dev/master in the browser

{"name": "configserver", "profiles": ["dev"], "label": "master", "version": "073cda9ce85a3eed00e406f4ebcc4651ee4d9b19", "state": null, "propertySources": [{"name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties"," source ": {" name ":" chhliuxyh " "hello": "i'm the king of the worldviews!", "profile": "profile-default"}}]}

You can see that the server side can read the configuration file from the gitlab. You can access resources on gitlab in the following form

/ {application} / {profile} [/ {label}] / {application}-{profile} .yml / {label} / {application}-{profile} .yml / {application}-{profile} .properties / {label} / {application}-{profile} .properties

For example, enter: http://localhost:8888/configserver-dev.yml in the browser, and the result is as follows:

Hello: i'm the king of the world!!! Name: chhliuxyh profile: profile-default

Second, configure the client

1. Add pom dependencies

Org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web

2. Configure the bootstrap.yml file

Note: the configuration file here needs to be placed in the bootstrap.properties or bootstrap.yml file, because the relevant configuration of config precedes application.properties, and bootstrap.properties loads before application.properties.

Server: port: 8889 spring: application: name: configserver # must be consistent with the prefix of the configuration file. For example, if our configuration file name here is configserver-dev.properties, we need to configure it as configserver cloud: config: uri: http://localhost:8888/ configure spring cloud config server url profile: dev # specify profile label: master # specify the branch of gitlab repository

3. Verify the client

Add a Controller to the client

Package com.chhliu.springcloud.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController @ SpringBootApplication @ RestController @ RefreshScope / / Note @ RefreshScope instructs the Config client to refresh the injected attribute value public class SpringcloudConfigClientApplication {public static void main (String [] args) {SpringApplication.run (SpringcloudConfigClientApplication.class, args);} @ Value ("${hello}") / / read the attribute in the gitlab configuration file when the server configuration changes. If we read the value, the client is the private String profile of OK. @ GetMapping ("/ hello") public String hello () {return this.profile;}}

Visit: http://localhost:8889/hello in the browser, and the result is as follows:

I'm the king of the world!!!

It means that the client can get the value from the server.

Third, dynamic refresh

Spring Cloud Config managed configuration can be updated without restarting the client

1. Update the attribute values corresponding to hello in the configserver-dev.properties configuration file in the gitlab repository

2. Visit http://localhost:8888/configserver/dev/master and find that the content on server has been updated.

3. Send a POST request http://localhost:8889/refresh to the Conf client and return 200 OK. When you visit http://localhost:8889/hello again, you can see that the property values read have been dynamically updated without restarting the client service.

PS: to achieve dynamic refresh, you need to add the following starter to the pom file

Org.springframework.boot spring-boot-starter-actuator has read this article, "spring cloud config Integration gitlab how to build a distributed configuration center" article has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report