为了更好的管理应用的配置,也为了不用每次更改配置都重启应用,我们可以使用配置中心

关于eureka的服务注册和rabbitMQ的安装使用(自动更新配置需要用到rabbitMQ)这里不赘述,只关注配置中心的内容

我们需要引入关键的包是这三个

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

需要在启动类加上@EnableConfigServer注解,这里有个busRefresh2方法需要讲下,其实这里有个坑,如果在github的webhook直接访问/actuator/bus-refresh是有json转换错误的,

因为github触发webhook的时候会带一大串字符,就是这段字符引发json转换错误,所以我们在这里包装下/actuator/bus-refresh

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
@RestController
public class ConfigApplication {
@PostMapping("/actuator/bus-refresh2")
@ResponseBody
public Object busRefresh2(HttpServletRequest request, @RequestBody(required = false) String s) {
System.out.println(s);
return new ModelAndView("/actuator/bus-refresh");
}
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
} }

然后在配置文件上加上这些内容,主要是配置github上的配置文件的路径,配置rabbitMQ和启用bus-refresh端点。

spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/skychmz/config.git
username: skychmz
password: xxxxxxx
rabbitmq:
host: localhost
username: guest
password: guest management:
endpoints:
web:
exposure:
include: bus-refresh

如果某个服务需要使用配置中心,先引入以下包

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

在yml配置文件上加上如下配置,主要是开启配置中心,我们的配置中心的服务名是CONFIG

spring:
cloud:
config:
discovery:
enabled: true
service-id: CONFIG

把该服务的配置文件放到github,注意eureka的配置还是要放本地的,因为服务启动的时候要先访问eureka才会找到CONFIG服务

配置github的webhooks,这里我用的是内网穿透的地址

在需要刷新配置的地方加上@RefreshScope注解,这样就完成了。当我们更改github上的配置文件之后,GitHub就会触发webhook从而自动刷新配置。

最后提一下如果zuul网关服务也需要自动刷新配置的话可以在启动类加一个方法返回一个ZuulProperties(),当然还要加上@RefreshScope注解

@SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication { public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
} @ConfigurationProperties("zuul")
@RefreshScope
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
}

java微服务的统一配置中心的更多相关文章

  1. spring cloud+dotnet core搭建微服务架构:配置中心(四)

    前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...

  2. spring cloud+dotnet core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

  3. spring cloud+.net core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

  4. spring cloud+.net core搭建微服务架构:配置中心(四)

    前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...

  5. 微服务SpringCloud之配置中心和消息总线

    在微服务SpringCloud之Spring Cloud Config配置中心SVN博客中每个client刷新配置信息时需要post请求/actuator/refresh,但客户端越来越多时,,需要每 ...

  6. 微服务从nacos配置中心获得配置信息

    一,安装nacos, 略 二,创建父工程和微服务工程 service1, service2,以idea为例 1, new -> project -> Maven -> 填写group ...

  7. .NET Core微服务之基于Apollo实现统一配置中心

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.关于统一配置中心与Apollo 在微服务架构环境中,项目中配置文件比较繁杂,而且不同环境的不同配置修改相对频繁,每次发布都需要对应修改 ...

  8. Java微服务(二):服务消费者与提供者搭建

    本文接着上一篇写的<Java微服务(一):dubbo-admin控制台的使用>,上篇文章介绍了docker,zookeeper环境的安装,并参考dubbo官网演示了dubbo-admin控 ...

  9. 多云架构下,JAVA微服务技术选型实例解析

    [摘要] 本文介绍了基于开源自建和适配云厂商开发框架两种构建多云架构的思路,以及这些思路的优缺点. 微服务生态 微服务生态本质上是一种微服务架构模式的实现,包括微服务开发SDK,以及微服务基础设施. ...

随机推荐

  1. 标准ACL详解

  2. 7、vueJs基础知识07

    UI组件库 element-ui和mint-ui 其实都是借鉴了bootstrap bootstrap: 由twitter 开源 简洁.大方 官网文档https://www.bootcss.com/ ...

  3. 采用正则表达式实现startWith、endWith效果函数

    startsWith函数,时Java中的 在js使用时他并不是每个浏览器都有的,所以我们一般要重写一下这个函数 采用正则表达式实现startWith.endWith效果函数 String.protot ...

  4. 解决 screen 连接不上,提示“There is no screen to be resumed matching 18352.” 的问题

    当你挂起screen,下次想重新连上screen时,有时会出现screen session的状态为Attached但是却连不上的情况,比如我想重新进入session id 为18352的screen, ...

  5. Win10安装PyQt5与Qt Designer【转】

    https://blog.csdn.net/u011342224/article/details/78879633 1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 ...

  6. 免费s账号网站

    下面网址按排序顺序优先使用,数字越小优先级越高 1,https://io.freess.today/ 2,https://free-ss.site/ 3,https://ss.freess.org/ ...

  7. 自然语言处理中注意力机制---Attention

    使用Multi-head Self-Attention进行自动特征学习的CTR模型 https://blog.csdn.net/u012151283/article/details/85310370 ...

  8. shell获取今天、明天、昨天、n天、周、月、年日期

    1.获取今天日期 $ date -d now +%Y-%m-%d   或者$ date +%F 1    2 2.获取明天日期 $ date -d next-day +%Y-%m-%d$ date - ...

  9. leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)

    dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...

  10. go cmd nohup 的坑

    https://github.com/go-cmd/cmd/issues/20 golang 的 cmd 包在执行系统命令时,如果使用的到了 nohup 命令, 那么就需要注意, 需要在 nohup ...