spring cloud config统一配置中心
来源:互联网 发布:coc亡灵升级数据 编辑:程序博客网 时间:2024/05/01 08:01
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个配置文件
2个文件的内容分别为:
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@EnableConfigServerpublic class ConfigServer { public static void main(String[] args) throws FileNotFoundException { SpringApplication.run(ConfigServer.class, args); }}
application.properties 配置文件
server.port=8888spring.cloud.config.server.git.uri=https://github.com/rejoiceFromJesus/config-repo.gitmanagement.security.enabled=falsespring.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=8080spring.cloud.config.uri=http://localhost:8888spring.application.name=configclient #代表当前服务的名称,会与configclient-{profile}.properties相对应spring.config.name=configserverspring.cloud.config.profile=dev //将会激活dev,从而访问http://localhost:8080/configclient/active,响应:hello i am dev
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 {@AutowiredConstantProperties 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;@SpringBootApplicationpublic 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
- spring cloud config统一配置中心
- 配置中心:Spring Cloud Config
- 使用Spring Cloud构建统一配置中心
- 使用Spring Cloud构建统一配置中心
- 使用Spring Cloud Config搭建配置中心
- 使用Spring Cloud Config搭建配置中心
- Spring Cloud Config 配置中心 教程
- spring cloud config server 配置中心
- Spring Cloud入门1-config配置中心
- SpringCloud: 分布式配置中心(Spring Cloud Config)
- 分布式配置中心Spring Cloud Config初窥
- Spring Cloud学习--配置中心(Config)
- 使用的Spring Cloud 构建的统一配置中心
- 非spring 工程使用 spring cloud config配置中心
- 统一配置服务——spring cloud config
- spring cloud config整合gitlab搭建分布式的配置中心
- spring cloud config分布式配置中心的高可用
- Spring Cloud入门2——配置中心Config
- Android笔记__PopupWindow底部弹出自定义菜单
- 为什么fragment使用hide()和show()而不使用replace()
- Android使用百度自动更新遇到的奇葩问题
- 子类与父类的初始化
- 一个很好的标注工具sketch
- spring cloud config统一配置中心
- Android notifyDatasetChanged失效
- SQL-(4) 约束
- 动态批量删除
- Mat 矩阵的叉乘与点乘
- C语言算法—生成数集的所有子集(类似建立树的回溯法)
- UVa_Maximum Product
- Java对象序列化
- 实例讲解C++中CopyFileEx函数的简单用法