Spring Cloud编写Config Server

一 介绍
编写一个Config Server。在本例中,使用Git作为Config Server的后端存储。

二 在Git仓库https://gitee.com/cakin24/spring-cloud-config-repo中新建几个配置文件
例如:
microservice-foo.properties
microservice-foo-dev.properties
microservice-froo-test.properties
microservice-froo-production.properties
内容分别为:
profile=default-1.0
profile=dev-1.0
profile=test-1.0
profile=production-1.0
为了测试版本控制,为该Git仓库创建Config-label-v2.0分支,并将各个配置文件中的1.0改为2.0

三 创建一个Maven工程,工程名为microservice-config-server,并为项目添加以下的依赖
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  </dependencies>
四 编写启动类,并在启动类上添加注解@EnableConfigServer,声明这是一个Config Server
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}
五 编写配置文件,并在配置文件中增加以下内容
server:
  port: 8080
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri:https://git.oschina.net/cakin24/spring-cloud-config-repo# 配置Git仓库的地址
          username:                                                         # Git仓库的账号
          password:                                                         # Git仓库的密码
六 Config Server的端口
可以使用Config Server的端点获取配置文件的内容。端点与配置文件的映射规则如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
以上端点都可以映射到{application}-{profile}.properties这个配置文件,{application}表示微服务的名称,{label}对应Git仓库的分支,默认是master。
按照以上规则,对于本例,可使用以下URL访问到Git仓库master分支的microservice-foo-dev.properties。
七测试
从结果可以直观地看到应用名称、项目profile、Git label、Git version、配置文件URL、配置详情等信息。
八 注意
访问 http://localhost:8080/microservice-foo/dev,结果中类似 https://git.oschina.net/cakin24/spring-cloud-config-repo/microservice-foo-dev.properties的URL并不能访问。这是正常的,因为它并不代表配置文件的实际URL路径,而只是一个标识。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config Server是一个用于集中管理和提供应用程序配置的服务器。它可以从Git仓库中获取配置文件,并将其提供给客户端应用程序。配置服务器的配置可以在服务端的配置文件中进行设置,如引用\[1\]所示。其中,可以指定Git仓库的URI、搜索路径、分支等信息。 客户端应用程序需要在bootstrap.properties或bootstrap.yml文件中配置相关信息,如引用\[2\]所示。其中,需要指定配置服务器的URI、应用程序名称、激活的配置文件和分支等。 为了集成Config Server,需要在项目的依赖中添加spring-cloud-config-server的依赖项,如引用\[3\]所示。同时,需要编写一个启动类,并使用@EnableConfigServer注解来启用Config Server功能。 在启动类中,可以设置Config Server的配置,如Git仓库的地址、账号和密码等。 启动Config Server后,可以通过访问http://127.0.0.1:8080/{application}/{profile}.yaml来获取相应的配置文件。其中,{application}是应用程序名称,{profile}是激活的配置文件。 以上是关于Spring Cloud Config Server的简要介绍和配置方法。如果您有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *2* [Spring Cloud - 2 (Spring Cloud Config Server)](https://blog.csdn.net/ysl19910806/article/details/96755134)[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] - *3* [Spring Cloud配置中心之Config Server](https://blog.csdn.net/mg0324/article/details/122635762)[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、付费专栏及课程。

余额充值