标签:Delve local rip path 一个 tom 优先 XML tst
分布式系统中,往往拥有大量的服务应用,而每个应用程序都需要有对应的配置文件来协助完成服务环境初始化、运行。因此生产了大量的服务配置文件,Spring Cloud Config 可以实现配置文件的统一管理,它支持将配置服务放置在服务端的内存中(即服务端的本地内存),并且它也默认支持 git
,所以我们也可将配置文件放置在 git
仓库,以便于我们的访问和开发。
实现管理配置的方式有多种,例如使用 Eureka
或者 Spring Cloud Config
以及文件系统等,本次采用 Spring Cloud Config + Git 储存库的方式实现。
在本次的 Spring Cloud Config 组件中,总共有三个角色,一是 Config Server,一个是 Config Client,然后就是 Git 远程仓库。
新建一个 Spring Boot 项目
pom.xml
文件<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jojo</groupId>
<artifactId>configurationserver</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Config Server</name>
<description>Config Server demo for demo project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.jojo.configuration.server.ConfigServerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.jojo.configuration.server;
import ch.qos.logback.classic.joran.action.ConfigurationAction;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigurationAction.class, args);
}
}
resources
目录下创建 application.yml 配置文件,并做以下配置spring:
application:
name: Config Server
cloud:
config:
label: master
server:
git:
uri: http://ip:port/path
search-paths: repos
username: username
password: password
server:
port: 8888
配置文件说明:
spring.cloud.config.label
:配置的远程仓库的分支spring.cloud.config.server.git.uri
:配置 Git 仓库地址(Github、GitLab、码云...)spring.cloud.config.server.git.search-paths
:配置 Git 仓库中放置配置文件的目录spring.cloud.config.server.git.username
:配置访问 Git 仓库的用户名spring.cloud.config.server.git.password
:配置访问 Git 仓库的密码注意实现:
git.uri
需要在结尾加上 .git
,GitHub 仓库则不用。本次客户端简单得以一个 Spring Boot Admin 程序为例,本地配置文件只做简单得对接配置,将与 Spring Boot Admin 有关的配置放置在远程仓库。
pom.xml
文件<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jojo</groupId>
<artifactId>configurationclient</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Config Client</name>
<description>Config Client demo for demo project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.jojo.configuration.client.ConfigClientApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.jojo.configuration.client;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
resources
目录下创建 application.yml 配置文件,并做以下配置spring:
cloud:
config:
uri: uri
label: master
name: config-client
profile: dev
接下来对配置文件做说明:
spring.cloud.config.uri
:配置分布式配置中心的网址spring.cloud.config.label
:配置远程 Git 仓库的分支spring.cloud.config.name
:配置此服务对应的远程配置文件名称的前缀spring.cloud.config.profile
:配置文件的环境标识注意事项:
8888
,如果修改了默认端口,则客户端项目就不能在 application.yml
或 application.properties
中配置 spring.cloud.config.uri
,必须在 bootstrap.yml
或是 bootstrap.properites
中配置,原因是 bootstrap
开头的配置会被优先加载和配置。在远程 Git 中新建项目,并在项目中创建 repos
目录,在目录下创建 config-client-dev.yml
配置文件,并在文件中做以下配置:
spring:
application:
name: Config Client
server:
port: 8081
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
打开浏览器,访问 http://localhost:8081
,如果能看到下示界面,则表示配置成功!
【Spring Cloud】Spring Cloud Config 实现分布式配置中心
标签:Delve local rip path 一个 tom 优先 XML tst
原文地址:https://www.cnblogs.com/jojop/p/11490203.html