一. 关于spring-cloud中的分布式配置

  Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性 ,通常情况下我们可以把需要管理的配置文件放置在svn或者git上进行做统一的配置仓库。

二 创建git远程仓库远程仓库中创建所需的配置文件

  本例子中是demo-local.yml , 配置文件内容如下:

  

student:
name: test
age: 28

三 创建config-server端

1) 创建gradle模块config-server并添加gradle依赖

dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
}

2)编辑application.yml配置

server:
port: 8000
spring:
cloud:
config:
server:
git:
uri: git@gitlab.com:xxxx/config.git
enabled: true
profiles:
active: local

其中spring.cloud.config.server.git是配置远程仓库的地址,如果不使用SSH协议请配置username和password属性

3)编写启动类

package com.bdqn.lyrk.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigServer { public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}

注意在启动类上添加@EnableConfigServer注解

四 创建config-client端

1)创建gradle模块demo-server并添加gradle依赖

dependencies{
compile('org.springframework.cloud:spring-cloud-starter-config')
}

添加bootstrap.yml (Bootstrap.yml(bootstrap.properties)在application.yml(application.properties)之前加载)

server:
port: 8001
spring:
cloud:
config:
uri: http://localhost:8000
profile: local
application:
name: demo

注意几点:

  配置服务从 /{name}/{profile}/{label} 提供属性源,客户端应用程序中的默认绑定

  “name”= ${spring.application.name}
  “profile”= ${spring.profiles.active} (实际上是 Environment.getActiveProfiles() )

  “label”=“master”

  这里面的spring.application.name与远程仓库的配置文件名demo-local对应

  

编写DemoConfig类

 package com.bdqn.lyrk.server.demo.config;

 import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; @Configuration
@ConfigurationProperties(prefix = "student")
public class DemoConfig {
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

注意 @ConfigurationProperties(prefix = "student")注解,该注解作用是:获取yml配置文件的前缀student,配置文件中余下部分用javabean的属性替换即可

编写启动类:

 package com.bdqn.lyrk.server.demo;

 import com.bdqn.lyrk.server.demo.config.DemoConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.context.ApplicationContext; @SpringBootApplication
public class DemoProvider { public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DemoProvider.class, args);
DemoConfig demoConfig = applicationContext.getBean(DemoConfig.class);
System.out.println(demoConfig.getName());
}
}

运行后得到上述结果,此时我们已经从远程配置中获取到所需的信息了

SpringCloud学习之快速搭建分布式配置的更多相关文章

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

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

  2. SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...

  3. 白话SpringCloud | 第七章:分布式配置中心的使用

    前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...

  4. SpringCloud第六步:搭建分布式配置中心

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

  5. SpringCloud搭建分布式配置中心(基于git)

    1.简介 Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用 ...

  6. 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

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

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

  8. 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

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

  9. Spring Boot 学习(一) 快速搭建SpringBoot 项目

    快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...

随机推荐

  1. Node入门教程(4)第三章:第一个 Nodejs 程序

    第一个 Nodejs 程序 本教程仅适合您已经有一定的JS编程的基础或者是后端语言开发的基础.如果您是零基础,建议您先学一下老马的前端免费视频教程 第一步:创建项目文件夹 首先创建 demos 文件夹 ...

  2. PCB名詞解釋:通孔、盲孔、埋孔(转载)

    文章转载自:https://www.researchmfg.com/2011/07/pth-blind-hole-buried-hole/ PCB名詞解釋:通孔.盲孔.埋孔 Posted by 工作熊 ...

  3. Python内置函数(7)——sum

    英文文档: sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns ...

  4. python入门(14)定义函数和接收返回值

    定义函数: 定义一个求绝对值的my_abs函数为例: def my_abs(x): if x >= 0: return x else: return -x 如果没有return语句,函数执行完毕 ...

  5. 【52ABP实战教程】00-- ASP.NET CORE系列介绍

    为什么是.net core? 记得在半年前.NET CORE刚刚出了1.0,当时有朋友推荐我使用的时候,个人觉得还不成熟. 现在.NET Core已经到了2.0,.NET Standard 2.0 添 ...

  6. Apache命令

    参考于:http://www.jinbuguo.com/apache/menu22/programs/apxs.html 安装httpd-devel才有apxs

  7. I/O和管道

    一:I/O设备 I/O(Input/Output),即输入/输出,通常指数据在内部存储器和外部存储器或其他周边设备之间的输入和输出. 标准输入(STDIN):0 默认接受来自键盘的输入 标准输出(ST ...

  8. 老男孩python学习之作业二---三级菜单

    因为之前花力气完成了购物小程序的作业 现在做这个三级菜单简直是so easy!! 1.显示省级菜单 2.交互,提示用户输入要查看的省份(退出e) 2.1.用户正确输入列表中的省份 3.显示市级菜单 3 ...

  9. Extensions in UWP Community Toolkit - WebViewExtensions

    概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...

  10. Spring MVC【入门】就这一篇!

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...