一、简介
在微服务架构中,我们的服务往往由多个微服务组成,而这些服务可能部署在不同机房、不同地区、不同域名下。这种情况下,客户端(例如浏览器、手机、软件工具等)想要直接请求这些服务(比如我们请求: http://10.130.3.88:7001/test/getPort ),就需要知道它们具体的地址信息,例如 IP 地址、端口号等这种方式存在以下问题:
- 客户端需要维护大量的服务地址,这对于客户端来说,是非常繁琐复杂的
- 当服务地址更改时,客户端维护更麻烦,每个使用到的都要更改
- 身份认证的难度大,每个微服务需要独立认证
- 可能会存在跨域请求的问题。
这个时候我们的Spring Cloud Gateway 就很好的解决这些问题了,它的主要功能特征如下:
- 基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0 构建
- 能够在任意请求属性上匹配路由
- predicates(断言) 和 filters(过滤器)是特定于路由的
- 集成了 Hystrix 熔断器
- 集成了 Spring Cloud DiscoveryClient(服务发现客户端)
- 易于编写断言和过滤器
- 能够限制请求频率
- 能够重写请求路径
其实说这么多,还是很懵,直接说,怎么用吧?来来来,干起来!!!
二、配置
2.1、pom文件
pom.xml
<?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>cn.alian.microservice</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>gateway-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway-service</name>
<description>网关服务</description>
<dependencies>
<!-- 健康检查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 使用okhttp3 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
</dependencies>
</project>
为了使其简单,我这里使用okhttp。其他的依赖相信大家都耳熟能详了,就不过多的解释了。
三、配置文件
3.1、application.properties
#服务名
spring.application.name=gateway-service
#端口
server.port=9999
#gateway开启服务注册和发现的功能
spring.cloud.gateway.discovery.locator.enabled=true
#将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了)
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
#与Eureka注册服务中心的通信zone和url地址
eureka.client.serviceUrl.defaultZone=http://10.130.3.222:8761/eureka
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
eureka.client.register-with-eureka=false
#该实例,相较于hostname是否优先使用IP
eureka.instance.prefer-ip-address=true
#跨域问题
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedOrigins=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedHeaders=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods=*
#相同header多个值时的处理方式,三种规则可选(RETAIN_FIRST|RETAIN_UNIQUE|RETAIN_LAST)
#spring.cloud.gateway.default-filters[0]=DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_FIRST
#actuator健康检查
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=localhost
#部署时配置加上
#logging.config=config/logback.xml
感觉配置已经说得很明白了就不再解释了,这里稍微说明下,配置中心和网关中心都可以注册到Eureka,但是网关服务没有连接数据库的情况下,就没必要弄成配置中心的客户端了,它主要还是做路由的作用,并且它的地址是需要配置到配置中心,让其他服务获取的。
四、主类
GatewayServiceApplication.java
package com.alian.microservice.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonConfiguration;
@RibbonClients(defaultConfiguration = {OkHttpRibbonConfiguration.class})
@EnableEurekaClient
@SpringBootApplication
public class GatewayServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceApplication.class, args);
}
}
全局配置负载均衡算法: @RibbonClients(defaultConfiguration = {OkHttpRibbonConfiguration.class})
- RibbonClients设置的必须为@Configuration类
- 该@Configuration类不能被@ComponentScan引用
- 不能与@SpringBootApplication类在同一个包下(默认扫描的位置是当前启动类所在的包以及启动类所在包下面的所有可扫描注解),否则将作为共享方法使用,无法针对单个服务配置。
到这里我们的网关中心也可以使用了,启动完成后提供服务的地址是:
- 网关中心的地址是: http://10.130.3.222:9999