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

本文主要是介绍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-client01cloud:consul:host: localhostport: 8500discovery:service-name: ${spring.application.name}prefer-ip-address: trueheartbeat: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 {@AutowiredHelloFeignClient 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-gatewaycloud:consul:host: 127.0.0.1port: 8500discovery:register: trueregister-health-check: truegateway:discovery:locator:enabled: trueroutes:- id: consul-client01uri: lb://consul-client01predicates:- Path=/**- id: consul-client02uri: lb://consul-client02predicates:- 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

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

这篇关于Springboot3.0.0-M3+spring-cloud2022.0.0-M3+consul注册中心(看到最后可以留言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/815931

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2