2-13-Gateway网关-快速入门
搭建网关
1.新建springboot项目
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">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>cn.itcast.demo</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mygateway</artifactId>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置nacos相关信息便于服务注册发现
server:
port: 10088
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
cluster-name: HZ
application:
name: mygateway
网关路由
1.配置服务路由
注意:当路由有多个的时候 可能会出现异常 原因暂时未知 但是应该可以通过一个微服务一个前缀来实现隔离
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
cluster-name: HZ
gateway:
routes:
- id: userservice
uri: lb://userservice
predicates:
- Path=/user/**
- id: orderservice
uri: lb://orderservice
predicates:
# - Path=/order/**
- Path=/feignOrder/**
# - Path=/restOrder/**