spring cloud config统一配置中心
spring cloud config可以用来作分布式配置中心,本质上configServer会从git / svn / 本地目录上下载到本地环境,EnvironmentController提供了一些了接口给ConfigClient启动时调用,EnvironmentController会返回本地缓存的配置文件给ConfigClient,从而实现分布式配置
具体规则可参照官网或EnvironmentController接口,个人喜好参照源码,有时文档会存在出入或羞涩
服务端配置:
spring.cloud.config.server.git.uri=https://github.com/rejoiceFromJesus/config-repo.git //git仓库位置
management.security.enabled=false//关闭安全验证
spring.cloud.config.server.git.basedir=target/config //加载到ConfigServer项目所在的本地目录的位置
spring.cloud.config.server.git.search-paths=configclient //本地目录搜索路径
因此最终访问的绝对路径是spring.cloud.config.server.git.basedir+spring.cloud.config.server.git.search-paths,这样有个缺点是不能在Client客户端指定搜索的目录
1、创建git仓库,然后创建2个配置文件
image2个文件的内容分别为:
com.jiongyi.constant.active=dev
和
com.jiongyi.constant.active=test
tips:将会展现不同的环境加载不同的配置文件
2、创建父工程
pom.xml
<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.jiongyi.springboot</groupId>
<artifactId>jiongyi-springboot</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>spring-boot-config-server</module>
<module>spring-boot-configclient</module>
</modules>
<properties>
<main.basedir>${basedir}/..</main.basedir>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency> <!-- 加上这个才能辨认到log4j2.yml文件 -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-nosql</artifactId> <!-- 必需,否则报错 -->
<version>2.7</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>import</scope>
</dependency>
<!-- 配置提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- <resources> <resource> <directory>src/main/resources</directory> <excludes>
<exclude>*.properties</exclude> </excludes> </resource> </resources> -->
</build>
</project>
3、创建子工程configserver,代表服务端,负责加载配置文件到本地
pom.xml
<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>
<parent>
<groupId>com.jiongyi.springboot</groupId>
<artifactId>jiongyi-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
ConfigServer.java 启动类
package com.jiongyi.springboot.configserver;
import java.io.FileNotFoundException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) throws FileNotFoundException {
SpringApplication.run(ConfigServer.class, args);
}
}
application.properties 配置文件
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/rejoiceFromJesus/config-repo.git
management.security.enabled=false
spring.cloud.config.server.git.basedir=target/config #git的配置文件会加载到本地的目录
spring.cloud.config.uri=http://localhost:8888
4、创建子工程configclient客户端,配置文件将从configserver获取
pom.xml
<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>
<parent>
<groupId>com.jiongyi.springboot</groupId>
<artifactId>jiongyi-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-configclient</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.properties
server.port=8080
spring.cloud.config.uri=http://localhost:8888
spring.application.name=configclient #代表当前服务的名称,会与configclient-{profile}.properties相对应
spring.config.name=configserver
spring.cloud.config.profile=dev //将会激活dev,从而访问http://localhost:8080/configclient/active,响应:<span style="font-family:'Microsoft YaHei';font-size:14px;">hello i am dev</span>
ConstantProperties.java 用来读取配置文件的值,激活的profile不同,值不同
package com.jiongyi.springboot.configclient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix="com.jiongyi.constant")
public class ConstantProperties {
public String active;
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
}
HelloController.java
package com.jiongyi.springboot.configclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("configclient")
public class HelloController {
@Autowired
ConstantProperties constantProperties;
@RequestMapping(value="active",method={RequestMethod.GET})
public String hello(){
return "hello i am "+constantProperties.getActive();//返回当前激活的环境
}
}
ConfigClient.java 客户端启动类
package com.jiongyi.springboot.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient {
public static void main(String[] args) {
SpringApplication.run(ConfigClient.class, args);
}
}
测试:浏览器访问http://localhost:8080/configclient/active,若是激活dev,输出hello i am dev ,激活test,输出hello i am test