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 ...
随机推荐
- Java集合(类)框架(二)
1.Set集合 1.1 HashSet集合 HashSet底层为哈希码 不是数组,因此没有下标的概念,也就不能根据下标来查询某个元素 存放元素无序,不可重复 1.1.1 声明 Set<Strin ...
- P2937 [USACO09JAN]Laserphones S
题意描述 [USACO09JAN]Laserphones S 学过物理的同学都知道这种镜子是可以把光线旋转 90 度的. 那么显然就是要求添加镜子的最小个数. 貌似题目漏了一句就是题目保证有解的情况. ...
- JSON&AJAX
JSON 定义:JSON(JavaScript Object Notation, JS 对象简谱)是一种轻量级的数据交换格式.它基于 ECMAScript(欧洲计算机协会制定的 JS 规范)的一个子集 ...
- python_面向对象_组合
组合: 一个类的对象是另外一个类对象的属性 # 组合 # 一个类的对象是另一个类对象的属性 # 什么时候使用组合:当两个类之间的关系是 :什么有什么的关系 : 班级有学生 学生有班级 班级有课程 图书 ...
- vs code远程开发
VS Code如何配置远程开发 你是如何远程开发的?还在使用FTP/SFTP同步文件?那你out了,有了宇宙第一IDE:VS就不需要这么麻烦了,一起学习一下吧. 第一步,安装Remote SSH插件 ...
- 僵尸进程与SIGCHLD信号
什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打开的文件等,但内核为每一个终止子进程保存了一定量的信息.这些信息至少包括进程ID,进程的终止状态,以及该 ...
- C#高级编程之反射
反射的定义 MSDN定义:反射提供描述程序集.模块和类型的对象(Type类型). 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后调用其方法或访问器字段和属性. 如 ...
- DockerPush
1.阿里云镜像发布流程 2.镜像生成 语法:docker commit [OPTIONS] 容器ID [REPOSITORY[:TAG]] [root@pluto data]# docker imag ...
- Docker学习第四天(Docker四种网络模式)
Docker四种网络模式 实现原理 Docker使用Linux桥接(参考<Linux虚拟网络技术>),在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根 ...
- sqlilab less28 less28a
less-28 less-28a 二者相差不大 单引号小括号包裹,黑名单过滤--,#,空格,union空格select(不区分大小写) less-28的黑名单 less-28a的黑名单 %a0,不被 ...