springCloud微服务组件:配置中心(config)

springCloud微服务组件:配置中心(config)

概念:统一管理各个模块的配置文件,将各个模块的配置文件放进配置中心,各个模块只需要连接这个配置中心,在配置中心中更改配置文件,各个模块的配置也会相对应的修改。

1.建立config-server配置中心模块连接gitee远程仓库

导包

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

编写启动类

@SpringBootApplication
@EnableConfigServer //启用config server 功能
public class ConfigApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApp.class,args);
    }
}

编写yml配置文件

server:
  port: 8888 # 配置中心默认端口

spring:
  application:
    name: CONFIG-SERVER

  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/gwx1998/config-server.git #gitee远程仓库的地址,注意要用http的地址
      label: master # 远程仓库分支名

通过地址栏访问配置中心 http://localhost:8888/master/config-dev.yml,需要指定分支名加上配置文件的名字

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qb50jXiA-1598856213550)(C:\Users\gwx\AppData\Roaming\Typora\typora-user-images\image-20200831114331633.png)]

2.编写配置中心对应的客户端模块

导包

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

编写bootstrap.yml配置文件

spring:
  cloud:
    config:
      uri: http://localhost:8888 # 配置配置中心的地址
      label: master # 指定要连接配置中心远程仓库的具体分支
      name: config  # 指定要连接配置中心远程仓库的具体分支配置文件名称
      profile: dev # 指定要连接配置中心远程仓库的具体分支配置文件名称的环境,dev表示开发环境的配置

测试:通过这个客户端是否可以获得配置中心的配置

客户端刷新:如果配置中心对应的远程仓库配置文件发生改变,继续访问配置中心值也会跟着改变,但是客户端连接着配置中心,客户端获得的配置信息不会发生改变,如果想事实的发生改变要不就重启客户端,或者加上一些配置

导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

哪个地方需要配置信息,就在哪个类上加上注解@RefreshScope

在bootstrap.yml配置文件中加上自动刷新客户端的配置

management:
  endpoints:
    web:
      exposure:
        include: '*'

最后在windows命令窗口发一个post请求就可以自动刷新客户端的配置信息

curl -X POST http://localhost:9000/actuator/refresh

客户端动态的获取配置中心的地址,将配置中心注册到nacos中,再将客户端的配置文件修改成从注册中心获取配置中心的地址

配置中心配置:

导包

<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>0.2.2.RELEASE</version>
</dependency>

修改配置文件

nacos:
  discovery:
    server-addr: 127.0.0.1:8848

客户端:

修改配置文件

spring:
  cloud:
    config:
#      uri: http://localhost:8888 # 配置配置中心的地址
      label: master # 指定要连接配置中心远程仓库的具体分支
      name: config  # 指定要连接配置中心远程仓库的具体分支配置文件名称
      profile: dev # 指定要连接配置中心远程仓库的具体分支配置文件名称的环境,dev表示开发环境的配置d
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
management:
  endpoints:
    web:
      exposure:
        include: '*'

scovery:
enabled: true
service-id: CONFIG-SERVER
management:
endpoints:
web:
exposure:
include: ‘*’


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值