创建SpringCloudGateWay
本案例基于尚硅谷《谷粒商城》项目,视频27 创建测试API网关
1、创建module
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的地址):