spring cloud 配置中心 config 搭建过程

1.搭建config-server 服务端

1.1. 新建boot工程 pom引入依赖

<!-- config配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.2.启动类添加注解@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }

1.3 application.properties添加配置

server.port=9003
spring.application.name=config-server #eureka注册中心
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port} #本地配置
#spring.profiles.active=native
#spring.cloud.config.server.native.search-locations=classpath:/ #远端配置
spring.cloud.config.server.git.uri=http://127.0.0.1:8040/root/config-resp.git
spring.cloud.config.server.git.username=root
spring.cloud.config.server.git.password=123456
spring.cloud.config.server.git.search-paths=user_service,product_service
spring.cloud.config.label=master

1.4 配置验证

远端仓库配置文件

页面读取查看

2.配置读取客户端

2.1 pom引入依赖

  <!--配置中心 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2.2 添加bootstrap.properties 配置文件

由于spring boot 配置读取顺序,将config相关配置写在application.properties 无效,spring boot 会自动连接默认的localhost:8888 读取配置中心 。所以新建bootstrap.properties配置,bootstrap.properties的优先级大于application.properties。

bootstrap.properties

#eureka注册中心
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
#config server
spring.cloud.config.name=product_service
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

2.3 读取配置,使用验证

程序代码

@Value("${product.name}")
private String productName; @GetMapping("/getProductName")
public String getProductName(){
return productName;
}

调用程序获取

项目github 地址  https://github.com/yongxiangliu123/SpringCloud

spring cloud(三) config的更多相关文章

  1. Alibaba Nacos 学习(二):Spring Cloud Nacos Config

    Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...

  2. Spring Cloud Consul Config 知识点

    Spring Cloud Consul Config 是 Config Server 和 Client的替代方案. 搭建一个配置中心,可以选择的方案: Spring Cloud Config 或者 S ...

  3. Spring Cloud之——Config(配置中心)

    Spring Cloud Config(配置中心) 大家好,有一段时间没有写技术博客了.由于工作上的事情,这方面很难分配时间.近几年随着服务化的兴起,一批服务化的框架应运而生,像dubbo,thrif ...

  4. Spring Cloud(四) --- config

    Spring Cloud Config 随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的 ...

  5. Spring Cloud 之 Config与动态路由.

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

  6. spring cloud(三)服务提供与调用

    服务提供 我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务 1.pom包配置 创建一个springboot项目 ...

  7. Spring Cloud(三):服务提供与调用

    上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例. 案例中有三个角色:服务注册中心.服务提 ...

  8. spring cloud (八) Config client 和项目公共配置

    1 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  9. spring cloud (七) Config server基于svn配置

    1 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

随机推荐

  1. PHP tools for Visual Studio 2013 安装、破解、配置教程

    安装 首先,必须要安装vs2013.本人安装的是社区版,免费的同时功能又全面. 然后,去http://download.csdn.net/detail/liangzehong007/9076855 或 ...

  2. ExtJs之Ext.comboBox的远程数据源读取程序

    既然可以测试本地AJAX,那就把书前面的代码作一次学习吧. <!DOCTYPE html> <html> <head> <title>ExtJs< ...

  3. BA-WG-调试经验

    Modbus网关 目前常用的产品为BAM360,仅可以用做modbus RTU主站,可以从从站扫描数据,接入的数据必须为标准的MODBUS RTU从站 串口数据 使用sc-431硬件将串口数据转换为m ...

  4. leetcode第一刷_Minimum Depth of Binary Tree

    非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象 ...

  5. ViewPage+Frament+listView滑动效果

    近期在做一个须要使用Frament+ViewPage制作一个滑动的效果,看了非常多资料,最终实现了,这与大家分享一下战果 总结一下.这里我做了一个Demo分享给大家 我的文件文件夹结构图 1.首先要有 ...

  6. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  7. luogu3811 【模板】乘法逆元

    题目大意:给出n,求1~n所有数的乘法逆元. 乘法逆元的概念是:如果b*rev(b)≡1 (mod p),p与b互质,则rev(b)就是b的模p乘法逆元.乘法逆元往往用于除法取模. 具体操作详见htt ...

  8. GIS中mybatis_CMEU的配置方法

    基本常用功能预览: 生成实体类(可以自定义:get/set,有参无参构造方法,自定义类型与属性,序列化等); 生成dao层接口(查询全部信息,通过ID查询信息,插入全部属性,插入不为空的属性,通过ID ...

  9. pom.xml出现web.xml is missing and <failOnMissingWebXml> is set to true解决方案

    提示信息应该能看懂.也就是缺少了web.xml文件,<failOnMissingWebXml>被设置成true了. 搜索了一下,Stack Overflow上的答案解决了问题,分享一下. ...

  10. hdoj--3072--Intelligence System(scc+缩点+数据去重)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...