原创

Spring Cloud 学习笔记7 高可用分布式配置中心

高可用分布式配置中心

本次代码都已经提交在github上,点击这里访问,项目当前的构建状态:构建状态

本篇代码基于上篇文章的代码,见学习笔记6

方大佬的这两章将项目区分开来,我这里不做区分,还是在原先的项目中,增加module进行部署。

###1.改造configserver

1.1修改configserver的pom.xml

路径configserver\pom.xml

增加spring-cloud-starter-netflix-eureka-client

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

此时configserver的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>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>configserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configserver</name>
    <description>config server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

1.2 修改application.properties

增加eureka server注册中心的地址。
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/
此时完整的application.properties如下

spring.application.name=configserver
server.port=7888

spring.cloud.config.server.git.uri=https://github.com/chengs2035/blog2
spring.cloud.config.server.git.searchPaths=SpringCloudConfig/respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/

1.3 修改ConfigserverApplication.java

增加注解@EnableEurekaClient
此时ConfigserverApplication.java代码如下

package cn.djc8.configserver;

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

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

}

###2.改造config client

2.1 pom.xml修改

<dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
           <version>2.3.1.RELEASE</version>
           <scope>compile</scope>
       </dependency>

此时config client的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>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>configclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configclient</name>
    <description>Config client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

2.2 修改configclient\src\main\resources\bootstrap.yml

增加注册中心,使用eureka server的地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

以及

spring:
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: configserver

此时bootstrap.yml文件如下

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:7888/ 屏蔽了,通过serviceId获取服务了
    discovery:
      enabled: true
      serviceId: configserver
server:
  port: 7881

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

3.测试

依次启动:eurekaserver,configserver,configclient

访问http://localhost:7001/

浏览器显示如下

1.png

访问http://localhost:7881/getgithubconfig

浏览器显示:

cn.djc8

参考

  1. 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

本文来自:Spring Cloud 学习笔记7 高可用分布式配置中心-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2022年03月01日,已超过 780 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录