文章目录

  • 1、概述
  • 1.1 分布式系统面临的配置问题
  • 1.2 配置中心功能
  • 2、SpringCloud Config 服务端配置
  • 2.1 在github上新建lingluocloud-config的repository
  • 2.2 在本地硬盘中新建git仓库并clone
  • 2.3 在本地git仓库中新建一个application.yml
  • 2.4 新建module,lingluocloud-config-3344
  • 3、SpringCloud Config 客户端配置与测试
  • 3.1 在本地git仓库中新建一个springcolud-config-client.yml
  • 3.2 新建lingluocloud-config-client-3355模块
  • 4、配置实战
  • 4.1 目前情况
  • 4.2 Git配置文件本地配置
  • 4.3 Config版的eureka服务端7001
  • 4.4 Config版的dept微服务8001


1、概述

1.1 分布式系统面临的配置问题

集中式的、动态的配置管理必不可少

springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring

Spring colud Config为微服务架构中心的微服务提供集中式的外部配置支持,配置服务器为各个不同的微服务应用提供一个中心化的外部配置

Spring colud Config分为服务端和客户端两部分

  • 服务端
    也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置等访问接口
  • 客户端
    通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取配置,配置服务器默认采用git来存储配置信息。这样有助于对环境配置的版本管理,并且可以通过git客户端访问配置内容
1.2 配置中心功能
  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署,dev/test/prod/beta/release
  • 当配置发生变化是,服务不需要重启
  • 运行期间动态调整配置
  • 将配置信息以rest接口形式暴露
  • 与github、svn整合

2、SpringCloud Config 服务端配置

2.1 在github上新建lingluocloud-config的repository

SSH地址:
git@github.com:littleCatBobi/springcloud-config.git

HTTP地址:
https://github.com/littleCatBobi/springcloud-config.git

2.2 在本地硬盘中新建git仓库并clone

在本地硬盘新建一个文件夹lingluoSpringCloud,进去,右键 Git Bash Here

git clone git@github.com:littleCatBobi/springcloud-config.git

springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring_02

2.3 在本地git仓库中新建一个application.yml

格式必须是UTF-8的形式保存,否则会有乱码

spring:
  profiles:
    active: test
---
spring:
  profiles: dev #开发环境
  application:
    name: lingluocloud-config-dev

---
spring:
  profiles: test
  application:
    name: lingluocloud-config-test

这里格式要特别注意,最好在idea上新建一个文件,别用手打,很坑

依次执行如下命令,提交到github

git add .
git commit -m "init file"
git push origin master

去Github上看一下

springcloud 任务调度框架 Quartz springcloud分布式任务调度_git_03

2.4 新建module,lingluocloud-config-3344
  • 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>lingluocloud</artifactId>
        <groupId>com.lingluo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lingluocloud-config-3344</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--zuul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--Hystrix容错-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</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>
        </dependency>
        <!--热部署 修改后立即生效-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>
  • yml文件
server:
  port: 3344

spring:
  application:
    name: lingluocloud-config #配置中心名字
  cloud:
    config:
      server:
        git:
          #GitHub上面的git仓库名字
          uri: git@github.com:littleCatBobi/springcloud-config.git
  • 主启动类 Config_3344_App
@SpringBootApplication
@EnableConfigServer
public class Config_3344_App {
    public static void main(String[] args) {
        SpringApplication.run(Config_3344_App.class,args);
    }
}
  • 修改host文件增加映射
127.0.0.1  config3344.com
  • 启动服务,访问yml

开发环境:http://config3344.com:3344/application-dev.yml

springcloud 任务调度框架 Quartz springcloud分布式任务调度_git_04

测试环境: http://config3344.com:3344/application-test.yml

springcloud 任务调度框架 Quartz springcloud分布式任务调度_git_05

成功实现了用SpringCloud Config通过GitHub获取配置信息,现在开始打通客户端与服务端的通道。

3、SpringCloud Config 客户端配置与测试

3.1 在本地git仓库中新建一个springcolud-config-client.yml

