当前位置: 首页 > news >正文

Spring-Cloud-Gateway-Samples,nacos为注册中心,负载均衡

背景:本想找个简单例子看下,无奈版本依赖太过复杂,花了点时间。记录下吧

使用Spring Cloud Gateway作为网关服务,Nacos作为注册中心,实现对子服务的负载均衡访问。简单例子。

一、gateway-main-nacos服务端:     

完整pom文件:

<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.example</groupId><artifactId>gateway-main-nacos</artifactId><version>1.0.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Cloud Gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- Nacos Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.1</version></dependency><!-- Nacos Config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2021.1</version></dependency><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2020.0.3</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

完整的yml配置文件:

server:port: 8888
spring:application:name: gateway-servicemain:web-application-type: reactivecloud:nacos:discovery:server-addr: localhost:8848 # Nacos 服务器地址namespace:  namespace_xxxconfig:server-addr: localhost:8848 # Nacos 配置中心地址file-extension: yamlgateway:discovery:locator:enabled: trueroutes:- id: neo_routeuri: http://www.ityouknow.compredicates:- Path=/spring-cloud
#     - id: add_request_parameter_route
#       uri: http://localhost:8889
#       filters:
#       - AddRequestParameter=foo, bar
#       predicates:
#         - Method=GET- id: lb_nacosuri: lb://user-service  #会进行服务负载均衡filters:- AddRequestParameter=foo, bar123predicates:- Path=/user/** # 匹配 /user/** 路径的请求- id: lb_nacos01uri: lb://user-servicefilters:- StripPrefix=1 # 去掉前缀 /user01predicates:- Path=/user01/** # 匹配 /user01/** 路径的请求

    GatewayApplication启动类:

package com.neo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}

二、  路由的子服务(可以发布两个服务,达到负载均衡的效果)   

完整pom文件:

<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.example</groupId><artifactId>user-service</artifactId><version>1.0.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Nacos Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.1</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2020.0.3</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

完整的yml文件:

server:port: 8889spring:application:name: user-service#main:#  web-application-type: reactivecloud:gateway:discovery:locator:enabled: truenacos:discovery:server-addr: localhost:8848 # Nacos 服务器地址namespace:  namespace_xxx  #nacos命名空间,修改为自己定义的config:server-addr: localhost:8848 # Nacos 配置中心地址namespace:  namespace_xxxfile-extension: yaml

子服务启动类:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);}
}

对应测试的Controller类:

​
@RestController
public class HelloController {@GetMapping("/hello")public String index() {return "hello world!";}@GetMapping("/user/hello")public String indexUser() {return "hello world user!";}@RequestMapping("/user/foo")public String foo(String foo) {return "hello "+foo+"!";}}​

三、测试

本地启动gateway-main-nacos、user-service、user-service-1。 访问 http://localhost:8888/user/hello,多次访问发现会返回 hello world user! 或者 hello world user,another!。达到负载均衡的效果。

访问这个地址也可以看到同样的效果, http://localhost:8888/user01/hello

两个url不同的路由匹配规则。

四、遇到的问题:
1.#允许bean Service循环依赖
spring.main.allow-circular-references=true

2.#Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
#同时使用Spring MVC和Spring Cloud Gateway
spring.main.web-application-type=reactive

3.SpringCloud-Gateway搭配服务注册Nacos进行lb动态路由遇到的问题
高版本的spring-cloud-alibaba-dependencies依赖中去除了ribbon依赖,若想能通过Gateway网关找到对应的微服务,需要引入loadbalancer依赖。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

参考的文档:http://www.ityouknow.com/springcloud/2019/01/19/spring-cloud-gateway-service.html


源码地址:https://github.com/tracysw/Spring-Cloud-Gateway-Samples


 

相关文章:

Spring-Cloud-Gateway-Samples,nacos为注册中心,负载均衡

背景&#xff1a;本想找个简单例子看下&#xff0c;无奈版本依赖太过复杂&#xff0c;花了点时间。记录下吧 使用Spring Cloud Gateway作为网关服务&#xff0c;Nacos作为注册中心&#xff0c;实现对子服务的负载均衡访问。简单例子。 一、gateway-main-nacos服务端&#xff…...

StarRocks Awards 2024 年度贡献人物

在过去一年&#xff0c;StarRocks 在 Lakehouse 与 AI 等关键领域取得了显著进步&#xff0c;其卓越的产品功能极大地简化和提升了数据分析的效率&#xff0c;使得"One Data&#xff0c;All Analytics" 的愿景变得更加触手可及。 虽然实现这一目标的道路充满挑战且漫…...

Autoencoder(李宏毅)机器学习 2023 Spring HW8 (Boss Baseline)

