第七篇:高可用配置中心(Spring Cloud Config)

当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用

1.准备

使用上一篇文章的工程

2.构建高可用配置中心

首先创建一个注册中心eureka-server

新建一个model,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">
    <parent>
        <artifactId>spring-cloud-config</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Eureka 服务端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写配置文件application.yml

server:
  port: 8889

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

在启动类开启eureka,使用@EnableEurekaServer注解

复制config-server,改名为config-server-high-availability,引入spring-cloud-config依赖和eureka依赖。poom文件如下

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

    <artifactId>config-server-high-availability</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-config依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!---eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

    <!--Maven打包工具-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

编写配置文件application.properties

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

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
# 配置仓库路径
spring.cloud.config.server.git.searchPaths=respo
# 配置仓库的分支
spring.cloud.config.label=master
# 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
# 访问git仓库的用户名
#spring.cloud.config.server.git.username=
# 访问git仓库的用户密码
#spring.cloud.config.server.git.password=

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

启动类上加上@EnableEurekaClient注解

复制config-client,改名为config-server-high-availability,引入spring-cloud-config依赖和eureka依赖。poom文件如下

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

    <artifactId>config-client-high-availability</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-starter-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包-->
    <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:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
# 从配置中心读取文件
spring.cloud.config.discovery.enabled=true
# 置中心的servieId,即服务名
# 读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用
spring.cloud.config.discovery.serviceId=config-server
server.port=8881

现在读取配置文件是服务名,不再写ip地址

如果配置服务部署多份,通过负载均衡,从而高可用

依次启动eureka-server,config-server-high-availability,config-client-high-availability,访问http://localhost:8881/test

now is 100

源码:https://gitee.com/niugit/spring-cloud-config

猜你喜欢

转载自www.cnblogs.com/niudaben/p/12437064.html