本章只讲 Spring Cloud 本地配置方式,可以很方便的高可用集群,且存在良好通讯,不用担心云服务器与内网之间GIT带来的不便,GIT(网上GIT教程一搜一大把了….)

- 快速开始

Spring Cloud Config为分布式系统中的外部配置,提供了服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射的概念与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_config

- 服务架构图

服务架构图

画图工具https://www.processon.com/

- 准备工作

1.创建 battcn-config-server 和 battcn-config-client ,如果已经从第一章看到这里的朋友们应该都知道pom.xml的一些基本配置了,本节开始只贴关键部分代码,完整的直接看GIT 就行了,不然每次都导致内容太多让人没有看的欲望了…

- battcn-config-server

2.导入 config-server 包,目前只需要这一个就够了

1
2
3
4
5
6
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

3.创建启动APP.java程序,添加 @EnableConfigServer 注解即可

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

4.application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port: 9000
spring:
application:
name: battcn-config-server
profiles:
active:
- native
cloud:
config:
name: config-server #{application}
enabled: false
server:
health:
enabled: false

5.最后创建一个 config-server-order-default.yml 这个就是给到其它项目使用的,使用方式也极其简单

1
2
order:
name: My Name's Order Service,Are you Afraid?

- 测试一把

http请求地址和资源文件映射如下:

  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties
  • /{application}/{profile}[/{label}]

其中application是在常规Spring Boot应用程序中注入spring.cloud.config.name的 SpringApplication(即通常是”application”),application是活动配置文件(或逗号分隔的属性列表),label是可选的git标签(默认为master)。

所以捏,我们这快 地址应该写成 http://localhost:9000/config-server/order-default 其中 config-server 就是我们配置的spring.cloud.config.name 也就是 {application} 然后 order-default 就是我们的 {profile}

结果:{"name":"config-server","profiles":["order-default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/config-server-order-default.yml","source":{"order.name":"My Name's Order Service,Are you Afraid?"}}]} 表示OK了,服务端配置完毕,接下来配置需要调用的客户端

- battcn-config-client

1.导入以下包,一个是consul的服务发现包,如果不知道的请参考 一起来学SpringCloud之-注册中心(Eureka/Consul),第二个就是Client需要依赖的包,第三个是心跳检测需要依赖的,cloud中很多都会使用到,有兴趣的可以百度了解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

2.我们 battcn-config-client 是没有配置 order.name 属性的,因此是从 config-server 中读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConfigClientApplication { @Value("${order.name}")
String orderName; @RequestMapping("/test")
public String test() {
return "client ====>>> " + orderName;
} public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

3.创建 bootstrap.yml,不能是application.yml 具体原因请看注意事项

1
2
3
4
5
6
7
8
9
10
server:
port: 9001
spring:
application:
name: battcn-config-client
cloud:
config:
name: config-server
profile: order-default
uri: http://localhost:9000

- 注意事项

创建 bootstrap.yml ,这里需要注意一下的是,SpringCloud Config是不认 application.yml 的配置,这也是Cloud官方有说明的,不然配置的 uri属性是无效的

参考链接:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_the_bootstrap_application_context 因此有的配置只能通过 bootstrap.yml,否则就会被覆盖(附源码)

源码

- 测试一把

启动:consul agent -dev 启动consul

启动:battcn-config-server 和 battcn-config-client

访问:http://localhost:9001/test

1
client ====>>> My Name's Order Service,Are you Afraid?		#表示成功

分布式配置中心(Native - Config)的更多相关文章

  1. SpringCloud 进阶之分布式配置中心(SpringCloud Config)

    1. SpringCloud Config SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用 的所有环境提供了一个中心化的外部配置; ...

  2. Spring-cloud微服务实战【九】:分布式配置中心config

      回忆一下,在前面的文章中,我们使用了spring cloud eureka/ribbon/feign/hystrix/zuul搭建了一个完整的微服务系统,不管是队内还是对外都已经比较完善了,那我们 ...

  3. springcloud 高可用分布式配置中心

    SpringCloud教程七:高可用的分布式配置中心(SpringCloud Config) 当服务有很多 都要从服务中心获取配置时 这是可以将服务中心分布式处理 是系统具备在集群下的大数据处理 主要 ...

  4. springcloud学习之路: (五) springcloud集成SpringCloudConfig分布式配置中心

    SpringCloud全家桶中的分布式配置中心SpringCloudConfig, 它使用git来管理配置文件, 在修改配置文件后只需要调用一个接口就可以让新配置生效, 非常方便. SpringClo ...

  5. SpringCloud(6)分布式配置中心Spring Cloud Config

    1.Spring Cloud Config 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组 ...

  6. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  7. springCloud学习-高可用的分布式配置中心(Spring Cloud Config)

    1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...

  8. springCloud学习-分布式配置中心(Spring Cloud Config)

    1.简介 Spring Cloud Config :分布式配置中心,方便服务配置文件统一管理,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud co ...

  9. 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)

    上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...

  10. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

随机推荐

  1. Java实现 LeetCode 216. 组合总和 III(三)

    216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...

  2. Java实现 LeetCode 103 二叉树的锯齿形层次遍历

    103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...

  3. Java实现 蓝桥杯VIP 算法提高 格子位置

    算法提高 格子位置 时间限制:1.0s 内存限制:512.0MB 问题描述 输入三个自然数N,i,j (1<=i<=N,1<=j<=N),输出在一个N*N格的棋盘中,与格子(i ...

  4. Linux文件系统--基于EXT2

    一.文件系统基本知识 ① 存储在永久性存储介质,由程序按照某种格式制作的数据集合叫做文件 ② 磁盘上管理文件的文件.数据结构和操作构成磁盘文件系统,简称文件系统 ③ 文件属性 (1)名称 (2)位置 ...

  5. 曹工说JDK源码(3)--ConcurrentHashMap,Hash算法优化、位运算揭秘

    hashcode,有点讲究 什么是好的hashcode,一般来说,一个hashcode,一般用int来表示,32位. 下面两个hashcode,大家觉得怎么样? 0111 1111 1111 1111 ...

  6. 源码分析 | 手写mybait-spring核心功能(干货好文一次学会工厂bean、类代理、bean注册的使用)

    作者:小傅哥 博客:https://bugstack.cn - 汇总系列原创专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 一个知识点的学习过程基本分为:运行helloworld ...

  7. 如何将H5一键部署到托管服务中

    随着各个大型App都推出了自己的小游戏平台,游戏也越来越受到开发者的关注.Cocos Creator是一个完整的游戏开发解决方案,包含了轻量高效的跨平台游戏引擎,以及能让你更快速开发游戏所需要的各种图 ...

  8. 详解CurrentHashMap之预习篇

    CurrentHashMap的出现时为了解决HashMap的高并发导致OOM的缺陷,并且能够保证高性能读取.那么解读CurrentHashMap需要具备哪些知识的呢? HashMap 解读 Java ...

  9. centos7 hive 单机模式安装配置

    前言:由于只是在自己的虚拟机上进行学习,所以对hive只是进行最简单的配置,其他复杂的配置文件没有配置. 1.前提 1.1 安装配置jdk1.8 1.2 安装hadoop2.x hadoop单机模式安 ...

  10. Excel随机生成批量日期,以及注意事项

    这个是WPS里写的一个函数,用来随机生成日期.首先E1和E2是两个日期端点,右键把单元格格式先设置成“日期”中的“xxxx年xx月xx日 xx:xx”,然后E3=E1-E2算出它们的距离. 在E4里面 ...