美文网首页
springcloud-config-client的那些坑

springcloud-config-client的那些坑

作者: 王小胖v9 | 来源:发表于2020-03-23 14:52 被阅读0次

坑一:client启动类不要添加@EnableConfigServer,不要添加

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;"><dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies></pre>

否则会获取不到配置文件参数并报错

坑二:

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">#bootstrap.properties 加载优先于 application.properties 所以注册中心要写在bootstrap.properties中,否则无法加载git服务器配置文件
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/</pre>

Spring Cloud Config

在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。

首先在github上面创建了一个文件夹config-repo用来存放配置文件,为了模拟生产环境,我们创建以下两个配置文件:

image

server 端

1、添加依赖

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;"><dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies></pre>

只需要加入spring-cloud-config-server包引用既可。

2、配置文件

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">server.port=8088
spring.application.name=leisure-config

配置git仓库地址

spring.cloud.config.server.git.uri=https://gitee.com/leisure-w/config-repo/

配置仓库下相对地址,可以配置多个,用,分割

spring.cloud.config.server.git.search-paths=myconfigpath

配置仓库的分支

spring.cloud.config.label=master

访问git仓库的用户名

spring.cloud.config.server.git.username=xxxooo

访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写

spring.cloud.config.server.git.password=xxxooo

注册地址

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/</pre>

Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

3、启动类

启动类添加@EnableConfigServer,激活对配置中心的支持

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
}

}</pre>

到此server端相关配置已经完成

server段很简单不会有太多坑

4、测试

首先我们先要测试server端是否可以读取到github上面的配置信息,直接访问:http://localhost:8088/leisure-config/company

返回信息如下:

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;"><Environment><name>leisure-config</name><profiles><profiles>company</profiles></profiles><label/><version>bb1b25ea69a966f22905783a3a509f15f4b70685</version><state/><propertySources><propertySources><name>https://gitee.com/leisure-w/config-repo/leisure-config-company.properties</name><source><leisure>hello update company!</leisure></source></propertySources></propertySources></Environment></pre>

上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。

如果直接查看配置文件中的配置信息可访问:http://localhost:8088/leisure-config-company.properties,返回:"hello update company!"

修改配置文件leisure-config-company.properties中配置信息为:leisure.hello=hello company!,再次在浏览器访问http://localhost:8088/leisure-config-company.properties,返回:hello company!。说明server端会自动读取最新提交的内容

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

以leisure-config-company.properties为例子,它的application是leisure-config,profile是company。client会根据填写的参数来选择读取对应的配置。

client 端

主要展示如何在业务项目中去获取server端的配置信息

1、添加依赖

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;"><dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies></pre>

2、配置文件

需要配置两个配置文件,application.properties和bootstrap.properties

application.properties如下:

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">spring.application.name=spring-cloud-config-client
server.port=8002</pre>

bootstrap.properties如下:

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master</pre>

  • spring.application.name:对应{application}部分
  • spring.cloud.config.profile:对应{profile}部分
  • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
  • spring.cloud.config.uri:配置中心的具体地址
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。

特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

3、启动类

启动类不要添加@EnableConfigServer,激活对配置中心的支持

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">@SpringBootApplication
public class ConfigClientApplication {

public static void main(String[] args) {
    SpringApplication.run(ConfigClientApplication.class, args);
}

}</pre>

启动类只需要@SpringBootApplication注解就可以

4、web测试

使用@Value注解来获取server端参数的值

<pre code-lang="bash" class="juejin-editor-highlight" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.8em; position: relative; padding: 0.5em 1em; background: rgb(248, 248, 248); overflow: auto; border-radius: 2px;">@RestController
class HelloController {
@Value("${neo.hello}")
private String hello;

@RequestMapping("/hello")
public String from() {
    return this.hello;
}

}</pre>

相关文章

网友评论

      本文标题:springcloud-config-client的那些坑

      本文链接:https://www.haomeiwen.com/subject/ycswyhtx.html