1.构建config-server

创建一个pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bennytitan</groupId><artifactId>config-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-server</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 新增依赖 --><dependency><groupId>com.fasterxml</groupId><artifactId>classmate</artifactId><version>1.4.0</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>public</id><name>aliyun nexus</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>

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

application.properties

spring.application.name=config-server
server.port=8888eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#spring.cloud.config.server.git.uri=https://gitee.com/JeeGemEdu/SpringCloudConfig/
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=

启动服务,浏览器输入http://localhost:8888/foo/dev,得到下图结果表示config状态正常,可以远程取用配置文件了

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

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

2.构建一个config client

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bennytitan</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-client</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 新增依赖 --><dependency><groupId>com.fasterxml</groupId><artifactId>classmate</artifactId><version>1.4.0</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>public</id><name>aliyun nexus</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>

ConfigClientApplication.java
package com.bennytitan.configclient;

import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication@RestControllerpublic class ConfigClientApplication {

    public static void main(String[] args) {        SpringApplication.run(ConfigClientApplication.class, args);    }

    @Value("${foo}")    String foo;    @RequestMapping(value = "/hi")    public String hi(){        return foo;    }}
 

application.properties

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881

打开网址访问:http://localhost:8881/hi,网页显示:

3.高可用分布式配置

上面的config-server已经配置好了,如果可以,可以启动多个config-server。

主要是改造config-client的pom.xml增加客户端服务管理:

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

然后application.properties的配置如下,这里都不需要指定config-server的IP地址,直接通过服务ID就能获取到了:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881

测试,访问http://localhost:8881/hi,浏览器显示:

转载于:https://www.cnblogs.com/BennyTitan/p/9212202.html

Spring Cloud Config 配置中心相关推荐

  1. (七)Alian 的 Spring Cloud Config 配置中心(客户端)

    目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景   通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...

  2. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  3. (六)Alian 的 Spring Cloud Config 配置中心(服务端)

    目录 一.简介 二.数据库 2.1.应用表 2.2.属性表 2.3.视图 2.4.初始化数据 三.配置 3.1.pom.xml 3.2.application.properties 3.3.主类 3. ...

  4. Spring Cloud Config配置中心的使用

    一.概述 1. 为什么使用? 1> 配置文件太多,不方便维护 2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人 3> 更新配置项目需 ...

  5. git在clone时需要输入密码Enter passphrase for key 导致spring cloud config 配置中心无法拉取配置文件的解决方法

    前几天把系统从win7换到了win10 重装了开发环境 一直没什么问题 今天在调试spring cloud 时 发现无论如何都拉取不到配置文件, 通过微服务日志提示 Could not locate ...

  6. 【SpringCloud】Spring Cloud Config 配置中心

    文章目录 1.概述 1.1 是什么 1.2 怎么玩 1.3 怎么用 3.案例 3.1 案例1 3.1.1 配置 3.1.2 主类 3.1.3 Git信息 3.1.4 访问 4. 例子 5.客户端 5. ...

  7. Spring Cloud Config配置中心使用(草稿版)

    服务端搭建: cli:

  8. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  9. 【夯实Spring Cloud】Spring Cloud分布式配置中心详解

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

最新文章

  1. 移动前端html5 head 头标签
  2. Host Switch Plus结合nginx使用
  3. SkGradientShader::CreateLinear 的参数。
  4. java 中文域名转码_转换java方法
  5. php正则匹配怎么写,正则表达式 - 求助怎么写php的正则匹配
  6. Python GUI
  7. 如何设置静态内容缓存时间
  8. [WCF]终结点与服务寻址(一)
  9. android单例模式代码,在Android studio 中使用单例模式(示例代码)
  10. java 反射调用方法_Java 反射详解,重要方法解析
  11. c语言程序设计 甘勇,C语言程序设计
  12. 沙盘模拟软件_电子沙盘的分类
  13. 面向对象编程三大特性------封装、继承、多态
  14. php 获取农历,PHP获取农历、阳历转阴历
  15. iOS-苹果官方开源网站;objc、Runloop、GCD、OC等开源代码
  16. 语c语言描写,【自由の翼】语c介绍
  17. lcx端口转发工具的使用
  18. python线性加权回归_第二十一章 regression算法——线性回归局部加权回归算法(上)...
  19. 使用 CSS 创建自定义鼠标游标
  20. python抓取微信_python抓取搜狗微信公众号文章

热门文章

  1. 301重定向的好处:
  2. NHibernate: Session.Save 采用版本控制时无必要地自动Update版本字段的问题
  3. python源代码的后缀名是_Python代码编译与反编译
  4. 分治法的关键特征_你真的读懂《OKR工作法》了吗?
  5. springboot tomcat配置_用了 10 多年的 Tomcat 居然有bug !
  6. 网站服务器停止响应,如何解决apache停止响应的问题
  7. 普通高中计算机课程软件,普通高中信息技术课程标准(2017 年版)(4):选修课程...
  8. mysql v8 漏洞_mysql'密码安全 - osc_v8gts6gd的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. 8086 c语言,2016年上海大学机电工程与自动化学院微机硬件及软件(包含8086微机和C语言)之C程序设计考研复试题库...
  10. c 运行 java linux命令行参数,Linux下用命令行编译运行Java总结