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

创建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.lryc.cn/news/317382.html

相关文章:

  • 鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:StepperItem)
  • 游戏盾SDK是如何实现智能加速的?
  • 西井科技参与IATA全球货运大会 以AI绿动能引领智慧空港新未来
  • RPC通信原理(二)
  • Redis 淘汰策略
  • 游戏数据处理
  • Qt+FFmpeg+opengl从零制作视频播放器-14.程序Ubuntu移植
  • Go 语言中的 Cond 机制详解
  • 如何使用vue定义组件之——子组件调用父组件数据
  • 如何使用ArcGIS Pro生成带计曲线等高线
  • 蓝桥杯C++大学B组一个月冲刺记录2024/3/13
  • 计算机网络——Internet结构和ISP
  • E.接龙数列【蓝桥杯】/动态规划
  • cv2.cvtColor()将二维转化为彩色图像
  • 为什么 VSCode 不用 Qt 而要用 Electron?
  • 环信ChatroomUIKit功能详解——超详细介绍
  • 怎么读取springboot中的properties.yml配置文件里的配置值(亲测有效)
  • 18、设计模式之解释器模式(Interpreter)
  • cpp qt 一个奇怪的bug
  • 第6章:MATLAB文本数据处理进阶篇的目录 (MATLAB入门课程)
  • 软件杯 深度学习 opencv python 公式识别(图像识别 机器视觉)
  • vscode通过多个跳板机连接目标机(两种方案亲测成功)
  • C++基础复习003
  • Docker Commit提交
  • 百度现在应该怎么去做搜索SEO优化?(川圣SEO)蜘蛛池
  • 登录凭证------
  • matplotlib系统学习记录
  • 【DL】ML系统学习笔记 1
  • ffmpeg视频处理常用命令
  • 前端npm和yarn更换国内淘宝镜像