1. Autoencoder 简介 Autoencoder是一种用于学习数据高效压缩表示的人工神经网络。它由两个主要部分组成: Encoder 编码器将输入数据映射到一个更小的、低维空间中的压缩表示,这个空间通常称为latent space或bottleneck。 这一过程可以看作是数据压缩,去除冗余信息,仅保留…...

深入探索 ScottPlot.WPF:在 Windows 桌面应用中绘制精美图表的利器

一、ScottPlot.WPF 简介 ScottPlot.WPF 是基于 ScottPlot 绘图库专门为 Windows Presentation Foundation (WPF) 框架量身定制的强大绘图组件。它无缝集成到 WPF 应用程序中,为开发者提供了一种简洁、高效的方式来可视化数据,无论是科学研究中的实验数据展示、金融领域的行情…...

React中的useMemo 和 useEffect 哪个先执行?

在 React 组件的渲染过程中&#xff0c;useMemo 和 useEffect 的执行顺序是不同的。具体来说&#xff1a; useMemo 先执行&#xff1a;useMemo 是在 渲染阶段 执行的&#xff0c;它的作用是缓存计算结果&#xff0c;确保在渲染过程中可以直接使用缓存的值。 useEffect 后执行&…...

错误修改系列---基于RNN模型的心脏病预测(pytorch实现)

前言 前几天发布了pytorch实现&#xff0c;TensorFlow实现为&#xff1a;基于RNN模型的心脏病预测(tensorflow实现)&#xff0c;但是一处繁琐地方 一处错误&#xff0c;这篇文章进行修改&#xff0c;修改效果还是好了不少&#xff1b;源文章为&#xff1a;基于RNN模型的心脏病…...

Table-Augmented Generation(TAG):Text2SQL与RAG的升级与超越

当下AI与数据库的融合已成为推动数据管理和分析领域发展的重要力量。传统的数据库查询方式&#xff0c;如结构化查询语言&#xff08;SQL&#xff09;&#xff0c;要求用户具备专业的数据库知识&#xff0c;这无疑限制了非专业人士对数据的访问和利用。为了打破这一壁垒&#x…...

Stable Diffusion本地部署教程(附安装包)

想使用Stable Diffusion需要的环境有哪些呢? python3.10.11(至少也得3.10.6以上):依赖python环境NVIDIA:GPUgit:从github上下载包(可选,由于我已提供安装包,你可以不用git)Stable Diffusion安装包工具包: NVIDIA:https://developer.nvidia.com/cuda-toolkit-archiv…...

【物联网原理与运用】知识点总结(上)

目录 名词解释汇总 第一章 物联网概述 1.1物联网的基本概念及演进 1.2 物联网的内涵 1.3 物联网的特性——泛在性 1.4 物联网的基本特征与属性&#xff08;五大功能域&#xff09; 1.5 物联网的体系结构 1.6 物联网的关键技术 1.7 物联网的应用领域 第二章 感知与识别技术 2.1 …...

JuiceFS 2024:开源与商业并进,迈向 AI 原生时代

即将过去的 2024 年&#xff0c;是 JuiceFS 开源版本推出的第 4 年&#xff0c;企业版的第 8 个年头。回顾过去这一年&#xff0c;JuiceFS 社区版依旧保持着快速成长的势头&#xff0c;GitHub 星标突破 11.1K&#xff0c;各项使用指标增长均超过 100%&#xff0c;其中文件系统总…...

C#,动态规划问题中基于单词搜索树(Trie Tree)的单词断句分词( Word Breaker)算法与源代码

1 分词 分词是自然语言处理的基础&#xff0c;分词准确度直接决定了后面的词性标注、句法分析、词向量以及文本分析的质量。英文语句使用空格将单词进行分隔&#xff0c;除了某些特定词&#xff0c;如how many&#xff0c;New York等外&#xff0c;大部分情况下不需要考虑分词…...

计算机网络(六)应用层

6.1、应用层概述 我们在浏览器的地址中输入某个网站的域名后&#xff0c;就可以访问该网站的内容&#xff0c;这个就是万维网WWW应用&#xff0c;其相关的应用层协议为超文本传送协议HTTP 用户在浏览器地址栏中输入的是“见名知意”的域名&#xff0c;而TCP/IP的网际层使用IP地…...

上海亚商投顾:沪指探底回升微涨 机器人概念股午后爆发

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 市场全天探底回升&#xff0c;沪指盘中跌超1.6%&#xff0c;创业板指一度跌逾3%&#xff0c;午后集体拉升翻红…...

conda相关操作

