SpringCloud 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

Posted Eddie.Lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud 第七篇: 高可用的分布式配置中心(Spring Cloud Config)相关的知识,希望对你有一定的参考价值。

前言:

必需学会SpringBoot基础知识

简介:

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。

工具:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2018.1 x64

 

上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:

                        SpringCloud_配置中心777

 

一、准备工作

继续使用上一篇文章的工程,创建一个 eureka-config-server-cluster Project,用作服务注册中心。

在 pom.xml File 引入 Eureka 的起步依赖 spring-cloud-starter-eureka-server,代码如下:

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.lwc</groupId>
	<artifactId>eureka-config-server-cluster</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-config-server-cluster</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR3</spring-cloud.version>
	</properties>

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

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

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

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


</project>

在 application-dev.yml ,指定服务端口为8889,配置服务注册中心,代码如下:

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类:

package com.lwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author Eddie
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaConfigServerClusterApplication {

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

 

二、修改 eureka-config-server

在 pom.xml File 引入 Eureka 的起步依赖 spring-cloud-starter-eureka-server,代码如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lwc</groupId>
    <artifactId>eureka-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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-eureka</artifactId>
        </dependency>
    </dependencies>

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

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


</project>

配置文件application-dev.yml,指定服务注册地址为http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下:

server:
  port: 8888
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/eddie-code/SpringCloudDemo
          search-paths: config-repo
          username:
          passphrase:
      label: master

并且启动类添加@EnableConfigServer和@EnableEurekaClient的注解

 

三、修改 eureka-config-client

将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-eureka,代码如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lwc</groupId>
    <artifactId>eureka-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

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

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

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

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

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

配置文件bootstrap-local.yml,注意是bootstrap。加上服务注册地址为 http://localhost:8889/eureka/

server:
  port: 8881
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/
spring:
  cloud:
    config:
      label: master
      profile: local
      discovery:
        serviceId: eureka-config-server
        enabled: true

 

spring.cloud.config.discovery.enabled 是从配置中心读取文件。

spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。

 

这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。

依次启动

  1. eureka-config-server-cluster
  2. eureka-config-server
  3. eureka-config-client

访问网址:http://localhost:8889/

image

 

访问 http://localhost:8881/configs ,浏览器显示:

This is local config Blog: http://www.cnblogs.com/EddieBlog/

 

四、源码下载

标签 7-1

https://github.com/eddie-code/SpringCloudDemo/tree/master/eureka-config-server-cluster

 

五、踩过的坑

留意注解

 

六、理解

如果有亲手做过的同学应该会发现, 我们是新建一个注册中心, 再将配置服务端和配置客户端一起注册去中心,  其余的和第一篇的差不多一样, 具体理解还是看个人, 师傅带入门, 修行靠自己!

以上是关于SpringCloud 第七篇: 高可用的分布式配置中心(Spring Cloud Config)的主要内容,如果未能解决你的问题,请参考以下文章

业余草 SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

Spring Cloud第七篇 | 声明式服务调用Feign

第七篇:分布式组件(DubboNettyNigixZookeeperMQJob)

关于AJAX 第七篇

SpringCloud学习系列之一 ----- 搭建一个高可用的注册中心(Eureka)

SpringCloud-高可用的分布式配置中心(config)