在本地springcloud-config目录下新建

spring:
  profiles:
    active: dev
---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: lingluocloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: lingluocloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://eureka-test.com:7001/eureka/

请保存为UTF-8格式!!!
再次提交到github上。

3.2 新建lingluocloud-config-client-3355模块
  • 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>lingluocloud</artifactId>
        <groupId>com.lingluo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lingluocloud-config-client-3355</artifactId>
    <dependencies>

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

        <!--zuul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

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

        <!--Hystrix容错-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</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>
        </dependency>

        <!--热部署 修改后立即生效-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
  • 主启动类
@SpringBootApplication
public class Config_Client_3355_App {
    public static void main(String[] args) {
        SpringApplication.run(Config_Client_3355_App.class,args);
    }
}
  • yml文件
    application.yml是用户级的资源配置项
    bootstrap.xml是系统级的,优先级更高
    Springcolud 会创建一个BootStrap Context,作为spring应用的Application Context的父上下文。初始化的时候,BootStrap Context负责从外部资源加载配置属性并解析配置。这两个上下文共享一个外部获取的Environment。BootStrap 属性有高优先级,默认情况下,他们不会被本地配置覆盖。BootStrap Context和Application Context有着不同的约定。
    所以新增一个bootstrap.xml,保证BootStrap Context和Application Context的配置分离。
  • application.yml
spring:
  application:
    name: lingluocloud-config-client
  • bootstrap.xml
spring:
  cloud:
    config:
      name: springcloud-config-client #需要从github上服务的资源名称,注意没有yml后缀
      profile: test  #本次访问的配置项是测试环境
      label: master
      uri: http://config3344.com:3344 #本次服务启动后,先去找3344服务,通过SpringCloudConfig获取github上的配置
  • host文件配置
127.0.0.1  config-client.com
  • 新建rest类,验证是否能从Github上读取配置
package com.lingluo.springcloud.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
  public class ConfigClientRest {
  
      @Value("${spring.application.name}")
      private String applicationName;
      @Value("${eureka.client.service-url.defaultZone}")
      private String eurekaServers;
      @Value("${server.port}")
      private String port;
  
      @RequestMapping("/config")
      public String getConfig(){
  
          System.out.println("applicationName:"+this.applicationName
                  +"eurekaServers:"+this.eurekaServers
                  +"port:"+this.port);
          return "applicationName:"+this.applicationName
                  +"eurekaServers:"+this.eurekaServers
                  +"port:"+this.port;
      }
  }
  • 测试
    启动3344、3355

bootstrap.yml里面的profile值是什么,决定从github上读取什么
假如目前是 profile: dev
dev默认在github上对应的端口就是8201
http://config-client.com:8201/config 假如目前是 profile: test
test默认在github上对应的端口就是8202
http://config-client.com:8202/config

因为我这是测试环境,所以访问http://config-client.com:8202/config

springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring_06


成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息!如果访问开发环境是不OK的,因为我们在bootstrap.xml指定了profile: test #本次访问的配置项是测试环境

springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring_07

4、配置实战

4.1 目前情况

config服务端配置测试OK,我们可以和config+github进行配置修改并获得内容

现在我们来做一个euraka服务+一个dept访问的微服务,将两个微服务的配置统一由github获得实现统一配置,分布式管理,完成多环境变更。

4.2 Git配置文件本地配置
  • 在项目根目录下新建springcloud-config-eureka-client.yml
spring:
  profile:
    active: dev
---
server:
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
  profiles: dev
  application:
    name: lingluocloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
  profiles: test
  application:
    name: lingluocloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  • 在项目根目录下新建springcloud-config-dept-client.yml
spring:
  profile:
    active: dev
---
server:
  port: 8001

spring:
  profiles: dev
  application:
    name: lingluocloud-config-dept-client
  datasource:
    url: jdbc:mysql://localhost:3306/clouddb01?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true&verifyServerCertificate=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.lingluo.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations: classpath:mybatis/mapper/*.xml                       # mapper映射文件

