springboot配置中心
来源:互联网 发布:淘宝代理货源要钱吗 编辑:程序博客网 时间:2024/06/05 06:08
Spring Boot有一个非常重要的改变就是简化了配置,使用application.properties文件定义了很多默认配置(参考之前的文章:http://www.jianshu.com/p/860addd7865d)。
但是配置文件分开管理来还是比较麻烦的,而且环境越多配置约容易出问题。Spring Cloud提供了一种统一配置的方案:Spring Cloud Config Server。
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分。
Server端配置
Spring Cloud Config Server本质上也是一个Spring Boot的web项目,只需要添加对应的parent,然后加入相关的依赖就可以启动这个工程了。
Maven的pom.xml中需要添加以下内容:
<parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.BUILD-SNAPSHOT</version> <relativePath /> <!-- lookup parent from repository --></parent><dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build><!-- repositories also needed for snapshots and milestones --><repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>http://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>http://repo.spring.io/libs-release-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository></repositories><pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>http://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository></pluginRepositories>
config server的resource目录下的application.properties:
server.port=8888spring.cloud.config.server.git.uri=file://Users/whthomas/config-repospring.application.name=configserverspring.cloud.config.uri=http://localhost:8888
启动项目的代码:
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
其实和一般的SpringBoot项目启动没有什么区别,只是多了一个@EnableConfigServer注解。
配置环境仓库( Environment Repository )
上面的application.properties中有一个
spring.cloud.config.server.git.uri=file://Users/whthomas/config-repo
这个配置指的项目配置仓库的位置,这个位置可以是:git文件夹、svn文件夹或者github项目位置,任何能访问到文件的地方。
环境仓库(例子中的文件夹中)中提供环境配置对象配资源给Config Server发布给各个consumer使用。
环境资源的命名规则由以下的三个参数确定:
{application}映射到Config客户端的spring.application.name属性{profile}映射到Config客户端的spring.profiles.active属性,可以用来区分环境,比如dev,test,produce等等{label}映射到Git服务器的commit id,分支名称或者tag,默认值为master
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
举个栗子:
我在配置中心的目录下放置文件:
cloud-config-rd.propertiescloud-config-dev.propertiescloud-config-test.propertiescloud-config-test.properties
以cloud-config-rd.properties为例子,它的application是cloud-config,profile是rd.client会根据填写的参数来选择读取对应的配置。
那么接下去来看client端的处理。
Client端配置
创建一个普通的SpringBoot项目,pom.xml中加入Spring Cloud的配置。
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RC2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>pom.xml中的dependencies节点下添加<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency>
resource目录下的application.properties添加这样几个配置:
# 配置中心服务的地址spring.cloud.config.uri=localhost:8888# 要读取的配置文件application属性spring.cloud.config.name=cloud-config# 要读取的配置文件profile属性,默认是devspring.cloud.config.profile=${config.profile:dev}
以上的几个配置也可以在命令行启动jar时填写。
以上配置完成之后,在远端配置中心的对应的配置就会加载到项目中,和本地使用application.properties配置中添加配置是几乎一样的效果,使用@Value注解的配置也可以顺利读取到对应的配置。
- springboot配置中心
- SpringBoot -- 配置中心服务/webhook
- SpringBoot应用之配置中心
- SpringBoot之配置中心服务化和高可用
- SpringBoot+ZooKeeper+ZKUI+Drools 实现应用配置中心及业务规则动态加载
- springboot填坑之 -- spring cloud基于ip的discovery服务注册中心配置
- springboot填坑之 -- spring cloud基于ip的discovery服务注册中心配置
- 配置中心
- springboot配置
- springboot配置
- SpringBoot配置
- 【SpringBoot】SpringBoot核心-基本配置
- 【SpringBoot】SpringBoot核心-外部配置
- SpringBoot-01SpringBoot配置FastJson
- 工作中心相关配置
- 统一配置中心
- diamond配置中心部署
- springCloud配置中心相关
- 使用MediaCodec播放视频
- 正则表达式
- 153. Find Minimum in Rotated Sorted Array
- python round函数 竟然把5舍去了
- AS3.0 性能分析工具Profier使用总结
- springboot配置中心
- Ubuntu 16.04.1 + NVIDIA driver + cuda 8.0 安装
- solr 中文分词器
- Android启动模式之singleInstance
- 详解Java8特性之Stream API并行流
- React6 实战项目2
- validate错误提示放在input后(即使带有单位)以及判断日期大小
- Mac:TexStudio ctex.sty not found
- 【数据库-MySql】清空所有表格的所有数据