SpringCloud配置中心Config的搭建

avatar 2019年06月30日17:20:30 6 9106 views
博主分享免费Java教学视频,B站账号:Java刘哥
  为什么需要配置中心?换句话说,不用配置中心,会怎么样?
  • 不方便维护:代码和配置在一起,如 springboot ,配置全部在项目的resources目录下,要修改某个配置,需要打开对应的项目源码,进行修改。
  • 配置内容安全与权限:配置和源码在一起通常是不安全的,比如数据库连接信息敏感的内容,我不想让开发人员或者其他人看到,开发环境看了还无所谓,尤其是测试环境和生产环境的配置一般是不暴露的。
  • 更新配置项目需要重启:配置文件写在项目里,修改了配置必然需要重启,而重启项目的代价还是很高的。
为了解决上面的问题,所以我们必须使用统一配置中心,springcloud 的 config 组件可以帮助我们快速创建一个配置中心。 配置中心读取配置文件通常有两种,一直是从 git 仓库读,一直是从本地物理路径读。一般公司用的还是后者,但是为了方便我们还是使用前者通过读取 git 仓库中的配置。 开始实践吧!  

一、搭建一个简单的 config server 项目

config server 即注册中心,该项目一旦启动就不用管了,甚至打包后就不用管了。跟 Eureka 一样 "一次打包,不再修改,直接运行"。 需要准备:GitHub账号,IDEA 项目结构如下   只需要关注pom.xml 和 application.yml,以及 ConfigApplication 即可 1.pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>com.liuyanzhao</groupId>
  6.     <artifactId>config</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>
  9.     <name>config</name>
  10.     <description>Demo project for Spring Boot</description>
  11.     <parent>
  12.         <groupId>org.springframework.boot</groupId>
  13.         <artifactId>spring-boot-starter-parent</artifactId>
  14.         <version>2.0.0.BUILD-SNAPSHOT</version>
  15.         <relativePath/> <!-- lookup parent from repository -->
  16.     </parent>
  17.     <properties>
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20.         <java.version>1.8</java.version>
  21.         <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
  22.     </properties>
  23.     <dependencies>
  24.         <dependency>
  25.             <groupId>org.springframework.cloud</groupId>
  26.             <artifactId>spring-cloud-config-server</artifactId>
  27.         </dependency>
  28.     </dependencies>
  29.     <dependencyManagement>
  30.         <dependencies>
  31.             <dependency>
  32.                 <groupId>org.springframework.cloud</groupId>
  33.                 <artifactId>spring-cloud-dependencies</artifactId>
  34.                 <version>${spring-cloud.version}</version>
  35.                 <type>pom</type>
  36.                 <scope>import</scope>
  37.             </dependency>
  38.         </dependencies>
  39.     </dependencyManagement>
  40.     <build>
  41.         <plugins>
  42.             <plugin>
  43.                 <groupId>org.springframework.boot</groupId>
  44.                 <artifactId>spring-boot-maven-plugin</artifactId>
  45.             </plugin>
  46.         </plugins>
  47.     </build>
  48. </project>
  2.application.yml
  1. spring:
  2.   application:
  3.     name: config
  4.   cloud:
  5.     config:
  6.       server:
  7.         git:
  8.           #  Git仓库地址
  9.           uri: https://github.com/saysky/springcloud-config-repo
  10.           #  Git仓库用户名或邮箱
  11.           username:  GitHub邮箱
  12.           #  Git仓库密码
  13.           password: GitHub密码
  3.ConfigApplication.java
  1. package com.liuyanzhao.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.config.server.EnableConfigServer;
  6. @SpringBootApplication
  7. @EnableConfigServer
  8. public class ConfigApplication {
  9.     public static void main(String[] args) {
  10.         SpringApplication.run(ConfigApplication.class, args);
  11.     }
  12. }
  4.在GitHub上创建一个叫 springcloud-config-repo 的私有仓库   5.上传四个配置文件   6.启动项目 默认是启动在8080端口 通过地址栏访问如下链接,都可以读取到dev环境的配置,即 git 仓库中 master 分支application-dev.yml 里的内容
  • http://localhost:8080/application-dev.yml
  • http://localhost:8080/application/dev
  • http://localhost:8080/application-dev.properties
效果如下图

http://localhost:8080/application/dev 示例图

http://localhost:8080/application-dev.properties 示例图

http://localhost:8080/application-dev.yml 示例图

  注意,这里省略了分支名,默认是master分支 完整的写法是: http://localhost:8080/master/application/dev http://localhost:8080/master/application-dev.yml http://localhost:8080/master/application-dev.properties   其实映射的规则如下 /{application}/{profile} /{application}-{profile}.yml /{application}-{profile}.properties   针对 application-dev.yml 而言 其中 {application} 指的是配置文件的前缀,通常我们取微服务的名称好一点,这里为了简单就叫application; {profile} 是-后面的部分,即dev,通常指的是环境  

二、编写 config client

现在我们需要让我们的项目直接从配置中心读取配置,我们这里以我去年写的一个SpringCloud的博客(当时写了一大半,后来就没写了)项目为例。 这是一个服务提供者,即生产者。消费者是 blog-web这里就不贴了。 我们目前配置是从 resources 下读取配置,比如目前读取的是 application.yml 和 application-dev.yml 我们希望让该项目从注册中心读取配置 来试试吧!   1.pom.xml 里添加依赖
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-config</artifactId>
  4. </dependency>
  2.删除之前的配置文件,创建 bootstrap.yml 文件
  1. spring:
  2.   application:
  3.     # 对应config server所获取的配置文件的 {application}
  4.     name: application
  5.   cloud:
  6.     config:
  7.       # 指 config server 地址
  8.       uri: http://localhost:8080/
  9.       # 指定 config server 里的 {profile}
  10.       profile: dev
  11.       # 指定Git仓库分支,对应config server的{label}
  12.       label: master
 

bootstrap.yml 内容

3.启动项目 我们在 IDEA 控制台可以看到读取了配置中心的配置 同时,项目也启动成功了,调用查询数据库的接口,发现没问题    

 三、使用 Spring Cloud Bus 自动刷新配置

见下文:使用 Spring Cloud Bus 自动刷新配置(SpringBoot2.x版本)
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0