-------------------------目录------------------------------
一、配置中心应用(Git)
二、配置中心的手动刷新
三、配置中心的自动刷新(Spring Cloud Confifig+Spring Cloud Bus 实现)
四、配置中心应用(数据库)
-------------------------------------------------------

一、配置中心应用(Git)

Spring Cloud Config是⼀个分布式配置管理⽅案,包含了 Server端和 Client端两个部分。Config Server是集中式的配置服务,⽤于集中管理应⽤程序各个环境下的配置。 默认使⽤Git存

储配置⽂件内容,也可以SVN和数据库,在配置的时候也可以区分开发环境,测试环境和生产环境
配置前说明:

  1、如果在GitHub上建立的仓库是私有的,那么还要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 这两个配置

  2、springcloud config 的URL与配置文件的映射关系如下:

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

  3、如果github上建立的目录下的文件为application-config-dev.yml,那么当启动配置中心服务器端时,可以通过http://localhost:9006/config/application-config-dev.yml访问配置文件,如果访问成功则表示配置中心搭建成功。这里的config是分支名称

第一步:配置中心服务端

  1、依赖配置

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

  2、配置文件

spring:
application:
#应⽤名称,会在Eureka中作为服务的id标识(serviceId)
name: config-server9006
cloud:
config:
server:
git:
uri: git@gitee.com:niunafei1/springcloud.git
username: niunafei0315@163.com
password:
label: config
eureka:
client:
#eureka server的路径
serviceUrl:
#注册单实例只需要写一台服务器即可
#集群模式下,也需要写其它 http://Server其他服务地址:其他服务端口/eureka,如果多个服务需要使用逗号分隔
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
#使⽤ip注册,否则会使⽤主机名注册了(此处考虑到对⽼版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
#⾃定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@

  3、在启动类上添加注解,@EnableDiscoveryClient【@EnableDiscoveryClient注解可替换为@EnableEurekaClient】和@EnableConfigServer

package city.albert;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/9/22 11:44 PM
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServer9006 { public static void main(String[] args) {
SpringApplication.run(ConfigServer9006.class, args);
}
}

访问:http://localhost:9006/config/application-config-dev.yml

第二步:配置中心客户端

  1、引入依赖

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

   2、配置文件设置,文件名为bootstrap.yml

   在spring boot中系统文件名bootstrap.yml优先被加载使用,需要在系统加载application.yml之前初始化获取配置文件。

spring:
application:
#应⽤名称,会在Eureka中作为服务的id标识(serviceId)
name: gateway-server9002
cloud:
config:
name: application-config #配置⽂件名称
profile: dev #后缀名称
label: config #分⽀名称
uri: http://localhost:9006 #ConfigServer配置中⼼地址

  3、使用可以用@Value("${spring.port}")注入即可

二、配置中心的手动刷新

  实现手动刷新不⽤重启微服务,只需要⼿动的做⼀些其他的操作(访问⼀个地址/refresh)刷新,之后再访问即可此时,客户端取到了配置中⼼的值,但当我们修改GitHub上⾯的值时,服务端(Confifig Server)能实时获取最新的值,但客户端(Confifig Client)读的是缓存,⽆法实时获取最新值。Spring Cloud已 经为我们解决了这个问题,那就是客户端使⽤post去触发refresh,获取最新数据。

  1)Client客户端添加依赖springboot-starter-actuator(已添加)
        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  2)Client客户端bootstrap.yml中添加配置(暴露通信端点),依赖actuator中的对外暴露接口
management:
endpoints:
web:
exposure:
include: "*"
  3)Client客户端使⽤到配置信息的类上添加@RefreshScope
  4)⼿动向Client客户端发起POST请求,http://localhost:8080/actuator/refresh,刷新配置信息
注意:⼿动刷新⽅式避免了服务重启(流程:Git改配置—>for循环脚本⼿动刷新每个微服务)

三、配置中心的自动刷新(Spring Cloud Confifig+Spring Cloud Bus 实现)

   MQ消息代理,我们还选择使⽤RabbitMQ,ConfifigServer和ConfifigClient都添加都消息总线的⽀持以及与RabbitMq的连接信息

  1)Confifig Server服务端添加消息总线⽀持(config服务端和客户端),因为需要依赖RabbitMQ。
        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>  
  2)ConfifigServer添加配置(config服务端和客户端)
spring:
rabbitmq:
host: 127.0.0.1
password: guest
port: 5672
username: guest
  3)微服务暴露端⼝服务(config服务端)
  a、引入依赖:
        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  b、暴露服务

