【Spring Cloud】009-Config

一、概述

1、分布式系统面临的配置文件的问题

微服务意味着要将单体应用中的业务拆分成一个个子服务, 每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的, 动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,那上百的的配置文件要修改起来,岂不是要发疯!

 

2、什么是SpringCloud config分布式配置中心

Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置;
Spring Cloud Config 分为服务端客户端两部分:
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口;
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理。并且可以通过git客户端工具来方便的管理和访问配置内容;

 

3、git简单使用

从远程仓库克隆:

# git clone [ssh地址]
# 注意:本地要有公钥
git clone git@gitee.com:zibo2019/springcloud-config.git

提交到云端仓库:

# 添加文件
git add .
# 查看状态
git status
# 保存到本地仓库
git commit -m "保存说明"
# 提交到云端仓库
git push origin master

当前云端仓库文件:

 

二、服务端连接Git配置

1、创建一个新模块springcloud-config-server-3344,并导入配置

<?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>springcloud</artifactId>
        <groupId>com.zibo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-server-3344</artifactId>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <!--健康监控模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

 

2、写配置文件application.yaml

server:
  port: 3344
spring:
  application:
    name: springcloud-config-server
  # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zibo2019/springcloud-config.git # https 不是 git
          

 

3、写主启动类

package com.zibo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer //开启
public class Config_Server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Server_3344.class,args);
    }
}

 

4、运行测试

访问:http://localhost:3344/application-dev.yaml

 

5、HTTP服务具有以下格式的资源

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

 

三、客户端连接服务端访问远程

1、新建一个config-client.yaml

spring:
  profiles:
    active: dev
---
server:
  port: 8201
# spring配置
spring:
  profiles: dev
  application:
    name: springcloud-provider-dept
# Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 8202
# spring配置
spring:
  profiles: test
  application:
    name: springcloud-provider-dept

# Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

 

2、将文件上传到云端仓库

具体操作:

云端仓库:

 

3、创建一个新的模块springcloud-config-client-3355,并导入坐标

<?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>springcloud</artifactId>
        <groupId>com.zibo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-client-3355</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--健康监控模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

 

4、写配置文件bootstrap.yaml和application.yaml

bootstrap.yaml:

# 系统级别的配置
spring:
  cloud:
    config:
      name: config-client # 需要从git上获取的资源名称,不要后缀
      profile: dev
      label: master
      uri: http://localhost:3344

application.yaml:

# 用户级别的配置
spring:
  application:
    name: springcloud-config-client-3355

 

5、写启动类

package com.zibo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 

6、写Controller,从云端注入数据

package com.zibo.springcloud.controller;

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

@RestController
public class ConfigClientController {
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;
    @Value("${server.port}")
    private String port;
    
    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName:" + applicationName + ";eurekaServer:" + eurekaServer + ";port:" + port;
    }
}

 

7、运行测试

启动流程:3344——3355

访问http://localhost:8201/config

 

四、远程配置实战

1、创建一个配置文件config-eureka

spring:
    profiles:
        active: dev
---
server:
  port: 7001
# spring配置
spring:
  profiles: dev
  application:
    name: springcloud-config-eureka
# Eureka配置
eureka:
  instance:
    hostname: eureka7001.com # Eureka服务端实例名称
  client:
    register-with-eureka: false # 是否向eureka注册中心注册自己
    fetch-registry: false # 如果为false,则表示自己为注册中心
    service-url: # 监控页面
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
---
server:
  port: 7001
# spring配置
spring:
  profiles: test
  application:
    name: springcloud-config-eureka
# Eureka配置
eureka:
  instance:
    hostname: eureka7001.com # Eureka服务端实例名称
  client:
    register-with-eureka: false # 是否向eureka注册中心注册自己
    fetch-registry: false # 如果为false,则表示自己为注册中心
    service-url: # 监控页面
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

 

2、再创建一个配置文件config-dept.yaml

spring:
    profiles:
        active: dev
---
server:
  port: 8001

# mybatis配置
mybatis:
  type-aliases-package: com.zibo.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

# spring配置
spring:
  profiles: dev
  application:
    name: springcloud-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zb1?serverTimezone=UTC
    username: root
    password: zibo15239417242 

# Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 # 修改eureka的默认描述信息

# info配置信息
info:
  app.name: zibo-springcloud
  company.name: com.zibo
---
server:
  port: 8001

# mybatis配置
mybatis:
  type-aliases-package: com.zibo.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

# spring配置
spring:
  profiles: test
  application:
    name: springcloud-config-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/zb2?serverTimezone=UTC
    username: root
    password: zibo15239417242 

# Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 # 修改eureka的默认描述信息

# info配置信息
info:
  app.name: zibo-springcloud
  company.name: com.zibo

 

3、上传至云端

 

4、创建一个新模板springcloud-config-eureka-7001,并导包

<?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>springcloud</artifactId>
        <groupId>com.zibo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-eureka-7001</artifactId>
    <!--导包-->
    <dependencies>
        <!--config-->
        <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>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

 

5、创建application.yaml和bootstrap.yaml

application.yaml:

# 用户级别的配置
spring:
  application:
    name: springcloud-config-eureka-7001

bootstrap.yaml:

# 系统级别的配置
spring:
  cloud:
    config:
      name: config-eureka # 需要从git上获取的资源名称,不要后缀
      profile: dev
      label: master
      uri: http://localhost:3344

 

6、编写主启动类

package com.zibo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //服务端启动类,可以接受别人注册进来!
public class Config_Eureka_7001 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Eureka_7001.class,args);
    }
}

 

7、运行测试

测试7001从远程仓库获取配置:

 

8、创建一个新模板springcloud-config-provider-dept-8001,并导入依赖

(其他文件从springcloud-provider-dept-8001复制过来即可)

<?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>springcloud</artifactId>
        <groupId>com.zibo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-provider-dept-8001</artifactId>
    <dependencies>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--在服务提供者里面添加Eureka依赖坐标-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--actuator:完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--我们需要拿到实体类,所以需要配置api module-->
        <dependency>
            <groupId>com.zibo</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </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-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--jetty-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

 

9、编写配置文件application.yaml和bootstrap.yaml

application.yaml:

# spring配置
spring:
  application:
    name: springcloud-config-provider-dept-8001

bootstrap.yaml:

# 系统级别的配置
spring:
  cloud:
    config:
      name: config-dept # 需要从git上获取的资源名称,不要后缀
      profile: dev
      label: master
      uri: http://localhost:3344

 

10、运行测试

 

五、Spring Cloud总结

 

六、整个项目的源代码

截图:

下载地址:

链接:https://pan.baidu.com/s/1qruHjTW_G4BxHUdMleFqkA 
提取码:zibo

 

 

 

 

 

 

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页