创建SpringCloudGateWay

创建SpringCloudGateWay

本案例基于尚硅谷《谷粒商城》项目,视频27 创建测试API网关
1、创建module
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

输入gateway查找相关依赖
在这里插入图片描述

2、引入依赖

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.atguigu.gulimall</groupId><artifactId>gulimall-gateway</artifactId><version>0.0.1-SNAPSHOT</version><name>gulimall-gateway</name><description>API网关</description><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR3</spring-cloud.version></properties><dependencies><!--gulimall-common中依赖了服务注册与发现,将网关服务注册到服务中心--><dependency><groupId>com.atguigu.gulimall</groupId><artifactId>gulimall-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3、开启服务注册与发现
3.1

@EnableDiscoveryClient//加入该注解
@SpringBootApplication
public class GulimallGatewayApplication {public static void main(String[] args) {SpringApplication.run(GulimallGatewayApplication.class, args);}}

3.2 修改application.properties文件,将服务注册到nacos

#配置nacos注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#服务名称
spring.application.name=gulimall-gateway

3.3
在nacos页面创建网关命名空间
在这里插入图片描述
在这里插入图片描述
创建配置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

创建bootstrap.properties

spring.application.name=gulimall-gateway
#配置中心地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#命名空间id
spring.cloud.nacos.config.namespace=d6130a53-dcca-4e67-888c-a236b16f5f87

3.4

启动类添加排除数据库配置代码(exclude = DataSourceAutoConfiguration.class)

@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class GulimallGatewayApplication {public static void main(String[] args) {SpringApplication.run(GulimallGatewayApplication.class, args);}}```否则项目启动报错:```xml
***************************
APPLICATION FAILED TO START
***************************Description:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver classAction:Consider the following:If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).Process finished with exit code 1

3.5 配置网关模块的端口号为88
在这里插入图片描述

3.6 配置网关地址:
如果请求路径中有qq就跳转到https://www.qq.com
如果请求路径中有baidu,就跳转到https://www.baidu.com
新建一个
application.yml,内容如下

spring:cloud:gateway:routes:- id: test_routeuri: https://www.baidu.compredicates:- Query=url,baidu- id: test_routeuri: https://www.qq.compredicates:- Query=url,qq

