springcloud_config教程(7)--合并config-server到config-client

x33g5p2x  于2021-12-20 转载在 其他  
字(4.7k)|赞(0)|评价(0)|浏览(251)

  之前的工程中,有eureka-server,config-server,config-client,这一套配置系统显得很冗长,增加了维护服务的成本,可以将config-server嵌入到config-client中,且无需使用eureka-server。现在开始改造config-client。

  1.在之前的config-client工程中,引入spring-cloud-config-server依赖,完整pom.xml如下:

|

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.lx.springCloudLearn</groupId>
   <artifactId>config-client</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>config-client</name>
   <description>Demo project for Spring Boot</description>
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.8.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</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>
      <!--引入此依赖才能调用refresh接口,更新配置-->
 <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <!--消息总线-->
 <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-bus-amqp</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR6</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

   <repositories>
      <repository>
         <id>spring-milestones</id>
         <name>Spring Milestones</name>
         <url>https://repo.spring.io/milestone</url>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
      </repository>
   </repositories>
</project>

|

  2. 在入口类上,注解 @EnableConfigServer:

  3. 在bootstrap.properties中,增加注解spring.cloud.config.server.bootstrap=true,完整的配置如下:

|

#配置文件名称,即 "-" 之前的
spring.cloud.config.name=config
#配置文件profile,即"-"之后的
spring.profiles.active=dev
spring.application.index=${random.uuid}
spring.application.name=config-client
#关闭鉴权,这样才能post请求访问 当前项目/refresh 接口的时候更新配置文件
management.security.enabled=false
server.port=8881
#配置缓存目录,避免配置丢失
spring.cloud.config.server.git.basedir=/data/${spring.application.name}
spring.cloud.config.server.bootstrap=true
#使用rabitMQ结合spring-cloud-bus,更新所有客户端配置
spring.rabbitmq.host=192.168.50.69
spring.rabbitmq.port=5672

|

  4. 新建bootstrap.yml,在其中配置config-server的git仓库信息,完整配置如下,这里使用的ssh方式连接git,所以没有配置用户名和密码:

|

#对称加密密钥
encrypt:
 key: key1
spring:
 cloud:
 config:
 server:
 git:
# 用户名密码的登录方式使用http形式的uri,sshKey的登录方式使用ssh格式的uri
 uri: xxx
# username: xxx
# password: xxx

 #config-server的前缀,比如访问属性,/encrypt,/decrypt接口的时候需要加上
 prefix: /myconfig
      label: master

|

  5. 此时便完成了改造,经过测试,启动两个config-client,可以使用/bus/refresh刷新所有配置。

  注意:如果在入口类上不注解@EnableConfigServer,那么便不可以通过web访问属性,也不能使用/encrypt,/decrypt接口。

  6. 目前存在一个问题,将config-server嵌入到config-client中之后,即便配置了security相关的用户名密码,调用config-client的接口时,也要求输入用户名密码。解决方案是,在正式环境下或者对安全性有要求的环境下,去掉入口类的@EnableConfigServer注解,此时config-server的接口就无法访问了,包括获取配置文件,/encrypt,/decrypt接口,当需要修改加密的配置文件时,在本地运行项目,使用@EnableConfigServer注解,就可以使用/encrypt接口加密配置文件了。

相关文章