项目使用Spring Cloud做配置管理

首先把自己项目的spring配置,由properties转到yml(经本人测试,某些spring新特性只有yml才能更好的支持)。


配置文件相关用法,参照(国外网站打开慢,不能定位锚点的话,多刷新两次):

http://docs.spring.io/spring-boot/docs/1.3.8.RELEASE/reference/htmlsingle/#boot-features-external-config-loading-yaml



在项目跟目录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'

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Nacos作为配置中心的优势在于它具备统一管理、动态刷新和分布式配置的能力。下面是使用Spring Cloud Nacos作为配置中心的基本步骤: 1. 添加依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 配置Nacos服务地址:在项目的配置文件(application.yml或application.properties)中添加以下配置: ```yaml spring: cloud: nacos: config: server-addr: ${nacos.server-addr} ``` 其中`${nacos.server-addr}`是Nacos服务的地址,如`localhost:8848`。 3. 创建配置文件:在Nacos控制台创建配置文件,例如创建一个名为`example.properties`的配置文件,并添加一些配置项。 4. 使用配置:在Spring Boot应用程序中,通过使用`@Value`注解来注入配置项,例如: ```java @Value("${example.property}") private String exampleProperty; ``` 这样,`exampleProperty`变量将被自动注入为配置文件中`example.property`对应的值。 5. 动态刷新:当配置发生变化时,可以通过添加`@RefreshScope`注解来实现动态刷新,例如: ```java @RefreshScope @RestController public class ExampleController { // ... } ``` 这样,当配置发生变化时,注入的配置项将自动更新。 以上是使用Spring Cloud Nacos作为配置中心的基本步骤,希望能帮到你!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值