Spring Cloud 学习 (六) Spring Cloud Config
在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置、日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的。随着服务数量的增加,配置文件的管理也是一件非常复杂的事
在微服务架构中,需要有统一管理配置文件的组件,例如 Spring Cloud 的 Spring Cloud Config、阿里的 Diamond、百度的 Disconf、携程的 Apollo 等
新建 spring-cloud-config-server
从本地读取配置
pom
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.karonda</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml
server:
port: 8061
spring:
application:
name: config-server
profiles:
active: native # 从本地读取配置
cloud:
config:
server:
native:
search-locations: classpath:/shared # 本地配置路径
新建配置文件 shared/eureka-client-dev.yml
version: 1.0.0
启动类
@EnableConfigServer // 开启 Config Server
@SpringBootApplication
public class ConfigServerApp {
public static void main(String[] args){
SpringApplication.run(ConfigServerApp.class, args);
}
}
测试
启动 config-server
访问 http://localhost:8061/eureka-client/dev 可以看到配置
在客户端使用
在 eureka-client 测试
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加配置 bootstrap.yml
spring:
application:
name: eureka-client
cloud:
config:
uri: http://localhost:8061
fail-fast: true
profiles:
active: dev
bootstrap.yml 优先于 application.yml
修改测试代码
@RestController
public class HiController {
@Value("${server.port}")
int port;
@Value("${version}")
String version;
@GetMapping("/hi")
public String home(@RequestParam String name){
return "Hello " + name + ", from port: " + port + ", version: " + version;
}
}
测试
- 启动 config-server
- 启动 eureka-server
- 启动 eureka-client
访问 http://localhost:8011/hi?name=Victor
从远程 Git 仓库读取配置
修改 application.xml
server:
port: 8061
spring:
application:
name: config-server
# profiles:
# active: native # 从本地读取配置
cloud:
config:
server:
# native:
# search-locations: classpath:/shared # 本地配置路径
git:
uri: https://github.com/VictorBu/spring-cloud-config-demo
search-paths: repo # 搜索的文件夹地址
# username: # 如果是私有仓库须配置
# password: # 如果是私有仓库须配置
label: master # git 仓库分支名
测试
参考上节
完整代码:GitHub
本人 C# 转 Java 的 newbie, 如有错误或不足欢迎指正,谢谢
Spring Cloud 学习 (六) Spring Cloud Config的更多相关文章
- spring cloud学习(六)Spring Cloud Config
Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...
- spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志
spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...
- Spring boot 学习六 spring 继承 mybatis (基于注解)
MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(搭建)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...
- spring cloud学习(六) 配置中心-自动更新
上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Spring Cloud 学习 (九) Spring Security, OAuth2
Spring Security Spring Security 是 Spring Resource 社区的一个安全组件.在安全方面,有两个主要的领域,一是"认证",即你是谁:二是& ...
- spring学习 六 spring与mybatis整合
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...
随机推荐
- svg究竟是什么?
svg究竟是什么? 1 要点 要点1:svg与jpg/png等格式的用途完全不同,不可相提并论,没有可比性,不可互相替代. 要点2:日常生活中,我们用相机拍摄自然景象得到的照片和视频,能且只能用jpg ...
- php 使用 phpword 操作 word 读取 word
思路 1. 加载word文件.2. 循环判断加载出来的数据.( 数据下面有很多个节点 )( 节点是按照数据的类型分类的 例如 无样式的文本是RunText,换行是TextBreak,表格是table. ...
- Thinkphp3.2 cms之权限管理
五.权限管理 <?php namespace Admin\Controller; use Think\Controller; class CommonController extends Con ...
- 记载idea创建spring-boot项目时“Spring Initalizr Error”的问题处理
- 学习笔记——ESP8266项目的例子编译时发生cannot find -lstdc++问题的解决
在尝试对进行ESP8266项目的例子进行编译时发生cannot find -lstdc++问题 第一想法是安装libstdc++,结果安装时又发生了下面的情况: 再次查找原因,最后发现当前安装的交叉编 ...
- JS如何判断表单中用户选择哪个哪个选项?
JS如何判断表单中用户选择哪个哪个选项? HTML代码: <form name="form1" onsubmit="return foo();"> ...
- 基于云开发 CloudBase 搭建在线视频会议应用教程
基于云开发 CloudBase 搭建在线视频会议应用 在线视频会议应用是基于浏览器的能力 WebRTC 以及 腾讯云开发 CloudBase 能力构建而成的应用. 在云开发的助力下, 一个复杂的在线会 ...
- sql实现通过父级id查询所有的子集
通过sql实现传入父级id查询出所有的子集 最近刚好有个业务需要这样实现个功能,就是在点击查询列表详情的时候只会传入父级id,而详情得渲染出所有子集,那么做法有很多,可以直接通过代码递归查询去实现, ...
- Jmeter 处理接口乱码
第一步:添加 BeanShell Listener 第二步: 设置值 : prev.setDataEncoding("UTF-8") 第三步: 重新跑脚本,看接口返回值
- Linux bash反弹shell原理引起的一点思考
最近,一起做安全测试的兄弟问我:Linux反弹shell的命令是什么,我毫不犹豫地在笔记中找到发给了他,可是脑海中突然闪过有个疑问,为啥这样能反弹shell呢,用了那么多次却从来没有想过这个问 ...