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 distributed microservice: Spring Cloud Config

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

Share

Shulou(Shulou.com)06/02 Report--

Spring Cloud Config is a brand new project created by the Spring Cloud team to apply infrastructure and micro services in distributed systems.

Provide external configuration support for centralization, which is divided into two parts: the server and the client. Among them, the server is also called the distributed configuration center, which is a unique

The micro service application is used to connect to the configuration repository and provide the client with access to configuration information, encryption / decryption information, etc., while the client is micro.

Each micro-service application or infrastructure in the service architecture manages the business-related configuration of application resources through a designated configuration center, and in the

Get and load configuration information from the configuration center at startup. Spring Cloud Config implements the configuration of environment variables and properties in both the server and the client.

, so it can be used in applications running in any other language in addition to applications built by Spring Due to

The configuration center implemented by Spring Cloud Config uses Git to store configuration information by default, so the configuration server built by Spring Cloud Config is used.

Version management of configuration information for micro-service applications is naturally supported, and configuration content can be easily managed and accessed through Git client tools. Of course, it also mentions

Provide support for other storage methods, such as SVN warehouse, localized file system.

In this article, we will learn how to build a distributed configuration center based on Git storage and transform the client so that it can obtain from the configuration center

Configure the information and bind it to the entire process in the code.

Prepare to configure the warehouse

Prepare a git repository, which can be created on either Cloud or Github.

Assuming that we read the application of the configuration center named config-client, then we can use the default configuration file config-client.yml for the project in the git repository:

Info:profile: default

To demonstrate loading configurations for different environments, we can create another configuration file config-client-dev.yml for the dev environment in the git repository:

Info: profile: dev

Build a configuration center

Building a distributed configuration center through Spring Cloud Config is very simple and requires only three steps:

Create a basic Spring Boot project, named config-server-git, and introduce the following dependencies in pom.xml (omitting parent and

DependencyManagement section):

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

Create the main program class of Spring Boot and add the @ EnableConfigServer annotation to enable the server function of Spring Cloud Config.

@ EnableConfigServer@SpringBootApplicationpublic class Application {public static void main (String [] args) {new SpringApplicationBuilder (Application.class) .web (true) .run (args);}}

Add basic information about the configuration service and information about the Git repository in application.yml, such as:

Spring application: name: config-server cloud: config: server: git: uri: http://git.oschina.net/didispace/config-repo-demo/server: port: 1201

At this point, you are done using a distributed configuration center that is implemented through Spring Cloud Config and manages the configuration content using Git. We can start the application first to make sure there are no errors, and then try the following.

If we need permission to access our Git repository, we can do this by configuring the following two properties: spring.cloud.config.server.git.username: the user name to access the Git repository spring.cloud.config.server.git.password: the user password to access the Git repository

After completing these preparations, we can access our configuration directly through tools such as browser, POSTMAN, or CURL. To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59. The mapping between the URL that accesses the configuration information and the configuration file is as follows:

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

The above url maps the configuration files corresponding to {application}-{profile} .properties, where {label} corresponds to different branches on the Git and defaults to master. We can try to construct different url to access different configuration contents. For example, to access the master branch and the dev environment of the config-client application, you can access the url: http://localhost:1201/config-client/dev/master and get the following return:

{"name": "config-client", "profiles": ["dev"], "label": "master", "version": null, "state": null, "propertySources": [{"name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml"," "source": {"info.profile": "dev"}, {"name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml"," source ": {" info.profile ":" default "}]}

We can see that the Json returns the application name: config-client, environment name: dev, branch name: master, and the configuration contents of the default environment and the dev environment.

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