最近在讲SpringCloud,下面将Spring Cloud中的分布式配置config的操作步骤记录如下:

第一步:在项目能够访问的git上新建一个工程(此处选择的gitee.com,可以选择其他的,也可以选择局域网的gitlab等),并在工程中新建文件夹,并新建对应的项目中需要使用的配置信息。(具体见https://gitee.com/migid/myconfig/)

第二步:编写config server,代码如下:

application.yml

spring:

application:

name: config_server

cloud:

config:

server:

git:

uri: https://gitee.com/migid/myconfig/

search-paths: configs

# username: 私有项目需要git的用户名

# password: 私有项目需要git的密码

label: master

server:

port: 8887

# 配置的本地配置中心

eureka:

client:

service-url:

defaultZone: http://192.168.52.44:8888/eureka/

 

 

MyconfigserverApplication.java

 

@EnableEurekaClient

@EnableConfigServer

@SpringBootApplication

public class MyconfigserverApplication {

public static void main(String[] args) {

SpringApplication.run(MyconfigserverApplication.class, args);

}

 

}

 

第三步:编写其他调用服务,即config-client

首先删除项目中的application.properties或yml文件。

添加bootstrap.yml文件:

spring:

application:

name: mycloudtest

cloud:

config:

label: master

profile: dev

discovery:

enabled: true

service-id: config_server

server:

port: 8982

 

#此处既要发现配置中心,又要在远程的配置文件中找到服务中心的地址并注册到服务中心

eureka:

client:

service-url:

defaultZone: http://192.168.52.44:8888/eureka/,${eureka_url}

 

剩下的代码就跟使用普通的方式继续编写就可以了,例如使用ribbon或者feign调用后端服务等。