springboot-cloud-6-config

本文介绍如何使用 Spring Cloud Config 实现配置中心,并通过实战案例展示了如何进行配置的集中管理和动态刷新。文章涵盖配置中心的基本设置、客户端接入流程及故障响应机制。

config-server

application.properties
server.port=8005
spring.application.name=config-server

###git仓库地址
spring.cloud.config.server.git.uri= https://github.com/github-ygy/spring_cloud_test
####git 相对目录地址  多个,分开
spring.cloud.config.server.git.search-paths=cloud-config-repository

###github public 不用配置username 和 password
#spring.cloud.config.server.git.username =
#spring.cloud.config.server.git.password=
application
@SpringBootApplication
@EnableConfigServer  //开启config服务
public class ConfigMasterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigMasterApplication.class, args);
    }
}
输出
http://localhost:8005/test/prd
{
  "name": "test",
  "profiles": [
    "prd"
  ],
  "label": null,
  "version": "16610aaf21ae2e0934ec3522009ef993829ec086",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/github-ygy/spring_cloud_test/cloud-config-repository/test-prd.properties",
      "source": {
        "from": "git-prd-1.0"
      }
    }
  ]
}
http://localhost:8005/test/prd/dev_ygy
{
  "name": "test",
  "profiles": [
    "prd"
  ],
  "label": "dev_ygy",
  "version": "0978a00b06877b63a89653bea4ec0ac8df779df9",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/github-ygy/spring_cloud_test/cloud-config-repository/test-prd.properties",
      "source": {
        "from": "git-prd-2.0"
      }
    }
  ]
}

config-client

bootstrap.properties
#######必须使用bootstrap.properties  使用application.properties 将无法解析@Value属性
#######bootstrap.propeties 会优先于application.properties加载
server.port=8006

####配置中心git仓库 文件名   与<application> 对应
spring.application.name = test
#####配置中心git仓库 文件名 启动配置文件标识  与<profile> 对应
spring.cloud.config.profile = dev
####配置中心git仓库分支名    与<lable> 对应
spring.cloud.config.label=dev_ygy

####配置中心config server地址
spring.cloud.config.uri=http://localhost:8005/
controller
@RestController
public class ConfigClientController {


    @Value("${from}")
    private String from ;

    @RequestMapping("/getConfig")
    public String getConfig() {

        return this.from;
    }
}
输出
http://localhost:8006/getConfig
git-dev-2.0

config-eureka(整合)

失败响应机制/自动刷新
依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
bootstrap.properties


spring.application.name = config-client

#######必须使用bootstrap.properties  使用application.properties 将无法解析@Value属性
#######bootstrap.propeties 会优先于application.properties加载
server.port=8006
###eureka管理
eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka/
spring.cloud.config.discovery.enabled = true

### 失败响应
spring.cloud.config.fail-fast=true
####引入aop  retry依赖
####最大重试次数 默认6次
spring.cloud.config.retry.max-attempts= 6
####最大重试时间间隔
spring.cloud.config.retry.max-interval= 2000
#####初始重试间隔 默认1000
spring.cloud.config.retry.initial-interval = 1500
##### 下一次间隔系数
spring.cloud.config.retry.multiplier= 1.2

####开启自动刷新  引入actuator 依赖




####  感觉有bug  待后续解决 属性注册不成功,将config-server 项目注册为默认 configserver可行。
#spring.cloud.config.discovery.serviceid = config-server

####关闭授权
management.security.enabled=false


####配置中心git仓库 文件名   与<application> 对应
spring.cloud.config.name = test
#####配置中心git仓库 文件名 启动配置文件标识  与<profile> 对应
spring.cloud.config.profile = dev
####配置中心git仓库分支名    与<lable> 对应
spring.cloud.config.label=dev_ygy

####配置中心config server地址
###spring.cloud.config.uri=http://localhost:8005/

controller
@RestController
@RefreshScope
public class ConfigClientController {


    @Value("${from}")
    private String from ;

    @RequestMapping("/getConfig")
    public String getConfig() {

        return this.from;
    }
}
application
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
        //new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);
    }
}
post请求刷新
public class PostRquest {

    public static void main(String[] args) throws Exception {

        HttpResponse<String> jsonResponse = Unirest.post("http://localhost:8006/refresh")
                .asString();
        System.out.println(jsonResponse.getBody());
    }
}
输出
//http://localhost:8006/getConfig
git-dev-2.0

// post request
["config.client.version","from"]

//http://localhost:8006/getConfig
git-dev-2.0-refresh
### 作用 - **服务标识**:在微服务架构中,`spring.application.name` 用于唯一标识一个服务。当配置为 `springboot - practice` 时,它可以帮助服务发现组件(如 Eureka、Consul 等)识别该服务,使得其他服务能够通过这个名称来调用它。例如,在一个由多个微服务组成的系统中,不同的服务可能会向服务注册中心注册自己,`spring.application.name` 就作为服务的标识符,方便服务之间的相互调用和管理。 - **日志和监控**:在日志记录和监控系统中,`spring.application.name` 可以作为一个重要的标识。当配置为 `springboot - practice` 时,在日志文件或者监控界面中,就可以清晰地知道这些日志和监控数据是来自哪个服务,便于问题的排查和系统的维护。 ### 配置方法 - **application.properties**:在 `src/main/resources` 目录下的 `application.properties` 文件中添加如下配置: ```properties spring.application.name=springboot-practice ``` - **application.yml**:如果使用 YAML 格式的配置文件,在 `src/main/resources` 目录下的 `application.yml` 文件中添加如下配置: ```yaml spring: application: name: springboot-practice ``` - **命令行参数**:在启动 Spring Boot 应用时,可以通过命令行参数来配置 `spring.application.name`: ```bash java -jar your-application.jar --spring.application.name=springboot-practice ``` - **环境变量**:也可以通过设置环境变量来配置,例如在 Linux 系统中: ```bash export SPRING_APPLICATION_NAME=springboot-practice java -jar your-application.jar ``` ### 使用场景 - **微服务架构**:在微服务架构中,每个服务都需要有一个唯一的名称。当 `spring.application.name` 配置为 `springboot - practice` 时,该服务可以向服务注册中心(如 Eureka、Consul)注册自己,其他服务可以通过服务名称来调用它。例如,一个订单服务需要调用 `springboot - practice` 服务来获取相关数据,就可以通过服务名称进行调用。 - **配置管理**:在使用配置中心(如 Spring Cloud Config)时,`spring.application.name` 可以作为配置文件的一部分。例如,配置中心可能会有一个名为 `springboot-practice.properties` 或 `springboot-practice.yml` 的配置文件,Spring Boot 应用会根据 `spring.application.name` 来加载对应的配置- **日志和监控**:在日志系统和监控系统中,`spring.application.name` 可以帮助区分不同服务的日志和监控数据。例如,在 ELK(Elasticsearch、Logstash、Kibana)日志系统中,可以根据 `spring.application.name` 来过滤和分析不同服务的日志。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值