• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
isuning
博客园    首页    新随笔    联系   管理    订阅  订阅
Spring Cloud Config客户端使用

快速开始

本快速入门介绍了同时使用 Spring Cloud Config Server 的服务器和客户端。

HTTP 服务具有以下形式的资源:

/{应用程序}/{配置文件}[/{标签}]
/{应用程序}-{配置文件}.yml
/{label}/{application}-{profile}.yml
/{应用程序}-{profile}.properties
/{label}/{application}-{profile}.properties

例如:

卷曲 localhost:8888/foo/development
curl localhost:8888/foo/development/master
curl localhost:8888/foo/development,db/master
curl localhost:8888/foo-development.yml
卷曲 localhost:8888/foo-db.properties
curl localhost:8888/master/foo-db.properties

whereapplication被注入(通常spring.config.name在常规 Spring Boot 应用程序中),是一个活动配置文件(或逗号分隔的属性列表),并且是一个可选的 git 标签(默认为.)SpringApplication``application``profile``label``master

Spring Cloud Config Server 从各种来源拉取远程客户端的配置。以下示例从 git 存储库(必须提供)获取配置,如下例所示:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

其他来源是任何 JDBC 兼容的数据库、Subversion、Hashicorp Vault、Credhub 和本地文件系统。

客户端使用

要在应用程序中使用这些功能,您可以将其构建为依赖于 spring-cloud-config-client 的 Spring Boot 应用程序(例如,请参阅 config-client 的测试用例或示例应用程序)。添加依赖项最方便的方法是使用 Spring Boot starter org.springframework.cloud:spring-cloud-starter-config。还有一个spring-cloud-starter-parent用于 Maven 用户的父 pom 和 BOM ( ) 以及一个用于 Gradle 和 Spring CLI 用户的 Spring IO 版本管理属性文件。以下示例显示了一个典型的 Maven 配置:

pom.xml

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>{spring-boot-docs-version}</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>{spring-cloud-version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

<build>
	<plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
	</plugins>
</build>

   <!-- repositories also needed for snapshots and milestones -->

现在您可以创建一个标准的 Spring Boot 应用程序,例如以下 HTTP 服务器:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

当这个 HTTP 服务器运行时,它会从端口 8888 上的默认本地配置服务器(如果它正在运行)中获取外部配置。要修改启动行为,您可以使用application.properties如下所示更改配置服务器的位置例子:

spring.config.import=可选:configserver:http://myconfigserver.com

默认情况下,如果未设置应用程序名称,application将使用。要修改名称,可以将以下属性添加到application.properties文件中:

spring.application.name:myapp

Config Server 属性在/env端点中显示为高优先级属性源,如以下示例所示。

$ curl 本地主机:8080/env
{
  “活动配置文件”:[],
  {
    "名称": "servletContextInitParams",
    “特性”: {}
  },
  {
    “名称”:“配置服务器:https://github.com/spring-cloud-samples/config-repo/foo.properties”,
    “特性”: {
      “富”:{
        “价值”:“酒吧”,
        “起源”:“配置服务器 https://github.com/spring-cloud-samples/config-repo/foo.properties:2:12”
      }
    }
  },
  ...
}

一个名为的属性源configserver:<URL of remote repository>/<file name>包含foo值为的属性bar。

posted on 2022-05-01 22:17  吕布辕门  阅读(58)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3