背景:基于springcloud(九)继续
分布式配置中心服务端
1. 创建spring-cloud-config-server工程,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.zsx</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-config-server</name>
<description>Spring Cloud Config Server project</description>
<parent>
<groupId>com.zsx</groupId>
<artifactId>spring-cloud-customize-dependencies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-cloud-customize-dependencies/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zsx.ConfigServerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. application.yml配置文件
spring:
application.name: spring-cloud-config-server
cloud:
config:
# 配置仓库的分支
label: master
server:
git:
# 配置 Git 仓库地址
uri: https://github.com/
# 配置仓库路径(存放配置文件的目录)
search-paths: config-repository
# 访问 Git 仓库的账号
username:
# 访问 Git 仓库的密码
password:
server:
port: 8088
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
3. 引导类
package com.zsx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4. 提交config-client-dev.yml文件到git配置仓库路径下
server:
port: 8089
5. 启动引导类ConfigServerApplication主程序
6. 查看请求http://localhost:8088/config-client/dev/master
7. 查看请求http://localhost:8088/config-client-dev.yml
从结果可以看出:配置中心服务端从远程程序获取配置信息成功
8. 获取git上的资源信息遵循如下规则
http://ip:port/{application}/{profile}[/{label}]
http://ip:port/{application}-{profile}.yml
http://ip:port/{label}/{application}-{profile}.yml
http://ip:port/{application}-{profile}.properties
http://ip:port/{label}/{application}-{profile}.properties