简介
基于上面几篇的基础,我们已经搭建好了项目,搭建好了注册中心eureka,搭建好了测试微服务,下面来搭建网关,前面几篇参考
搭建eureka
搭建微服务
关于spring cloud api gateway的详细介绍,与zuul的区别,选型等问题详细参考文章
网关对比文章
spring cloud gateway
微服务网关spring cloud gateway
搭建gateway
1.new-module 创建gateway maven项目
2.pom中添加依赖
<dependencies>
<!--gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring boot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.11.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 是否替换@xx@表示的maven properties属性值 -->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<profileActive>pre</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
spring-cloud-starter-gateway 是gateway的依赖
3.添加配置文件 application.yml
spring:
profiles:
active: @profileActive@
application:
name: gateway
server:
port: 8080
eureka:
instance:
preferIpAddress: true
client:
registryFetchIntervalSeconds: 5
4.添加环境配置application-dev.yml
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lowerCaseServiceId: true
spring.cloud.discovery.locator.enabled=true 此路由配置项代表自动转发到注册中心中已有的服务
lowerCaseServiceId 代表无需大写服务名
5.添加gateway启动类GatewayApplication
@EnableEurekaClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
因为gateway也是一个微服务注册到注册中心,而且会从注册中心拉取注册的服务来转发请求,所以也要EnableEurekaClient
6.至此gateway配置完成,可直接启动
启动gateway后可以在eureka查看注册信息
7.测试gateway是否具有转发功能,这时需要用到上一篇已经注册到eureka的微服务zjk,使用网关请求服务方式:http://网关地址/服务名/具体请求mapping
以ZJK微服务为例,上一节测试方法如下
http://localhost:8024/zjkTest
网关测试 http://localhost:8080/zjk/zjkTest
如果返回结果一致代表网关转发成功
如果配置文件中没写 lowerCaseServiceId: true 这里的服务名要大写ZJK
8.本文只是实现了个裸的网关,只做了左右转发功能,微服务中网关除了路由转发还有比较大的用途是鉴权/审计/限流,关于这一部分的应用后面再详细举例介绍。