spring cloud学习之五: SpringCloud Config

Spring Cloud Config 是为分布式、微服务系统的配置 提供集中化外部配置管理。
主要分为服务端客户端两部分:
服务端称为分布式配置中心,是一个独立的服务应用,用来连接配置仓库,并为客户端提供获取配置信息、加密/解密信息等访问接口
客户端则是微服务架构中的各个应用服务,通过指定的配置中心,在启动时来获取相关配置
目前支持gitsvn两种方式来存储配置文件。其中默认为git

现在已对spring cloud config已有基础的了解,下面就开始进行搭建spring cloud config。

1、spring cloud config–服务端

  1. 创建一个空的spring boot 工程,在此就不进行展示,在之前的文章中已有创建过程。
  2. 添加config 服务端依赖(pom.xml)
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
			<version>1.3.2.RELEASE</version>
		</dependency>

3.启动类添加 @EnableConfigServer 注解

4.在配置文件中(application.properties)添加git/svn配置

git配置

#仓库位置
spring.cloud.config.server.git.uri=https://github.com/xingwl2018/springcloudconfig/
#配置仓库路径下的相对搜索位置,可以配多个,如果默认仓库根目录可注掉
spring.cloud.config.server.git.search-paths=repo/dev
# 访问git仓库的用户名
spring.cloud.config.server.git.username=
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=
spring.cloud.config.label=master

访问仓库中的配置文件:配置地址url与配置文件关系

  • /{application}/{profile}/[/{label}]
  • /{application-profile}.yml
  • /{application-profile}.properties
  • /{label}/{application-profile}.yml
  • /{label}/{application-profile}.properties
    访问方式:配置中心的 ip:port/{application-profile}.properties

svn配置

  • 添加svn依赖(pom.xml)
<!--需要额外添加svn依赖-->
		<!-- https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit -->
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
			<version>1.8.10</version>
		</dependency>
  • 修改配置文件(application.properties)
##配置SVN仓库
spring.cloud.config.enabled=true
#需要显示声明subversion,不可缺少
spring.profiles.active=subversion
#svn仓库位置
spring.cloud.config.server.svn.uri=svn://192.168.8.129:15003/springclouod
#svn用户名密码
spring.cloud.config.server.svn.username=
spring.cloud.config.server.svn.password=
#加载分支(默认读取位置)
spring.cloud.config.server.default-label=config

注意项:

  • 当使用默认的git仓库时,config-服务端可以使用多配置文件的方式
    在这里插入图片描述
  • 当使用svn作为配置文件存储时就不能使用多配置文件的方式了,只能用 application.properties 或者application.yml 配置文件了,并且一定要加
#需要显示声明subversion
spring.profiles.active=subversion

否则会报错:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-04-11 20:48:01.843 ERROR 17384 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$7ce4c7cf]: Constructor threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration$DefaultRepositoryConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: You need to configure a uri for the git repository
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1197) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:764) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124) ~[spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.20.RELEASE.jar:1.5.20.RELEASE]
	at com.ganinfo.ConfigServerApplication.main(ConfigServerApplication.java:14) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.20.RELEASE.jar:1.5.20.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$7ce4c7cf]: Constructor threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration$DefaultRepositoryConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: You need to configure a uri for the git repository
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271) ~[spring-beans-4.3.23.RELEASE.jar:4.3.23.RELEASE]
	... 23 common frames omitted

大概意思就是检测不到git配置

2、spring cloud config–客户端

  1. 添加config 依赖(pom.xml)
<!--读取配置文件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
  1. 配置文件修改及添加配置
    配置文件用(bootstrap.properties)
    原因:应用启动时默认会从工程的classpath中加载配置信息,并启动应用。当采用外部配置文件的时候,就需要先加载配置,在进行服务初始化,而 bootstrap.properties 的加载优先级高于应用jar包内的配置信息。若连接失败或尝试连接 http://localhost:8888/
#服务名称 对应配置文件规则中的{application}
spring.application.name=datamanagement
#配置中心 config-服务的地址
spring.cloud.config.uri=http://192.168.2.241:7081/
#分支  对应配置文件规则中的 {label}
spring.cloud.config.label=dev
#对应配置文件规则中的{profile}
spring.cloud.config.profile=dev

3、注册到服务中心

  1. 添加Eureka客户端配置,不懂请看之前关于Eureka的文章。
  2. 添加Eureka配置在配置文件中
#配置注册eureka地址
eureka.client.service-url.defaultZone=http://192.168.2.187:7081/eureka/
  1. 客户端配置修改
#从远程获取配置文件,开启
spring.cloud.config.discovery.enabled=true
#配置服务中心 注册的服务名
spring.cloud.config.discovery.service-id=config-server
#分支  对应配置文件规则中的 {label}
spring.cloud.config.label=dev
#对应配置文件规则中的{profile}
spring.cloud.config.profile=dev

4、失败快速响应与重试

目的:当应用服务相对复杂时,该工程在连接config-server服务前花费的时间就会长,
为了避免因为config-server的连接失败而重新启动造成的时间浪费,可以优先判断config-server服务是否正常只需要在配置文件中添加

#允许失败快速响应
spring.cloud.config.fail-fast=true

为了避免因网络原因造成服务启动失败,可以重复连接测试config-server服务
需要添加依赖

<!--连接重试-->
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

添加之后会默认进行6次连接尝试,可以通过配置进行参数调整

#允许重试,需要添加spring-retry和spirng-aop依赖,下面配置的都是和默认值一样
#默认连接次数
spring.cloud.config.retry.max-attempts=6
#默认连接间隔时长
spring.cloud.config.retry.initial-interval=1000
#默认间隔乘数,下一次尝试的间隔=间隔时长*乘数
spring.cloud.config.retry.multiplier=1.1
#默认最大间隔时间
spring.cloud.config.retry.max-interval=2000

5、动态刷新配置

需要添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

方式:
post请求:
客户端的 http://ip:port/工程名/refresh

目前只是简单的配置服务应用,本人在学习中会不断对该文章进行更新。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值