SpringCloud的搭建6_分布式配置中心(Spring Cloud Config)

11 篇文章 0 订阅
1 篇文章 0 订阅

分布式配置中心(Spring Cloud Config)

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、构建Config Server

创建父maven工程省略

1、首先创建一个ConfigServer工程。

pom.xml

<?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>EurekaProject</artifactId>
        <groupId>com.jiawen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ConfigServer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 1217
spring:
  application:
    name: config-server
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://gitlab.itophis.com/***/***
          searchPaths: config
          force-pull: true
          username: ***
          password: ***

ConfigServerApplication.java类

package com.jiawen;

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);
    }
}

在gitlab上面创建一个工程,然后在application.yml中配置url为gitlab的工程目录。
label中设置你的分支,本次我实在master分支创建的,所以用的master分支。
如果是私服那么输入你的gitlab账号和密码,如果是公开的是不需要输入账号密码的。

配置好后就可以启动项目了。

访问路径:http://localhost:1217/foo/dev
在这里插入图片描述

说明已经访问到了git上面的配置了。那么现在我们继续创建一个客户端进行配置信息的获取。

2、创建一个ConfigClient工程。

工程目录如下:
在这里插入图片描述
pom.xml

<?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>EurekaProject</artifactId>
        <groupId>com.jiawen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

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

</project>

bootstrap.properties

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:1217/
server.port=1218

ConfigClentApplication.java

package com.jiawen;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ConfigClentApplication {

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

    }

    @Value("${foo}")
    String foo;

    @RequestMapping("hi")
    public String hi(){
        return foo;
    }
}

配置好启动ConfigServer服务后再启动ConfigClient服务。

gitlab上配置了配置文件
在这里插入图片描述
配置文件中编辑了以下内容

foo=foo version 3

访问路径: http://localhost:1218/hi
在这里插入图片描述
特别注意:文件配置的映射

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

首先就是你想给哪一个服务配置那么配置文件的前缀就是服务名,例如本次的ConfigClient就是客户端的服务名,那么再给他创建配置文件时,就是config-client-***.properties,接下来就是***部分就是那个环境,如果是dev那就在spring.cloud.config.profile=dev配置成dev就可以了。

那么到这里就结束了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值