基于git库的Spring Cloud Config配置中心代码demo下载地址:https://gitlab.com/mySpringCloud/config-git

SpringBoot版本: 1.5.9.RELEASE

SpringCloud版本:Edgware.RELEASE

1、Spring Cloud Config Server 服务端

bootstrap.properties配置:

server.port=8001
spring.application.name=config-server-git
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ #关闭刷新安全认证
management.security.enabled=false spring.cloud.config.server.git.uri=https://gitlab.com/mySpringCloud/config-git/springcloudconfig.git
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=princess-azalea@qq.com
spring.cloud.config.server.git.password=qqGroup49167765

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.forezp</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>1.5.9.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>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</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>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>

启动类:

package com.forezp;

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

controller类:

package com.forezp.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /*
* 启动config client之前,要先访问config server端的任意url,要不然config client启动失败,报错为某个值@Value注入失败,原因不明。
*
*/
@RestController
public class ConfigServerController { @RequestMapping(value = "/hi")
public String hi(){
return "Hi this is Config Server!";
}
}

2、Spring Cloud Config Client 客户端

bootstrap.properties配置:

server.port=8002
spring.application.name=config-client-git
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/ #关闭刷新安全认证
management.security.enabled=false eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server-git

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.forezp</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>1.5.9.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>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</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>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>

启动类:

package com.forezp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

controller类:

package com.forezp.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 验证刷新步骤:
* 1、http://localhost:8002/hi 请求获取foo值
* 2、更改git库里foo值
* 3、http://localhost:8002/refresh 刷新某个微服务里的值,需类里加上注解@RefreshScope,并且jar依赖有spring-boot-starter-actuator
* 4、http://localhost:8002/hi 获取新的foo值
*
* @author zhuwen
*
*/
@RestController
@RefreshScope
public class ConfigClientController { @Value("${foo}")
String foo; @RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}

SpringCloudConfig配置中心git库以及refresh刷新的更多相关文章

  1. springcloud(七):配置中心svn示例和refresh

    上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...

  2. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  3. spring-cloud-config 配置中心快速上手

    spring-cloud-config 配置中心实现 Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端. s ...

  4. SpringCloud-Config 配置中心

    概述 分布式系统面临的问题 微服务意味着要将单体应用中的业务拆分成一个个的子服务,这些服务都需要必要的配置信息才能运行,如果有上百个微服务,上百个配置文件,管理起来是非常困难的,这时候,一套集中式的. ...

  5. Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh

    国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心. svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依 ...

  6. spring cloud深入学习(八)-----配置中心svn示例和refresh

    svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依赖 <dependencies> <dependency> <groupId>org.spri ...

  7. 记一次springboot(2.1.6)+springcloud(Greenwich.SR2) 配置中心搭建,支持在线刷新

    1.配置eureka注册中心 EureKaSpringApplication: package com.crow.eureka; import org.springframework.boot.Spr ...

  8. 十九、springcloud(五)配置中心本地示例和refresh

    1.创建spring-cloud-houge-config子项目,测试需要的项目入下 2.添加依赖 <dependency> <groupId>org.springframew ...

  9. springcloud(六):配置中心git示例

    随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...

随机推荐

  1. VS2017 nlog源码查看报错

    最近下载nlog的源码学习,打开解决方案以后无法编译. C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Sd ...

  2. SVN配置(服务器端及客户端)

    版本控制工具,在前几年一直用的SVN,从去年后半年开始本人开始使用GIT.为了避免后期再次使用SVN过程中出现生疏,在此将SVN服务端及客户端配置方式罗列出来. 本人使用的SVN版本为 服务端: Vi ...

  3. thinkphp5.0验证的封装

    刚学完这个验证器封装,刚开始还是有点晕的,后面仔细看了两遍,才慢慢感觉到了继承这个方法的好处,看来还得慢慢锻炼锻炼; 问题:结合上篇的自定义验证器,发现每次使用验证器都重复这样写代码;//验证器$va ...

  4. call,apply,bind 方法的学习

    这是三个常用的操作函数的方法,在js中函数就是一等公民,所以说掌握这三个方法还是有必要的 call 和 apply,都会绑定函数的上下文(context)并立即执行调用该方法函数,两者区别在于,接受的 ...

  5. Visual C++ 6.0中if..else..的简单用法和基本格式

    # include <stdio.h> int main (void) { float score; printf("请输入您的考试成绩:"); scanf(" ...

  6. Sql Server数据字典

    1:添加字段属性或者表属性 execute sys.sp_addextendedproperty @name = N'MS_Description', @value = N'要添加的属性信息', @l ...

  7. 查看那些进程使用了swap

    https://blog.csdn.net/xiangliangyu/article/details/8213127$ sudo pacman -S iotop https://blog.longwi ...

  8. Error: no override found for 'vtkRenderWindow'

    VSK7.1+QT5.10环境下报该错误,应该在mainwindow.cpp中添加如下语句 记住,是在mainwindow.cpp中添加,不是在main.cpp中添加:

  9. poj 3347

    #include <cstring> #include <iostream> #include <cstdlib> #include <iomanip> ...

  10. 在Ubuntu15.10中,使用wxPython的webview和JS进行交互

    在Ubuntu下进行wxPython开发,因为需求,所以使用了wxPython的webview和JS的交互. 在Windows下,下图显示的代码可以正常的运行,但是在Ubuntu下进行开发,以下的代码 ...