创建Gateway网关
和Eureka一样创建gateway项目
pom文件如下
下面展示一些 内联代码片
。
<?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>
<groupId>com.artfess</groupId>
<artifactId>ats-api-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ats-api-gateway</name>
<description>Application Gateway</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
将application.yml配置文件改名为bootstrap.yml,因为有了配置中心,项目会自动根据配置中心配置去拉去配置文件,改为bootstrap.yml会优先执行,感兴趣的可以自行了解学习,
配置文件内容如下
spring:
application:
name: api-gateway
cloud:
config:
discovery:
enabled: true
service-id: config
profile: dev
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
routes: #这下面配置了一些用于测试的路由,具体请根据自己项目而#定
#/myProduct/product/list -> /product/product/list
myProduct:
path: /myProduct/**
serviceId: product
sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
login:
path: /login/**
serviceId: user
#sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
stripPrefix: false
static:
path: /static/**
serviceId: user
#sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
stripPrefix: false
# 简洁写法
# product: /myProduct/**
# 排除某些路由
#ignored-patterns:
# - /**/product/product/listForOrder
server:
port: 8086
启动服务,刷新Eureka页面( http://localhost:8761/ ),会多出一个api-gateway(根据配置文件里面的项目名称生成)服务,