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

SpringCloud distributed microservice cloud architecture part 6: distributed configuration Center (SpringCloud Config)

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

Share

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

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 supports the configuration service to be placed in memory (that is, locally) of the configuration service.

It is also supported in remote Git repositories. To understand the springcloud architecture, you can add: three, three, six, two, four, seven, two, nine, in the springcloud config component

There are two roles, one is config server, the other is config client.

Second, construct Config Server

Parent maven project omitted, parent pom file:

4.0.0 com.forezp sc-f-chapter6 0.0.1-SNAPSHOT pom config-server config-client sc-f-chapter6 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 Finchley.RELEASE Org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Org.springframework.boot spring-boot-maven-plugin

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

4.0.0 com.forezp config-server 0.0.1-SNAPSHOT jar config-server Demo project for Spring Boot com.forezp sc-f-chapter6 0.0.1-SNAPSHOT org.springframework.boot Spring-boot-starter-web org.springframework.cloud spring-cloud-config-server org.springframework. Boot spring-boot-maven-plugin

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=spring.cloud.config.server.git.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 repository 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": []}

The Ming 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:

4.0.0 com.forezp config-client 0.0.1-SNAPSHOT jar config-client Demo project for Spring Boot com.forezp sc-f-chapter6 0.0.1-SNAPSHOT org.springframework.boot Spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework. Boot spring-boot-maven-plugin

Its configuration file bootstrap.properties:

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:

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