版权声明:原创文章欢迎转载,不过要记得加出处哦 https://blog.csdn.net/wljk506/article/details/82730542
环境
JAVA 1.8
SpringBoot2 2.0.5.RELEASE
fox.风
SpringBoot2 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.foxwho</groupId>
<artifactId>demo-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
建立入口类
(IDEA已经帮我们建立好了)修改增加一些配置即可
package com.foxwho.demo.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class DemoConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoConfigServerApplication.class, args);
}
}
config server 配置文件仓库配置
假设配置文件仓库是 https://github.com/foxiswho/foxwho-config-repo
其中demo
文件夹下配置文件config-client-dev.yml
中的内容是
title: Test Name foxwho.com
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
resources 下 application.yml
application.yml 配置文件内容如下
server:
port: 8080
spring:
cloud:
config:
#仓库的分支
label: master
server:
default-application-name: @pom.artifactId@
git:
#git仓库地址
uri: https://github.com/foxiswho/foxwho-config-repo
#仓库路径
search-paths: demo
#访问git仓库的用户名
username:
#访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
password:
启动程序访问
浏览器访问
http://localhost:8080/title/dev
显示内容如下
说明配置服务中心可以从远程程序获取配置信息
SpringBoot2 Spring Cloud Config Client
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.foxwho</groupId>
<artifactId>demo-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
建立入口类
(IDEA已经帮我们建立好了)修改增加一些配置即可
package com.foxwho.demo.config.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(DemoConfigClientApplication.class, args);
}
// git配置文件里的key title
@Value("${title}")
String title;
@RequestMapping(value = "/showTitle")
public String showTitle() {
return title;
}
}
resources 下 增加 bootstrap.yml 配置文件
bootstrap.yml(bootstrap.properties)在application.yml(application.properties)之前加载
更多说明 请看 https://www.cnblogs.com/EasonJim/p/7589546.html
bootstrap.yml 配置文件内容如下
server:
port: 8090
spring:
application:
# 和git里的文件名对应
name: config-client
cloud:
config:
# 仓库分支
label: master
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境 和git里的文件名对应
profile: dev
# 指明配置服务中心的网址
uri: http://localhost:8080/
网址访问
http://localhost:8090/showTitle
输出
源码
https://github.com/foxiswho/spring-boot2-config-server-client-demo