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

1:新建一个eureka server工程,命名为config-eureka-server,作为配置服务的注册中心。pom文件如下:

<?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.springcloud</groupId>
    <artifactId>config-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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.RELEASE</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>

入口类加入相应注解:

@EnableEurekaServer
@SpringBootApplication
public class ConfigEurekaServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigEurekaServerApplication.class, args);
    }
}

修改配置文件如下:

server.port=8889
eureka.instance.hostname=localhost
#配置成服务
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry=false
eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/

2:修改之前的config-server工程
在pom中加入需要的依赖;

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

修改配置文件:

spring.application.name=config-server
server.port=8888


#配置文件在本地
#spring.profiles.active=native
#配置文件的目录
#spring.cloud.config.server.native.search-locations=D:/workspace/GitHub/spring-config/config-file

#spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.uri=https://github.com/iot-wangshuyu/springcloud
#配置仓库路径
spring.cloud.config.server.git.searchPaths=SpringcloudConfig/respo/
#配置仓库的分支
spring.cloud.config.label=master
#访问git仓库的用户名
spring.cloud.config.server.git.username=
#访问git仓库的用户密码
spring.cloud.config.server.git.password=
#注册服务地址
eureka.client.service-url.defaultZone=http://localhost:8889/eureka/

修改启动类:

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

3:修改config-client工程

pom中添加相应的依赖:

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

修改配置文件名称为:bootstrap.properties
内容修改为:

#服务名称
spring.application.name=config-client
#仓库分支
spring.cloud.config.label=master
#dev开发环境配置文件
#test测试环境
#pro正式环境
spring.cloud.config.profile=dev
#使用eureka server的时候需要注释掉
#spring.cloud.config.uri= http://localhost:8888/

#服务注册地址
eureka.client.service-url.defaultZone=http://localhost:8889/eureka/
#从远程获取配置文件
spring.cloud.config.discovery.enabled=true
#订阅的服务名称
spring.cloud.config.discovery.service-id=config-server
server.port=8881

如果配置服务部署多份,通过负载均衡,从而高可用。
依次启动程序,config-eureka-server,config-server,config-client,浏览器访问:http://localhost:8889/可以查看服务注册情况;
浏览器访问:http://localhost:8881/hi;返回配置文件中的foo属性;
说明高可用分布式配置中心配置成功

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