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注册中心(看到最后可以留言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weir2008/article/details/125533869
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/815931

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、