conda 是一个开源的包管理和环境管理工具&#xff0c;主要用于 Python 和数据科学领域。它可以帮助用户安装、更新、删除和管理软件包&#xff0c;同时支持创建和管理虚拟环境。以下是关于 conda 的所有常见操作&#xff1a; 1. 安装 Conda Conda 通常通过安装 Anaconda 或 Mi…...

使用TCP协议实现智能聊天机器人

实验目的与要求 本实验是程序设计类实验&#xff0c;要求使用原始套接字编程&#xff0c;掌握TCP/IP协议与网络编程Sockets通信模型&#xff0c;并根据教师给定的任务要求&#xff0c;使用TCP协议实现智能聊天机器人。 &#xff08;1&#xff09;熟悉标准库socket 的用法。 …...

PHP二维数组去除重复值

Date: 2025.01.07 20:45:01 author: lijianzhan PHP二维数组内根据ID或者名称去除重复值 代码示例如下&#xff1a; // 假设 data数组如下 $data [[id > 1, name > Type A],[id > 2, name > Type B],[id > 1, name > Type A] // 重复项 ];// 去重方法 $dat…...

2025年01月11日Github流行趋势

项目名称&#xff1a;xiaozhi-esp32 项目地址url&#xff1a;https://github.com/78/xiaozhi-esp32项目语言&#xff1a;C历史star数&#xff1a;2433今日star数&#xff1a;321项目维护者&#xff1a;78, MakerM0, whble, nooodles2023, Kevincoooool项目简介&#xff1a;构建…...

备战蓝桥杯 队列和queue详解

目录 队列的概念 队列的静态实现 总代码 stl的queue 队列算法题 1.队列模板题 2.机器翻译 3.海港 双端队列 队列的概念 和栈一样&#xff0c;队列也是一种访问受限的线性表&#xff0c;它只能在表头位置删除&#xff0c;在表尾位置插入&#xff0c;队列是先进先出&…...

IT面试求职系列主题-Jenkins

想成功求职&#xff0c;必要的IT技能一样不能少&#xff0c;先说说Jenkins的必会知识吧。 1) 什么是Jenkins Jenkins 是一个用 Java 编写的开源持续集成工具。它跟踪版本控制系统&#xff0c;并在发生更改时启动和监视构建系统。 2&#xff09;Maven、Ant和Jenkins有什么区别…...

Vue篇-06

1、路由简介 vue-rooter&#xff1a;是vue的一个插件库&#xff0c;专门用来实现SPA应用 1.1、对SPA应用的理解 1、单页 Web 应用&#xff08;single page web application&#xff0c;SPA&#xff09;。 2、整个应用只有一个完整的页面 index.html。 3、点击页面中的导航链…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

1.3 VSCode安装与环境配置

进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件&#xff0c;然后打开终端&#xff0c;进入下载文件夹&#xff0c;键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

Mysql中select查询语句的执行过程

目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析&#xff08;Parser&#xff09; 2.4、执行sql 1. 预处理&#xff08;Preprocessor&#xff09; 2. 查询优化器&#xff08;Optimizer&#xff09; 3. 执行器…...

Web后端基础(基础知识)

BS架构&#xff1a;Browser/Server&#xff0c;浏览器/服务器架构模式。客户端只需要浏览器&#xff0c;应用程序的逻辑和数据都存储在服务端。 优点&#xff1a;维护方便缺点&#xff1a;体验一般 CS架构&#xff1a;Client/Server&#xff0c;客户端/服务器架构模式。需要单独…...

32单片机——基本定时器

STM32F103有众多的定时器&#xff0c;其中包括2个基本定时器&#xff08;TIM6和TIM7&#xff09;、4个通用定时器&#xff08;TIM2~TIM5&#xff09;、2个高级控制定时器&#xff08;TIM1和TIM8&#xff09;&#xff0c;这些定时器彼此完全独立&#xff0c;不共享任何资源 1、定…...

基于开源AI智能名片链动2 + 1模式S2B2C商城小程序的沉浸式体验营销研究

摘要&#xff1a;在消费市场竞争日益激烈的当下&#xff0c;传统体验营销方式存在诸多局限。本文聚焦开源AI智能名片链动2 1模式S2B2C商城小程序&#xff0c;探讨其在沉浸式体验营销中的应用。通过对比传统品鉴、工厂参观等初级体验方式&#xff0c;分析沉浸式体验的优势与价值…...

C++--string的模拟实现

一,引言 string的模拟实现是只对string对象中给的主要功能经行模拟实现&#xff0c;其目的是加强对string的底层了解&#xff0c;以便于在以后的学习或者工作中更加熟练的使用string。本文中的代码仅供参考并不唯一。 二,默认成员函数 string主要有三个成员变量&#xff0c;…...