Spring Cloud(Finchley.RCI) (九) Spring Cloud分布式配置中心

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

分布式配置中心服务端

创建一个工程名为hello-spring-cloud-config的项目, 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-config</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-config</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>

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

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

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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>
                <configuration>
                    <mainClass>com.funtl.hello.spring.cloud.config.ConfigApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Application

通过@EnableConfigServer注解, 开启配置服务功能

package com.funtl.hello.spring.cloud.config;

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

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

application.yml

增加Config相关配置, 并设置端口号为: 8888

spring:
  application:
    name: hello-spring-cloud-config
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://github.com/topsale/spring-cloud-config
          search-paths: respo
          username:
          password:
server:
  port: 8888
eureka:
  client:
    serviceUrl:
      default: http://localhost:8761/eureka

相关配置说明, 如下:

spring.cloud.config.label: 配置仓库的分支

spring.cloud.config.server.git.uri: 配置Git仓库的地址

spring.cloud.config.server.git.search-paths: 配置仓库路径

spring.cloud.config.server.git.username: 访问Git仓库的账号

spring.cloud.config.server.git.password: 访问Git仓库的密码

注意事项:

如果使用GitLab作为仓库的话, git.uri需要在结尾加上.git, GitHub则不用

测试

浏览器访问: http://localhost:8888/config--client/dev/master

分布式配置中心客户端

创建一个工程名为hello-spring-cloud-config-client的项目, pom.xml文件配置如下:

Application.yml

增加Config Client相关配置, 并设置端口号为: 8889

cloud:
  config:
    uri: http://localhost:8888
    name: web-admin-feign
    label: master
    profile: dev
发布了69 篇原创文章 · 获赞 8 · 访问量 9414

猜你喜欢

转载自blog.csdn.net/u011414629/article/details/101416196
今日推荐