5--SpringCloud搭建分布式配置中心

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行。   配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。当然他也提供本地化文件系统的存储方式,下面从这两方面介绍如何使用分布式配置来存储微服务应用多环境的配置内容。 构建Config Server   创建一个简单的springBoot项目 pom加上依赖 org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import   创建Spring Boot的程序主类,并添加@EnableConfigServer注解,开启Config Server @EnableConfigServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }   创建application.properties配置文件 spring.application.name=config-server server.port=7001 # git配置 #所在项目根目录 配置git仓库位置 spring.cloud.config.server.git.uri=https://gitee.com/cengjiang/springcloud_learning/ #所在地址目录 配置仓库路径下的相对搜索位置,可以配置多个 spring.cloud.config.server.git.searchPaths=5--SpringCloud--Config #如果是公开项目则不用写用户名密码 spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password   到这里,使用一个通过Spring Cloud Config实现,并使用git管理内容的配置中心已经完成了,启动该应用,成功后开始下面的内容。   Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。 服务器端验证   我的git仓库是在码云上创建的,里面存放在springCloud系列的示例代码。大家不想创建git仓库的也可以使用我这个地址。   在仓库地址https://gitee.com/cengjiang/springcloud_learning/下面创建了一个目录5--SpringCloud--Confi存放配置文件作为配置仓库,并根据不同环境新建了下面四个配置文件:     ghghspace-dev.properties     ghghspace-prod.properties     ghghspace-test.properties     ghghspace.properties   其中设置了一个from属性,为每个配置文件分别设置了不同的值,如:     from=git-default-1.0     from=git-dev-1.0     from=git-test-1.0     from=git-prod-1.0   为了测试版本控制,在master中,我们都加入1.0的后缀,同时创建一个config-label-test分支,并将各配置文件中的值用2.0作为后缀。   URL与配置文件的映射关系如下: application(应用)、profile(环境)、label(分支)       /{application}/{profile}[/{label}]     yml文件:       /{application}-{profile}.yml       /{application}-{profile}/{label}.yml     properties       /{application}-{profile}.properties       /{application}-{profile}/{label}.properties   访问:   {label}对应git上不同的分支,默认为master。   我们可以尝试构造不同的url来访问不同的配置内容,比如:要访问config-label-test分支,ghghspace应用的prod环境,可以通过这个url:http://localhost:7001/ghghspace/prod/config-label-test    微服务端映射配置   在完成并验证了配置服务中心之后,下面看看我们如何在微服务应用中获取配置信息。   创建一个简单的springBoot项目config-client pom加上依赖 org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import   创建启动主类 @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }      创建bootstrap.properties #对应前配置文件中的{application}部分 spring.application.name=ghghspace #对应前配置文件中的{profile}部分 spring.cloud.config.profile=dev #对应前配置文件的git分支 spring.cloud.config.label=master #配置中心的访问地址 spring.cloud.config.uri=http://localhost:7001/ server.port=7002   这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。      创建一个Rest Api来返回配置中心的from属性,具体如下: @RefreshScope @RestController public class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from() { return this.from; } }   通过@Value("${from}")绑定配置服务中配置的from属性。   启动该应用,并访问:http://localhost:7002/from ,我们就可以根据配置内容输出对应环境的from内容了。https://www.cnblogs.com/GH0522/p/9923916.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信