eureka:
  client: #客户端注册进eureka服务列表内
    #以下两个参数坑爹玩意,如果加上了就无法找到注册的实例
    #registerWithEureka: false
    #fetchRegistry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
      # 将该service注册到集群eureka中
      #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: lingluocloud-dept8001   # 在eureka中显示对应的服务名字
    prefer-ip-address: true     #在eureka中下边的访问路径可以显示IP地址

info: # 在eureka中可以点击info链接,显示有值。链接后的内容就是在这里配置的
  app.name: lingluocloud-config-dev
  company.name: www.lingluo.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 8001

spring:
  profiles: test
  application:
    name: lingluocloud-config-dept-client
  datasource:
    url: jdbc:mysql://localhost:3306/clouddb02?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true&verifyServerCertificate=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.lingluo.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations: classpath:mybatis/mapper/*.xml                       # mapper映射文件

eureka:
  client: #客户端注册进eureka服务列表内
    #以下两个参数坑爹玩意,如果加上了就无法找到注册的实例
    #registerWithEureka: false
    #fetchRegistry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
      # 将该service注册到集群eureka中
      #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: lingluocloud-dept8001   # 在eureka中显示对应的服务名字
    prefer-ip-address: true     #在eureka中下边的访问路径可以显示IP地址

info: # 在eureka中可以点击info链接,显示有值。链接后的内容就是在这里配置的
  app.name: lingluocloud-config-test
  company.name: www.lingluo.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

把这两个文件传到github上。后面我们从github上读配置文件。

4.3 Config版的eureka服务端7001
  • 新建微服务模块 cloud-config-eureka-client-7001

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>lingluocloud</artifactId>
        <groupId>com.lingluo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lingluocloud-config-eureka-client-7001</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

spring:
  cloud:
    config:
      name: springcloud-config-eureka-client #需要从github上服务的资源名称,注意没有yml后缀
      profile: test  #本次访问的配置项
      label: master
      uri: http://config3344.com:3344 #本次服务启动后,先去找3344服务,通过SpringCloudConfig获取github上的配置

application.yml

spring:
  application:
    name: lingluocloud-config-eureka-client

主启动类

@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApp {
    public static void main(String[] args) {
        SpringApplication.run(Config_Git_EurekaServerApp.class, args);
    }
}
  • 测试
    先启动lingluocloud-config-3344微服务,保证Config总配置是OK的
    再启动lingluocloud-config-eureka-client-7001微服务
    访问 http://eureka7001.com:7001/,出现eureak主页表示成功启动
  • springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring_08

4.4 Config版的dept微服务8001

参考之前的8001拷贝后新建工程lingluocloud-config-dept-client-8001

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>lingluocloud</artifactId>
        <groupId>com.lingluo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lingluocloud-config-dept-client-8001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
        <dependency>
            <groupId>com.lingluo.springcloud</groupId>
            <artifactId>lingluocloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</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>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!--  log4j12日志适配器 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

spring:
  cloud:
    config:
      name: springcloud-config-dept-client #需要从github上服务的资源名称,注意没有yml后缀
      profile: test  #本次访问的配置项
      label: master
      uri: http://config3344.com:3344 #本次服务启动后,先去找3344服务,通过SpringCloudConfig获取github上的配置

application.yml

spring:
  application:
    name: lingluocloud-config-dept-client

主启动类

@SpringBootApplication
//本服务启动后会自动注册进eureka服务
@EnableEurekaClient
@EnableDiscoveryClient//服务发现,对外暴露
public class Config_DeptProvider8001_App {

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

其他的包也都拷过来

  • 测试

test配置默认访问http://localhost:8001/dept/findAll 可以看到数据库配置是02

springcloud 任务调度框架 Quartz springcloud分布式任务调度_spring_09


本地换配置成dev访问http://localhost:8001/dept/findAll 可以看到数据库配置是01


SpringCloud入门系列到这里就结束了,感谢~~
SpringCloud入门完整版获取(超详细)