研究spring cloud天然少不了配置中心,项目中使用的是apollo,想研究下spring cloud config基于git的。html
一、首先在git上面建立你的配置文件信息,我用本身搭建的gitlab来搞下,建立了两个配置文件,文件内容很简单。git
testconfig: my is devweb
testconfig: my is prodspring
二、先添加引用在说,个人项目是多模块项目,在根pom下添加以下引用。bootstrap
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
三、在注册中心的项目添加pom引用app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
四、在注册中心的项目application.yml配置文件中添加配置信息,并在启动类添加注解@EnableConfigServer,注册中心就完事了。spring-boot
server: port: 8081 spring: application: name: spring-cloud-config-server cloud: config: server: git: #git配置文件的地址 uri: http://10.138.61.43:8081/root/spring-cloud.git username: root password: 12345678
运行起来能够直接访问 http://localhost:8081/spring-cloud-config/dev 来查看效果。仓库中的配置文件会被转换成web接口,访问能够参照如下的规则:gitlab
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.propertiespost
在我这里,个人配置文件是spring-cloud-config-dev.yml,application就是spring-cloud-config,profile就是dev,label暂时不知道啥玩意。网站
五、而后咱们开始搞客户端,来使用注册中心的配置信息。同样先添加引用
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
六、在application.yml配置文件和bootstrap.yml文件中分别加入下面两段配置信息
server: port: 8082 spring: application: name: spring-cloud-config-client
spring: cloud: config: name: spring-cloud-config profile: dev #配置中心的具体地址 uri: http://localhost:8081/ #对应git的分支,若是配置中心使用的是本地存储,则该参数无用 label: master
七、而后直接在使用的地方用注解使用便可。
@RestController public class HomeController { @Value("${testconfig}") private String testconfig; @RequestMapping("/index") public String index() { return "这里是client-test:" + testconfig; } }
而后运行客户端就能够看到效果了。总的来说不复杂比较简单的。可是手工修改配置文件后,你会发现注册中心的信息更新了,可是在客户端一直没有更新。接下来就搞下让他能及时更新下。
一、首先在客户端添加pom引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
二、在使用配置文件的类添加注解@RefreshScope
//使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。 @RefreshScope @RestController public class HomeController { @Value("${testconfig}") private String testconfig; @RequestMapping("/index") public String index() { return "这里是client-test:" + testconfig; } }
三、在application.yml文件中添加配置信息。不添加配置信息在接下来更新操做会出现404,注意....
management: endpoints: web: exposure: include: "*"
四、而后用post请求调用 http://localhost:8082/actuator/refresh 地址刷新下,在刷新网站就能够实现了。
还有另一种方法实现自动刷新的方式:经过git的WebHooks来动态刷新。其实就是侦听提交事件,而后执行相应的操做便可。
参考地址:
http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html