management:
endpoints:
web:
exposure:
include: "*"
  4)Client客户端使⽤到配置信息的类上添加@RefreshScope 
    5)重启各个服务
  更改配置之后:
  全部实力生效:向配置中⼼服务端发送post请求http://localhost:9003/actuator/bus-refresh,各个客户端配置即可⾃动刷新。
  定向某个服务生效:在发起刷新请求的时候http://localhost:9006/actuator/bus-refresh/lagou-service-resume:8081,即为最后⾯跟上要定向刷新的实例的服务名:端⼝号即可

四、配置中心应用(数据库)

  以下显示基于“一、配置中心应用(Git)”上的改动
  1、引入数据库依赖
        <!--连接msql数据库相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

  2、改动配置文件

spring:
application:
name: config-server-jdbc
profiles:
active: jdbc
cloud:
config:
server:
default-label: dev
jdbc:
sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=?
# mysql 属性配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root

这里主要讲下连接配置信息

(1)spring.profiles.active=jdbc,自动实现JdbcEnvironmentRepository。

(2)sql语句自定义,否则会默认为“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具体可以参考JdbcEnvironmentRepository实现。

(3)本人数据库建表为config_server,由于key,value和profile是mysql关键字,所以我都在最前面加了a。当然表名字段名都可以自定义。

(4) {application} 对应客户端的"spring.application.name"属性;

{aprofile} 对应客户端的 "spring.profiles.active"属性(逗号分隔的列表); 和

{label} 对应服务端属性,这个属性能标示一组配置文件的版本.

(5)只要select出来是两个字段,框架会自动包装到environment的map<key,value>。

SpringCloud之配置中心(config)的使用Git+数据库实现的更多相关文章

  1. 七、springcloud之配置中心Config(二)之高可用集群

    方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...

  2. SpringCloud分布式配置中心Config

    统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...

  3. 六、springcloud之配置中心Config

    一.配置中心提供的核心功能 Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对S ...

  4. springcloud(四):应用配置中心config的安全设置

    springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文 ...

  5. Spring-cloud微服务实战【九】:分布式配置中心config

      回忆一下,在前面的文章中,我们使用了spring cloud eureka/ribbon/feign/hystrix/zuul搭建了一个完整的微服务系统,不管是队内还是对外都已经比较完善了,那我们 ...

  6. SpringCloud配置中心config

    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...

  7. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  8. SpringCloud 分布式配置中心

    SpringCloud 分布式配置中心 服务端 创建工程并完善结构 国际惯例,把maven工程创建完善 pom.xml <?xml version="1.0" encodin ...

  9. (七)Spring Cloud 配置中心config

      spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...

随机推荐

  1. 利用Visual Studio调试JavaScript脚本

    方法1: 方法2: 打开IE,按F12调试. 方法3: JS断电点debugger代替

  2. 将MacOS Catalina 降级为 Mojave

    1.下载Mojave https://apps.apple.com/cn/app/macos-mojave/id1398502828?ls=1&mt=12 2.更改U盘格式和名称 3.制作U盘 ...

  3. MySQL数据库插入数据出现 ERROR 1526 (HY000): Table has no partition for value xxx

    MySQL数据库插入数据出现ERROR 1526 (HY000): Table has no partition for value xxx工作的时候发现无法插入数据,报错:ERROR 1526 (H ...

  4. Java 集合框架体系总览

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  5. WPF 应用 - WPF 播放 GIF 的两种方式

    1. 使用 Winform 的 PictureBox 1.1 引用 dll WindowsFormsIntegration.dll System.Windows.Forms.dll System.Dr ...

  6. url里bookmark是什么意思

    <a rel="bookmark" href="abc.com"> 点击查看 </a> rel 这个属性的全称是  relationsh ...

  7. vue+lib-flexible实现大小屏幕,超大屏幕的适配展示。

    p.p1 { margin: 0; font: 12px "PingFang SC" } span.s1 { font: 12px "Helvetica Neue&quo ...

  8. 1-认识c指针

    1.指针和内存 c程序在编译后,会以三种形式使用内存 1静态/全局内存 静态声明的变量分配在这里,全局变量也使用这部分内存.这些变量在程序开始运行时分配,直到程序终止时才会消失 2.自动内存 这些变量 ...

  9. 《Selenium自动化测试实战:基于Python》之 Python与Selenium环境的搭建

    第2章  Python与Selenium环境的搭建 购买链接:  京东:https://item.jd.com/13123910.html  当当:http://product.dangdang.co ...

  10. Asp.Net Core 学习随笔

    1.依赖注入 configureServices 中 //单例 services.AddSingleton<i,c>(); //http请求内 services.AddScopend< ...