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>

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

只有一个简单的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";
  }
}

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();
}

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;
  }
}

通过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>

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

可以看出非常简单基本都是配置,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);
  }
}

仅仅加了@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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值