4、测试如下
浏览器输入地址:
http://localhost:88/hello?url=qq
跳转到(实际访问的地址是 https://www.qq.com/hello 因为qq网址中没有hell):

浏览器输入地址:
http://localhost:88/hello?url=baidu
跳转如下(实际访问的地址是 https://www.baidu.com/hello 因为百度中也没有包含hell的地址):
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/743864.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring中的bean相关问题

Spring中的Bean的作用域主要有以下几种&#xff1a; singleton&#xff08;单例&#xff09;&#xff1a;在整个Spring IoC容器中&#xff0c;singleton作用域的Bean只会有一个实例存在。无论多少次请求容器提供该类型的Bean&#xff0c;容器都将返回同一个Bean实例。默认情况…

python中Unicode 数据库访问(Unicode Character Database)

此模块提供了对 Unicode Character Database (UCD) 的访问&#xff0c;其中定义了所有 Unicode 字符的字符属性。 此数据库中包含的数据编译自 UCD 版本 15.0.0。 该模块使用与 Unicode 标准附件 #44 “Unicode 字符数据库” 中所定义的相同名称和符号。 它定义了以下函数&…

WPF制作带图标和文字的按钮模板(通过附加属性实现)

1.界面模板代码部分 <Window.Resources><Style x:Key"IconButton" TargetType"Button"><Setter Property"Template"><Setter.Value><ControlTemplate TargetType"Button"><Border x:Name"borde…

(自用)Spring常用配置

spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mycnblog?characterEncodingutf8&useSSLfalse username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mvc: favicon: enable: false profiles: #多平台配置…

C++进阶--mep和set的模拟实现

红黑树链接入口 底层容器 模拟实现set和map时常用的底层容器是红黑树。 红黑树是一种自平衡的搜索二叉树&#xff0c;通过对节点进行颜色标记来保持平衡。 在模拟实现set和map时&#xff0c;可以使用红黑树来按照元素的大小自动排序&#xff0c;并且保持插入和删除操作的高效…

Set cancelled by MemoryScratchSinkOperator

Bug信息 Caused by: com.starrocks.connector.spark.exception.StarrocksInternalException: StarRocks server StarRocks BE{host=10.9.14.39, port=9060} internal failed, status code [CANCELLED] error message is [Set cancelled by MemoryScratchSinkOperator]Bug产生的…

使用 mapstructure 解析 json

介绍 先来介绍一下 mapstructure 这个库主要用来做什么的吧&#xff0c;官网是这么介绍的&#xff1a; mapstructure 是一个 Go 库&#xff0c;用于将通用映射值解码为结构&#xff0c;反之亦然&#xff0c;同时提供有用的错误处理。 该库在解码数据流&#xff08;JSON、Gob …

微信名【无感】的同学,你还好吗?

今天遇到个选择了微信一对一服务的同学&#xff0c;问Python问题&#xff0c;问题比较简单。 回答完问题&#xff0c;我就说了一句&#xff1a;问题比较简单&#xff0c;随意打赏一个红包就行了。 然后我就被拉黑了&#xff0c;然后我的解答问题&#xff0c;收到了一堆投诉&…

Java算法-力扣leetcode-380. O(1) 时间插入、删除和获取随机元素

380. O(1) 时间插入、删除和获取随机元素 实现RandomizedSet 类&#xff1a; RandomizedSet() 初始化 RandomizedSet 对象bool insert(int val) 当元素 val 不存在时&#xff0c;向集合中插入该项&#xff0c;并返回 true &#xff1b;否则&#xff0c;返回 false 。bool rem…

深入解析Java中锁机制以及底层原理

一、概述 1.1 背景 概念&#xff1a;锁是多线程编程中的机制&#xff0c;用于控制对共享资源的访问。可以防止多个线程同时修改或读取共享资源&#xff0c;从而保证线程安全。 作用&#xff1a;锁用于实现线程间的互斥和协调&#xff0c;确保在多线程环境下对共享资源的访问顺…

Flutter开发入门——Widget和常用组件

1.什么是Widget&#xff1f; 在Flutter中几乎所有的对象都是一个 widget 。与原生开发中“控件”不同的是&#xff0c;Flutter 中的 widget 的概念更广泛&#xff0c;它不仅可以表示UI元素&#xff0c;也可以表示一些功能性的组件如&#xff1a;用于手势检测的 GestureDetecto…

spring中事务失效的场景有哪些?

异常捕获处理 在方法中已经将异常捕获处理掉并没有抛出。 事务只有捕捉到了抛出的异常才可以进行处理&#xff0c;如果有异常业务中直接捕获处理掉没有抛出&#xff0c;事务是无法感知到的。 解决&#xff1a;在catch块throw抛出异常。 抛出检查异常 spring默认只会回滚非检…

对象的组合复用学习笔记

简单说&#xff0c;就是不同类的多个对象之间彼此调用对方的方法和变量&#xff0c;可能会多次调用&#xff0c;所以叫复用(重复)&#xff0c;所有对象本身具有独立的功能&#xff08;方法&#xff09;&#xff0c;共同完成一项任务的一部分&#xff0c;或者多个类的对象协助一…

ChatGPT浪潮来袭!谁先掌握,谁将领先!

任正非在接受采访时说 今后职场上只有两种人&#xff0c; 一种是熟练使用AI的人&#xff0c; 另一种是创造AI工具的人。 虽然这个现实听起来有些夸张的残酷&#xff0c; 但这就是我们必须面对的事实 &#x1f4c6; 对于我们普通人来说&#xff0c;我们需要努力成为能够掌握…

基于STM32的智慧农业管理系统设计与实现

文章目录 一、前言1.1 项目介绍【1】项目功能【2】设计实现的功能【3】项目硬件模块组成 1.2 设计思路1.3 传感器功能介绍1.4 开发工具的选择 二、EMQX开源MQTT服务器框架三、购买ECS云服务器3.1 登录官网3.2 购买ECS服务器3.3 配置安全组3.4 安装FinalShell3.5 远程登录到云服…

xsslabs靶场通关(持续更新)

文章目录 前言一、level1思路实现 二、levle2思路 三、level3思路实现 四、level4思路实现 五、level5思路实现 六、level6思路实现 七、level7思路实现 八、level8思路实现 九、level9思路实现 前言 本篇文章将介绍在xsslabs这个靶场&#xff08;在不知道源码的前提下&#x…

Linux从0到1——Linux环境基础开发工具的使用(上)

Linux从0到1——Linux环境基础开发工具的使用&#xff08;上&#xff09; 1. Linux软件包管理器yum1.1 yum介绍1.2 用yum来下载软件1.3 更新yum源 2. Linux编辑器&#xff1a;vi/vim2.1 vim的基本概念2.2 vim的基本操作2.3 vim正常模式命令集2.4 vim底行模式命令集2.5 视图模式…

Java初阶数据结构队列的实现

1.队列的概念 1.队列就是相当于排队打饭 2.在排队的时候就有一个队头一个队尾。 3.从队尾进对头出 4.所以他的特点就是先进先出 所以我们可以用链表来实现 单链表实现要队尾进队头出{要有last 尾插头删} 双向链表实现效率高&#xff1a;不管从哪个地方当作队列都是可以的&…

OpenMP 编程模型

OpenMP 内存模型 共享内存模型&#xff1a; OpenMP 专为多处理器/核心、共享内存机器设计&#xff0c;底层架构可以是共享内存UMA或NUM OpenMP 执行模型 基于线程的并行&#xff1a; OpenMP 程序基于多线程来实现并行&#xff0c; 线程是操作系统可以调度的最小执行单元。 …

Database Connection Pool 数据库连接池-01-概览及简单手写实现

拓展阅读 第一节 从零开始手写 mybatis&#xff08;一&#xff09;MVP 版本。 第二节 从零开始手写 mybatis&#xff08;二&#xff09;mybatis interceptor 插件机制详解 第三节 从零开始手写 mybatis&#xff08;三&#xff09;jdbc pool 从零实现数据库连接池 第四节 从…