首先把自己项目的spring配置,由properties转到yml(经本人测试,某些spring新特性只有yml才能更好的支持)。
配置文件相关用法,参照(国外网站打开慢,不能定位锚点的话,多刷新两次):
在项目跟目录pom.xml中加入如下配置,与<dependencies>同级
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
在子模块(一般加在xxx-service中)pom.xml中加入如下依赖
<!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Spring Cloud End --> |
把项目中的配置,进行抽象,公共部门,抽到application.yml中,application.yml放在项目本地,application.ym中可以使用${}替换符,敏感信息分别放到各个application-环境.yml中,application-环境.yml文件交由运维放置到配置中心仓库中(bitbucket),例如:
application.yml中放置
zookeeper: servers: ${zookeeper.servers} |
application-development.yml中放置
zookeeper: servers: zk.dev.utouu.com:2181 |
application-test.yml中放置
zookeeper: servers: zk.test.utouu.com:2181 |
application-production.yml中放置
zookeeper: servers: s1.zk.utouu.com:2181,s2.zk.utouu.com:2181,s3.zk.utouu.com:2181 |
- 配置文件有很多灵活多样的配置方式,例如,本地也可以放application-环境.yml文件,则远程application-环境.yml文件中的参数会替换、合并本地application-环境.yml中的参数配置
交给运维的配置文件结构如下:production文件夹中的文件名为application-production.yml,test文件夹中的为application-test.yml。命名一定要按照如此规则来配置,否则拉取不到匹配的配置!
utsoft-demos文件夹为项目名,对应application.yml中的如下参数:
spring: application: name: utsoft-demos |
本地项目需要修改的地方,添加bootstrap.yml
bootstrap.yml配置如下:(暂定配置如下,后期有更改会另行通知)
spring: application: name: utsoft-demos cloud: config: enabled: true discovery: enabled: true service-id: UTOUU-CONFIG profile: ${spring.profiles.active} profiles: active: deveopment #不执行环境默认走开发 eureka: client: cache-refresh-executor-thread-pool-size: 2 # fetch-registry: false # register-with-eureka: false instance: lease-renewal-interval-in-seconds: 60 lease-expiration-duration-in-seconds: 90 secure-port: ${server.port:8083} --- spring: profiles: production cloud: config: username: client password: 'QWE123zxc!@#' eureka: client: serviceUrl: defaultZone: http://discovery.internal.utouu.com/eureka/ instance: virtual-host-name: http://demos.utsoft.cn --- spring: profiles: development cloud: config: username: client password: 'QWE123zxc' eureka: client: serviceUrl: defaultZone: http://discovery.dev.utouu.com/eureka/ instance: virtual-host-name: http://demos.dev.utsoft.cn --- spring: profiles: test cloud: config: username: client password: 'QWE123zxc' eureka: client: serviceUrl: defaultZone: http://discovery.test.utouu.com/eureka/ instance: virtual-host-name: http://demos.test.utsoft.cn |
demo项目新仓库地址
https://bitbucket.utsoft.cc/users/heng.lu_utsoft.cn/repos/utsoft-demos/browse
附:关于对敏感信息配置信息做网络传输级的加密
windows系统下载curl工具 curl.exe
根据不同的环境,执行指令获得密文(开发环境):
curl config.dev.utouu.com/encrypt -d 1.1.12 -u client Enter host password for user 'client': 7c82bd0f8cced8affb25737412d0e93b88b29684b977e6609b6d3838412b51ad |
各环境密码参照bootstrap.yml中的spring.cloud.config.password
得到密文后,修改环境配置文件application-development.yml
my: version: '{cipher}7c82bd0f8cced8affb25737412d0e93b88b29684b977e6609b6d3838412b51ad' |