原创

Spring Cloud 学习笔记6 分布式配置中心

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

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

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

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

1.新建config server项目

1.png

2.png

2.修改config server项目的pom.xml

修改parent节点

<parent>
       <groupId>cn.djc8</groupId>
       <artifactId>blog2</artifactId>
       <version>0.0.1-SNAPSHOT</version>
   </parent>

3.修改blog2的pom.xml文件

增加configserver 的依赖

<modules>
        <module>eurekaserver</module>
        <module>eurekaclient</module>
        <module>ribbon</module>
        <module>feign</module>
        <module>zuulserver</module>
        <module>configserver</module>
    </modules>

4.修改ConfigserverApplication.java,增加注解

@EnableConfigServer

5.修改 configserver\src\main\resources\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=

spring.cloud.config.server.git.uri:配置git仓库地址 spring.cloud.config.server.git.searchPaths:配置仓库路径 spring.cloud.config.label:配置仓库的分支 spring.cloud.config.server.git.username:访问git仓库的用户名 spring.cloud.config.server.git.password:访问git仓库的用户密码

6.在blog2项目中增加目录:SpringCloudConfig

SpringCloudConfig中增加目录respo

respo目录中增加四个文件,文件名称以及文件内容如下: address-dev.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
server.port=8882
hystrix.command.default.execution.timeout.enabled=false 
management.security.enabled=false

config-client-dev.properties

foo = cn.djc8
democonfigclient.message=hello spring io

# routes to serviceId
# routes to url
# zuul.routes.api-a-url.path=/userapi/**
# zuul.routes.api-a-url.url=http://localhost:8881/
#eureka.client.serviceUrl.defaultZone=http://localhost:8880/eureka/

user-dev.properties

eureka.client.serviceUrl.defaultZone=http://localhost:8880/eureka/
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
hystrix.command.default.execution.timeout.enabled=false 
management.security.enabled=false
server.port=8881

zuul-dev.properties

server.port=8889
# routes to serviceId
zuul.routes.api-a.path=/userapi/**
zuul.routes.api-a.serviceId=USER
zuul.routes.api-b.path=/addressapi/**
zuul.routes.api-b.serviceId=ADDRESS

# routes to url
# zuul.routes.api-a-url.path=/userapi/**
# zuul.routes.api-a-url.url=http://localhost:8881/
#eureka.client.serviceUrl.defaultZone=http://localhost:8880/eureka/

我这里已经将git的地址配置我这个系列笔记的github仓库地址。查看提交的内容 先将配置都提交了,后面会进行修改

7.这些都昨晚后,启动项目configserver

访问:http://localhost:7888/config-client/dev/master

不出意外的话,会显示以下json串

{"name":"config-client","profiles":["dev"],"label":"master","version":"799216c31a4ac7c8dcf08f99ef5a3cb421755455","state":null,"propertySources":[{"name":"https://github.com/chengs2035/blog2/SpringCloudConfig/respo/config-client-dev.properties","source":{"foo":"cn.djc8","democonfigclient.message":"hello spring io"}}]}

http请求地址和资源文件映射如下: /{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

8.增加config Client模块

项目名称:configclient,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-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>
            <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>

增加bootstrap.yml,路径:configclient\src\main\resources\,内容为:

  spring:
  application:
    name:config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:7888/
server:
  port: 7881

修改ConfigclientApplication.java,增加注解

@RestController

增加getgithubconfig方法,增加foo属性,此时ConfigclientApplication.java代码如下

package cn.djc8.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ConfigclientApplication {

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

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/getgithubconfig")
    public String getgithubconfig(){
        return foo;
    }

}

9.查看效果

访问:http://localhost:7881/getgithubconfig 浏览器显示:cn.djc8 说明我们已经通过config server从github上取到了配置文件,并且加载出来了。

后记: 在我提交整个项目后,发现构建失败了,查看日志,提示foo注入失败,这是因为在自动构建的过程中,需要启动config server,为了方便后续构建,我修改了configclient\src\main\resources\application.yml文件,增加了foo的默认值

foo: NaN

参考: 1.史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本) 2.application.yml与bootstrap.yml的区别 3.Spring Cloud Config 实现配置中心,看这一篇就够了

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

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