学习Spring Cloud(2)之Spring Cloud Config服务端

学习Spring Cloud(1)之Spring Cloud 项目搭建

简介

Spring Cloud Config为分布式系统中的外部化配置提供服务器端客户端支持。
服务器端Config Server,可以在中心位置管理所有环境中应用程序的外部属性。在应用程序从开发人员到测试人员再到生产人员的整个部署过程中,可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git。

Spring Cloud Config Server

Spring Cloud Config Server为外部配置(名称-值对或等效的YAML内容)提供了一个基于HTTP资源的API。通过使用**@EnableConfigServer**注解,该服务器可嵌入到Spring Boot应用程序中。

在[《学习Spring Cloud(1)之Spring Cloud 项目搭建》]搭建的项目基础上,增加Spring Cloud Config Server配置:

1、将src文件夹删掉,在项目中右键,选择New-Module创建一个子项目
在这里插入图片描述
2、依然选择maven项目,next
在这里插入图片描述
3、填写ArtifactId和Version,next
在这里插入图片描述
4、填写项目名称、位置,finish
在这里插入图片描述
5、修改pom.xml文件,引入Spring Cloud Config

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>xx-cloud</artifactId>
        <groupId>cn.dyx.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>xx-cloud-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

</project>

6、增加启动类
通过使用@EnableConfigServer批注,该服务器可嵌入到Spring Boot应用程序中。

@EnableConfigServer
@SpringBootApplication
public class XxConfigApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(XxConfigApplication.class);
    }

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

}

继承SpringBootServletInitializer可以使用传统war包启动。
7、配置application.properties

server.port=1002
spring.application.name=system-config-server
#native访问本地配置 dev 打开git配置
spring.profiles.active=native
#本地环境配置
spring.cloud.config.server.native.search-locations=classpath:/shared
#配置git仓库的地址
#spring.cloud.config.server.git.uri=
#spring.cloud.config.server.git.default-label=
#git仓库地址下的相对地址,可以配置多个,用,分割。
#spring.cloud.config.server.git.search-paths=

项目结构:
在这里插入图片描述
至此,Spring Cloud Config Server 服务器端搭建完成。

理论知识

环境库

您要在哪里存储配置服务器的配置数据?管理此行为的策略是EnvironmentRepository,服务于Environment对象。这个Environment是Spring Environment(包括propertySources作为主要功能)的域的浅层副本。Environment资源由三个变量参数化:
{application}映射到客户端的“spring.application.name”;
{profile}映射到客户端上的“spring.profiles.active”(逗号分隔列表); 和
{label}这是一个服务器端功能,标记“版本”的一组配置文件。
存储库实现通常表现得像一个Spring Boot应用程序从“spring.config.name”等于{application}参数加载配置文件,“spring.profiles.active”等于{profiles}参数。配置文件的优先级规则也与常规启动应用程序中的相同:活动配置文件优先于默认配置,如果存在多个配置文件,则最后一个配置文件(例如向Map添加条目))。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值