博主分享免费Java教学视频,B站账号:Java刘哥
为什么需要配置中心?换句话说,不用配置中心,会怎么样?
2.application.yml
3.ConfigApplication.java
4.在GitHub上创建一个叫 springcloud-config-repo 的私有仓库
5.上传四个配置文件
6.启动项目
默认是启动在8080端口
通过地址栏访问如下链接,都可以读取到dev环境的配置,即 git 仓库中 master 分支application-dev.yml 里的内容
2.删除之前的配置文件,创建 bootstrap.yml 文件
- 不方便维护:代码和配置在一起,如 springboot ,配置全部在项目的resources目录下,要修改某个配置,需要打开对应的项目源码,进行修改。
- 配置内容安全与权限:配置和源码在一起通常是不安全的,比如数据库连接信息敏感的内容,我不想让开发人员或者其他人看到,开发环境看了还无所谓,尤其是测试环境和生产环境的配置一般是不暴露的。
- 更新配置项目需要重启:配置文件写在项目里,修改了配置必然需要重启,而重启项目的代价还是很高的。
一、搭建一个简单的 config server 项目
config server 即注册中心,该项目一旦启动就不用管了,甚至打包后就不用管了。跟 Eureka 一样 "一次打包,不再修改,直接运行"。 需要准备:GitHub账号,IDEA 项目结构如下 只需要关注pom.xml 和 application.yml,以及 ConfigApplication 即可 1.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.liuyanzhao</groupId>
- <artifactId>config</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>config</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.0.BUILD-SNAPSHOT</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>
- <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</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>
- </project>
- spring:
- application:
- name: config
- cloud:
- config:
- server:
- git:
- # Git仓库地址
- uri: https://github.com/saysky/springcloud-config-repo
- # Git仓库用户名或邮箱
- username: GitHub邮箱
- # Git仓库密码
- password: GitHub密码
- package com.liuyanzhao.config;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.config.server.EnableConfigServer;
- @SpringBootApplication
- @EnableConfigServer
- public class ConfigApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigApplication.class, args);
- }
- }
- 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 里添加依赖- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- spring:
- application:
- # 对应config server所获取的配置文件的 {application}
- name: application
- cloud:
- config:
- # 指 config server 地址
- uri: http://localhost:8080/
- # 指定 config server 里的 {profile}
- profile: dev
- # 指定Git仓库分支,对应config server的{label}
- label: master
bootstrap.yml 内容
3.启动项目 我们在 IDEA 控制台可以看到读取了配置中心的配置 同时,项目也启动成功了,调用查询数据库的接口,发现没问题三、使用 Spring Cloud Bus 自动刷新配置
见下文:使用 Spring Cloud Bus 自动刷新配置(SpringBoot2.x版本)- 微信
- 交流学习,有偿服务
- 博客/Java交流群
- 资源分享,问题解决,技术交流。群号:590480292
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