在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置、日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的。随着服务数量的增加,配置文件的管理也是一件非常复杂的事

在微服务架构中,需要有统一管理配置文件的组件,例如 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;
}
}

测试

  1. 启动 config-server
  2. 启动 eureka-server
  3. 启动 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的更多相关文章

  1. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...

  2. spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志

    spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...

  3. Spring boot 学习六 spring 继承 mybatis (基于注解)

    MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执 ...

  4. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  5. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  6. spring cloud学习(六) 配置中心-自动更新

    上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...

  7. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  8. Spring Cloud 学习 (九) Spring Security, OAuth2

    Spring Security Spring Security 是 Spring Resource 社区的一个安全组件.在安全方面,有两个主要的领域,一是"认证",即你是谁:二是& ...

  9. spring学习 六 spring与mybatis整合

    在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...

随机推荐

  1. Python3网络学习案例四:编写Web Proxy

    代理服务器的定义和作用请走百度百科~ 1. Web Proxy的实现思路 这是基于上一篇"编写Web Server"写的,主要逻辑见下图: 我们要写的就是中间的Web Proxy部 ...

  2. 小白如何学习PyTorch】25 Keras的API详解(下)缓存激活,内存输出,并发解决

    [新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑答疑解惑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx6450 ...

  3. AE(After Effects)的简单使用——记一次模板套用的过程

    一.环境配置 操作系统:win10 64位 软件版本:AE16.11版 二.模板下载 可去 newcger 网站进行模板下载,常用都是免费的,很好用,很推荐 三.具体操作 1.模板素材替换 点击[文件 ...

  4. sdasd

    create PROCEDURE v4(in c_year int) BEGIN declare num int(10) default 0; declare num1 int(10); select ...

  5. python的各版本的不同

    Python的版本主要分为 2.× . 3.× 两个系列. Python3计划每年发布一个新的子版本,一次只增加一两种新语法. 使用时当然选择越新的Python版本越好,版本越老的代码越难维护. 维护 ...

  6. MySql中指定符号分割并分行展示

    1.涉及到的函数三个: 1.1 REPLACE('value','str1','str2') 用法规则:使用str2替换掉value中的所有的str1; SELECT REPLACE('我来了','来 ...

  7. springboot mybatis保存数据中文保存成???

    springboot mybatis保存数据中文保存成??? 解决方法: 数据库存连接配置后面加characterEncoding=UTF-8 如下例: spring.datasource.url=j ...

  8. 1,认识web

    主要记录黄勇的视频讲解.CSDN:https://blog.csdn.net/nunchakushuang/article/list/1? 他的文档大纲为: video3 拓展1:HTTP与HTTPS ...

  9. Tab + Swipe+ RecyclerView + Collapsed

    随着Android的不断更新,老旧的布局页面已经过时,这就使得复杂的布局实现起来有些难度,在此记录一下手机中最常见的复杂界面实现方法. 最终效果 本文主要通过分析最新版AS下new project的S ...

  10. 经典c程序100例 1-10

    ==1--10 [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不 ...