spring cloud 配置中心(本地)

spring cloud 配置中心(本地)

配置中心(服务端)

该项目基于spring cloud Edgware.SR5 版本,目录结构如下:

  1. pom文件加入以下配置

    <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>
    
  2. application.properties

    # 端口
    server.port=8888
    
    # 应用名
    spring.application.name=config-server-local
    
    # 本地配置文件配置
    spring.config.name=config-server
    spring.profiles.active=native
    spring.cloud.config.server.native.search-locations=classpath:/config
    
    # 注册到eureka注册中心,以符合spring cloud服务治理的理念
    eureka.client.register-with-eureka=true
    eureka.client.fetch-registry=true
    eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
    
    # 去掉端点安全认证
    management.security.enabled=false
    
  3. config/*配置各个服务的配置信息

  4. 添加@EnableConfigServer注解

客户端

  1. pom.xml

    <!-- 客户端 -->
    <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>
    
  2. application.properties

    spring.application.name=consumer-server
    spring.profiles.active=dev
    server.port=8081
    server.context-path=/consumer
    
    # eureka注册中心地址
    eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
    
    # 配置中心
    spring.cloud.config.name=consumer-server
    spring.cloud.config.profile=dev
    # 使用配置中心服务名,而非url,以便符合服务治理理念
    #spring.cloud.config.uri=http://localhost:8888
    spring.cloud.config.discovery.enabled=true
    spring.cloud.config.discovery.service-id=config-server-local
    management.security.enabled=false
    
  3. controller

     @RefreshScope	// 实现刷新配置
     @RestController
     @RequestMapping("/fileConfig")
     public class FileConfigController {
     
     	@Value("${consumer.server.welcome:default value}")
     	private String foo;
    
         /**
          * 读取配置中心的配置
          * @return
          */
         @RequestMapping("/welcome")
         public String sayWelcome(){
             return foo;
         }
     }
    
  4. 测试
    依次启动注册中心,配置中心,客户端服务
    访问配置中心( http://localhost:8888/consumer-server/dev )查看配置如下:
    在这里插入图片描述
    访问客户端( http://localhost:8081/consumer/fileConfig/welcome ),查看是否能读取到配置
    在这里插入图片描述

热更新

修改配置中心 consumer.server.welcome=hello,i am dev env update,重启配置中心后,使用 post 刷新客户端 http://localhost:8081/consumer/refresh
在这里插入图片描述
再次请求 http://localhost:8081/consumer/fileConfig/welcome发现不需重启客户端就能实现配置刷新
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值