石碣镇网站建设/福州seo网站排名
一、前言
在前面的博客中,小编依次向大家介绍了配置中心Eureka,负载均衡机制Ribbon和Feign,系统容错机制Hystrix。这样基本上的分布式开发是没有问题了,在使用的时候也是非常的痛快吧。
但是现在我们的架构是微服务架构,所以我们将会有很多的微服务,每个微服务都有各自的配置文件,这样当我们需要对一些服务进行批量的修改的时候我们可能就会要挨个的依次修改,这样的工作量也是很大的。
针对这个情况,我们就想把所有的服务的 配置文件全部抽离出来然后统一管理。更改后,通过消息总线动态刷新项目。这篇博客,小编先向大家介绍一下SpringCloud Config。
二、什么是Spring Cloud Config?
springcloud config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持。
分为服务端和客户端:
-
服务端,也叫做分布式配置中心,是一个独立的微服务,用于连接所有客户端和配置仓库,提供配置信息、加密解密等访问接口。
-
客户端,可以是各个微服务,通过指定配置中心,在启动的时候,通过配置中心读取对应的配置文件。
配置中心默认是git,也支持svn,本地化文件系统。
三、实战
3.1 准备
根据上一篇博客搭建好的框架,我们在这个基础上进行修改,您可以在这里得到代码。
https://github.com/AresKingCarry/SpringCloudDemo
3.2 新建立config 服务端
建立新springboot项目config:
3.3 添加依赖
<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>
3.4 启动类添加注解
启动类添加@EnableConfigServer
注解,开启服务端配置中心的功能:
package com.wl.config;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {public static void main(String[] args) {SpringApplication.run(ConfigApplication.class, args);}
}
3.5 修改配置文件
配置Git配置文件:
server:port: 8888
spring:application:name: configcloud:config:server:git:uri: https://github.com/AresKingCarry/springcloudconfigserver.gitusername: AresKingCarrypassword: 15930**********elabel: master
spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。
在https://github.com/AresKingCarry/springcloudconfigserver.git的master上,指明了不同运行环境的文件,文件内容中写明了内容。
config-client.yml
config-client-dev.yml 开发环境配置文件
config-client-test.yml 测试环境配置文件
config-client-prod.yml 生产环境配置文件
http请求地址和资源文件映射如下:
• /{application}/{profile}[/{label}]
• /{application}-{profile}.yml
• /{label}/{application}-{profile}.yml
• /{application}-{profile}.properties
• /{label}/{application}-{profile}.properties
另附配置svn:
spring:profiles: devcloud:config:server:svn:uri: https://1**.**1.**.7:10096/svn/***/sjpt/SMF/branches/****/config-repousername: cloudpassword: passworddefault-label: dev
3.6 建立客户端
3.7 添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>
3.8 修改配置文件 bootstrap.yml
注意,这里要从新建立配置文件,命名为bootstrap.yml。
server:port: 6001
spring:application:name: config-clientcloud:config:label: masterprofile: devuri: http://localhost:8888
配置文件中:
• spring.cloud.config.label 指明远程仓库的分支
• spring.cloud.config.profile○ dev开发环境配置文件○ test测试环境○ pro正式环境
• spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。
为什么要用 bootstrap.yml 而不用 application.yml?
springboot的配置文件加载顺序,对于本应用外的jar包文件加载会优先于应用jar包内的配置内容,而通过 bootstrap.yml 对 config-server的配置,使得该应用会从config-server中获取一些外部的配置文件这些信息的优先级比本地的高,从而实现外部化配置。
3.9 配置controller访问类
package com.wl.configclient.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created by Ares on 2018/4/18.*/
@RestController
@RefreshScope
public class ConfigController {@Value("${shoujin}")private String shoujin;@GetMapping("/getInfo")public String getInfo(){return shoujin;}
}
这样我们就可以通过spring的@Value注解访问到配置文件中的参数的值了。
四、小结
可以说本例中只是一个简单的demo, 虽然配置中心中的值是抽象出来的,把所有的配置文件的值都抽象出来了,可以统一的管理,这样就对系统有了一个限制,比如,使用Git就必须要求所有的节点联网,使用svn就要求在局域网内,所以根据不同的环境进行不同的选择吧。