• Springboot3.0.0-M3+spring-cloud2022.0.0-M3+consul注册中心(看到最后可以留言)


    之前发布了一个《springboot3.0+jwt+RBAC正式上路》大家可以在公众号历史里面看到。

    图片

    Springboot3.0.0-M3和spring-cloud2022.0.0-M3 即spring-cloud体系脱离Spring Cloud Netflix之后开启了自己研发组件的道路比如网管、负载均衡等,今天要说的就是consul注册中心,可以用来取代netflix的Eureka。

    consul服务注册与发现一些概念我还是一如既往的略过,第一他不是什么新技术,第二也比较好理解用起来也比较简单,即便是集群模式也不复杂。

    今天要演示的demo有:weir-consul-client01和weir-consul-client02 作为微服务可以相互调用、weir-spring-cloud-gateway作为网关,源码位置也是一如既往:https://gitee.com/weir_admin/weir-project

    我这里贴出一个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>weir-parent</groupId>
        <artifactId>weir-parent</artifactId>
        <version>3.0</version>
        <relativePath>../parent/pom.xml</relativePath>
      </parent>
      <groupId>com.consul.client</groupId>
      <artifactId>weir-consul-client01</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>weir-consul-client01</name>
      <description>Demo project for Spring Boot</description>
      <properties>
        <java.version>17</java.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-consul-discovery</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        
        <dependency>
          <groupId>org.springdoc</groupId>
          <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
          <version>2.0.0-M3</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
    
    </project>
    
    
    • 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

    springdoc-openapi-starter-webmvc-ui 是针对springboot3的swagger之前的视频说过。

    application.yml:

    server:
      port: 9000
    spring:
      application:
        name: consul-client01
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
            prefer-ip-address: true
            heartbeat:
              enabled: true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    只有一个简单的controller:

    package com.consul.client.demo;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("client01")
    public class HelloController {
    
      @GetMapping("get")
      public String name() {
        return "client01";
      }
      
      @PostMapping("post")
      public String postName() {
        return "client01";
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    client02里面:

    package com.consul.client.demo;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @FeignClient(name = "consul-client01")
    public interface HelloFeignClient {
    
      @GetMapping("client01/get")
      String name();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.consul.client.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("client02")
    public class Hello02Controller {
    
      @Autowired
      HelloFeignClient helloFeignClient;
      
      @GetMapping("get")
      public String name() {
        String name = helloFeignClient.name();
        System.out.println("----------------name---" + name);
        return "client02" +"-----"+ name;
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    通过openfeign来调用client01。

    如果你现在启动consul启动两个程序client02来请求可以访问到client01的接口没有问题。

    我想问这里的client01和client02 能正常启动么?大家可以想想。如果你没有真正的去实践你可能第一感觉是可以启动。

    下面我们来搭建网管:

    <?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">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.weir.gateway</groupId>
      <artifactId>weir-spring-cloud-gateway</artifactId>
      <version>0.0.1</version>
      <packaging>jar</packaging>
    
      <name>spring-cloud-gateway</name>
      <description>spring-cloud-gateway</description>
    
      <parent>
        <groupId>weir-parent</groupId>
        <artifactId>weir-parent</artifactId>
        <version>3.0</version>
        <relativePath>../parent/pom.xml</relativePath>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      </properties>
    
      <dependencies>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!-- <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency> -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <scope>provided</scope>
        </dependency>
    
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
    </project>
    
    
    • 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
    server:
      port: 9992
    spring:
      application:
        name: spring-cloud-gateway
      cloud:
        consul:
          host: 127.0.0.1
          port: 8500
          discovery:
            register: true
            register-health-check: true
        gateway:
          discovery:
            locator:
              enabled: true
          routes:
          - id: consul-client01
            uri: lb://consul-client01
            predicates:
              - Path=/**
          - id: consul-client02
            uri: lb://consul-client02
            predicates:
              - Path=/**
    # 匹配所有端点
    #management:
    #  endpoints:
    #    web:
    #      exposure:
    #        include: "*"
    #  endpoint:
    #    health:
    #      show-details: always
    
    • 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

    可以看出非常简单基本都是配置,main方法也没有什么特别的:

    package com.weir.gateway;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class SpringCloudGatewayApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(SpringCloudGatewayApplication.class, args);
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    仅仅加了@EnableDiscoveryClient这个注解。

    到这里如果启动网关怎么通过网关的接口访问client01和client02呢?是不是这样:http://localhost:9992/consul-client01/client01/gethttp://localhost:9992/consul-client02/client02/get

    首先为什么有consul-client01 和consul-client02 这个大家知道原因么?对就是网关配置上面的url

    如果我去访问:http://localhost:9000/client01/gethttp://localhost:9001/client02/get这两个肯定是没有问题的对吧,就是访问每个单体的微服务接口,因为这里太简单的了我就不截图了。

    下面我要说的就是重点:http://localhost:9992/client01/get 这个接口可以访问成功么?你第一反应是不是不能?因为通过网关client01必须带consul-client01,client02必须带consul-client02

    你的理解没错,但是我测试的结果是可以的,这就是我今天为什么写这篇文章的原因,也是想让大家思考的原因。

  • 相关阅读:
    2022年8月22日上午七点CKA考试界面改动exe软件答题
    Freeswitch中mod_commonds
    华为攻击防范简介
    Golang入门:协程(goroutine)
    猿创征文|瑞吉外卖——移动端_手机端展示
    Electron_基础篇
    集合—LinkedList底层结构
    C++ 性能优化指南 KurtGuntheroth 第1章 优化概述
    SpringBoot后端初始框架搭建——基于SpringBoot和Vue的后台管理系统项目系列博客(四)
    父子组件通信方式详解
  • 原文地址:https://blog.csdn.net/weir2008/article/details/125533869