前言:

本文内容:Config:服务端连接Git配置、Zuul:客户端连接服务端远程访问、远程配置实战测试

推荐免费SpringCloud基础教程视频:【狂神说Java】SpringCloud最新教程IDEA版_哔哩哔哩_bilibili

SpringCloud笔记代码下载地址:

蓝奏云:下载地址 密码:joker

百度云:下载地址 提取码:04eh

Config:服务端连接Git配置

  1. 新建Module为springcloud-config-server-3344

  2. 在pom文件中引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  3. resources下新建application.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    server:
    port: 3344
    spring:
    application:
    name: springcloud-config-server
    #远程连接
    cloud:
    config:
    server:
    git:
    uri: https://gitee.com/jokerdig/springcloud-config.git
  4. 新建com.jokerdig.springcloud包,在包下新建ConfigServer_3344主启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.jokerdig.springcloud;

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

    /**
    * @author Joker大雄
    * @data 2022/8/10 - 10:45
    **/
    @SpringBootApplication
    @EnableConfigServer // 开启ConfigServer
    public class ConfigServer_3344 {
    public static void main(String[] args) {
    SpringApplication.run(ConfigServer_3344.class,args);
    }
    }
  5. 启动ConfigServer_3344,运行测试

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

    1
    2
    3
    4
    5
    spring:
    application:
    name: springcloud-config-dev
    profiles:
    active: dev

Config:客户端连接服务端远程访问

  1. Gitee提交config-client.yaml(提交步骤参考上一篇)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    spring:
    profiles:
    active: dev
    ---
    server:
    port: 3355
    # spring开发配置
    spring:
    profiles: dev
    application:
    name: springcloud-client-dept
    eureka:
    client:
    service-url:
    defaultZone: http://eureka7001.com:7001/eureka/
    ---
    server:
    port: 3356
    # spring测试配置
    spring:
    profiles: test
    application:
    name: springcloud-client-dept
    eureka:
    client:
    service-url:
    defaultZone: http://eureka7001.com:7001/eureka/
  2. 新建Module为springcloud-config-client-3355

  3. 在pom中引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  4. resources下新建bootstrap.yamlapplication.yaml

    bootstrap

    1
    2
    3
    4
    5
    6
    7
    8
    # bootstrap 系统级别的配置
    spring:
    cloud:
    config:
    uri: http://localhost:3344
    name: config-client # 需要读取的资源名称
    profile: dev
    label: master

    application

    1
    2
    3
    4
    # application用户级别的配置
    spring:
    application:
    name: springcloud-config-client-3355
  5. 新建包com.jokerdig.springcloud,在包下新建ConfigClient_3355主启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.jokerdig.springcloud;

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

    /**
    * @author Joker大雄
    * @data 2022/8/10 - 11:19
    **/
    @SpringBootApplication
    public class ConfigClient_3355 {
    public static void main(String[] args) {
    SpringApplication.run(ConfigClient_3355.class,args);
    }
    }
  6. 新建com.jokerdig.springcloud.controller包,在包下新建ConfigClientController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package com.jokerdig.springcloud.controller;

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

    /**
    * @author Joker大雄
    * @data 2022/8/10 - 11:29
    **/
    @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:3355/config

    1
    2
    3
    applicationName:springcloud-client-dept
    eurekaServer:http://eureka7001.com:7001/eureka/
    port:3355

Config:远程配置实战测试

把之前项目的配置从本地改为远端

  1. Gitee提交config-dept.yamlconfig-eureka.yaml

    config-dept

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    spring:
    profiles:
    active: dev
    ---
    server:
    port: 8001
    # mybatis配置
    mybatis:
    type-aliases-package: com.jokerdig.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/db01?useSSL=true&serverTimezone=UTC
    username: root
    password: 123456
    # 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
    prefer-ip-address: true

    # info信息配置
    info:
    app.name: jokerdig-springcloud
    company.name: jokerdig.com
    ---
    server:
    port: 8001
    # mybatis配置
    mybatis:
    type-aliases-package: com.jokerdig.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/db02?useSSL=true&serverTimezone=UTC
    username: root
    password: 123456
    # 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
    prefer-ip-address: true

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

    config-eureka

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    spring:
    profiles:
    active:

    ---
    server:
    port: 7001
    # spring配置
    spring:
    profiles: dev
    application:
    name: springcloud-config-eureka
    # eureka配置
    eureka:
    instance:
    hostname: eureka7001.com #服务端实例名称
    client:
    register-with-eureka: false # 是否向eureka注册中心注册自己
    fetch-registry: false # 如果为false 则表示自己为注册中心
    service-url:
    # http://${eureka.instance.hostname}:${server.port}/eureka/
    # 集群 需要取关联别人
    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 #服务端实例名称
    client:
    register-with-eureka: false # 是否向eureka注册中心注册自己
    fetch-registry: false # 如果为false 则表示自己为注册中心
    service-url:
    # http://${eureka.instance.hostname}:${server.port}/eureka/
    # 集群 需要取关联别人
    defaultZone:
  2. 新建Module为springcloud-config-eureka-7001

    复制springcloud-eureka-7001内容到这个Module

    修改主启动类名字为ConfigEurekaServer_7001

  3. pom中引入依赖(原本复制的依赖不变)

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  4. resources下新建bootstrap.yaml并重新配置application.yaml

    bootstrap

    1
    2
    3
    4
    5
    6
    7
    spring:
    cloud:
    config:
    name: config-eureka
    label: master
    profile: dev
    uri: http://localhost:3344

    application

    1
    2
    3
    spring:
    application:
    name: springcloud-config-eureka-7001
  5. 运行3344、ConfigEurekaServer_7001,进行测试

    http://localhost:7001/ 可以访问说明配置ok

  6. 新建Module为springcloud-config-dept-8001

    复制springcloud-provider-dept-8001内容到这个Module

    修改主启动类名字为ConfigDeptProvider_8001

  7. pom中引入依赖(原本复制的依赖不变)

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  8. resources下新建bootstrap.yaml并重新配置application.yaml

    bootstrap

    1
    2
    3
    4
    5
    6
    7
    spring:
    cloud:
    config:
    name: config-dept
    label: master
    profile: dev
    uri: http://localhost:3344

    application

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
       spring:
    application:
    name: springcloud-config-dept-8001

    9. 运行3344、`ConfigEurekaServer_7001`、`ConfigDeptProvider_8001`,进行测试

    http://localhost:8001/dept/get/1

    ```java
    {"deptno":1,"dname":"开发部","db_source":"db01"}

SpringCloud总结

39