六、Config

一、Config简介

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

二、搭建配置中心服务端

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

    <artifactId>config-server</artifactId>
    <description>分布式配置中心服务端</description>

    <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>
</project>
server:
  port: 8767
spring:
  application:
    name: config-server
  cloud:
    # 配置中心的信息
    config:
      server:
        # 使用git进行配置文件的管理
        git:
          # git仓库的地址
          uri: https://gitee.com/mojianshengwei/spirngcloud-config
          # 仓库具体的文件夹路径
          search-paths: config
          # 用户名和密码
          username:
          password:
      # 仓库的分支
      label: master
package com.mo;

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

/**
 * EnableConfigServer 开启配置中心服务端功能
 *
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/15 22:46
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

访问一下地址,读取git仓库的配置文件 

 http://127.0.0.1:8767/config-client.yml

http://127.0.0.1:8767/config/client

 

 三、搭建配置中心客户端

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

    <artifactId>config-client</artifactId>
    <description>配置中心客户端</description>

    <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>

</project>
# bootstrap配置文件为springboot远程加载的配置文件
spring:
  application:
    name: config-client
  cloud:
    config:
      # 远程仓库的分支
      label: master
      # 配置文件的环境,默认环境为default
      profile: default
      # 配置中心的网址
      uri: http://127.0.0.1:8767/
server:
  port: 7566
package com.mo;

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;

/**
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/15 23:18
 */
@RestController
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }


    @Value("${mo.user}")
    String user;

    /**
     * 访问该接口,获取远程git仓库中配置文件的信息
     *
     * @return
     */
    @RequestMapping(value = "/hi")
    public String hi() {
        return user;
    }
}

 

四、搭建集群化配置中心服务端 

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

    <artifactId>config-cluster</artifactId>
    <description>配置中心服务端集群化</description>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

</project>
server:
  port: 0
spring:
  application:
    name: config-cluster
  cloud:
    # 配置中心的信息
    config:
      server:
        # 使用git进行配置文件的管理
        git:
          # git仓库的地址
          uri: https://gitee.com/mojianshengwei/spirngcloud-config
          # 仓库具体的文件夹路径
          search-paths: config
          # 用户名和密码
          username:
          password:
      # 仓库的分支
      label: master
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${random.int}
    prefer-ip-address: true
package com.mo;

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

/**
 * 将配置中心注册到注册中心,进行多集群部署,可以进行负载均衡
 *
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/15 23:24
 */
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClusterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClusterApplication.class, args);
    }
}

 五、搭建配置中心客户端(集群)

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

    <artifactId>config-eureka-client</artifactId>
    <description>配置中心客户端从注册中心获取配置文件</description>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
</project>
# bootstrap.yml 远程配置文件
server:
  port: 0
spring:
  application:
    name: config-eureka-client
  cloud:
    config:
      label: master
      profile: default
      discovery:
        # 开启从注册中心获取配置文件
        enabled: true
        # 配置中心服务的服务名
        service-id: config-cluster
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${random.int}
    prefer-ip-address: true
package com.mo;

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

/**
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/15 23:38
 */
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigEurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigEurekaClientApplication.class, args);
    }

    @Value("${mo.password}")
    String password;

    /**
     * 访问该接口,获取远程git仓库中配置文件的信息
     *
     * @return
     */
    @RequestMapping(value = "/hi")
    public String hi() {
        return password;
    }
}

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值