consul做配置中心

1. 分布式配置中心

consul不仅可做注册中心,还可做配置中心
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
applicaiton.yml与bootstrap.yml:

  • applicaiton.yml是用户级的资源配置项
  • bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment

2. 添加依赖

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

3. 新增bootstrap.yml

spring:
  application:
    name: cloud-payment-service
    ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
      config:
        profile-separator: '-' # default value is ",",we update '-'
        format: YAML

# config/cloud-payment-service/data
#       /cloud-payment-service-dev/data
#       /cloud-payment-service-prod/data

修改application.yml

server:
  port: 8001

# ==========applicationName + druid-mysql8 driver===================
spring:
#  application:
#    name: cloud-payment-service

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2024?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    username: root
    password: root

  profiles:
    active: dev  # 多环境配置加载内容dev/prod,不写就是默认default配置

  ####Spring Cloud Consul for Service Discovery
#  cloud:
#    consul:
#      host: localhost
#      port: 8500
#      discovery:
#        service-name: ${spring.application.name}

# ========================mybatis===================
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.hong.entity
  configuration:
    map-underscore-to-camel-case: true

4. consul服务器k/v配置填写

在这里插入图片描述
在这里插入图片描述
配置文件首层目录名必须为config,目录后面带/.官网默认分割方式为**,本文改为-**
最底层文件名必须为data,不带/

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5. 测试获取consul中的配置

PayController中添加如下代码

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/getConfigInfoByConsul")
    @Operation(summary = "测试查询consul配置信息",description = "测试查询consul配置信息")
    public String getConfigInfoByConsul(@Value("${payment8001.info}") String consulInfo){
        return "consulInfo: " + consulInfo +"\t"+ " port: " + port;
    }

启动后
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6. 动态刷新

主启动类上添加@RefreshScope注解

package com.hong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @author hong
 * @date 2020/1/14
 * @description 支付模块v1启动类
 */
@SpringBootApplication
@MapperScan("com.hong.mapper")
@EnableDiscoveryClient
@RefreshScope
public class Main8001 {
    public static void main(String[] args) {
        SpringApplication.run(Main8001.class,args);
    }
}

在这里插入图片描述

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Consul作为配置中心有两个主要功能。首先,它可以作为注册中心,用于服务的注册和发现。其次,它可以作为配置中心,用于管理应用程序的配置信息。在使用Consul作为配置中心时,有一些需要注意的地方。通过@Value注入的属性,在修改Consul的配置后,属性不会立即生效,需要重启服务才能生效。而通过@ConfigurationProperties注入的属性,在修改Consul的配置后,属性会立即生效。因此,如果需要动态生效的配置,建议使用@ConfigurationProperties进行属性的注入。\[1\] 对于多个环境的情况,Consul作为配置中心也是支持的。它可以将原来使用的bootstrap.yml、application.yml、application-dev.yml、application-test.yml等配置文件迁移到Consul的内存中,并且取值的优先级比这些配置文件更高。因此,可以根据不同的环境在Consul中配置相应的属性值,实现多环境的配置管理。\[2\] 关于Consul作为配置中心的更多详细信息,可以参考官方文档提供的属性列表\[3\]。 #### 引用[.reference_title] - *1* [Consul3-使用consul作为配置中心](https://blog.csdn.net/wj903829182/article/details/81050507)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [consul作为配置中心](https://blog.csdn.net/lushuaiyin/article/details/109729499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值