目录
前言
假设目前公司有一个电商平台,电商平台的服务有很多:账号系统、商品系统、订单系统、优惠券系统、库存系统、物流系统等等,一般部署架构有两种方式:
1、“各自为政,不听总指挥”
这种部署方式有以下问题:
- 各个服务维护自己一套nginx配置、单独域名,增加运维的工作量和复杂度;
- 每个服务可能需要实现一套权限控制、限流控制、降级控制等通用功能,变成重复造轮子;
2、“团结力量,一致对外”
这种部署方式采用nginx做反向代理,统一访问域名shop.com,每个应用服务采用不同的访问前缀URI进行路由,这里解决了不同域名维护的问题;但并没有解决“重复造轮子”进行鉴权、限流、降级等。
这里可能有人会说:可以使用nginx+lua的方式进行鉴权、限流、降级等控制。对的,这是一种可行的方案,这时候nginx就类似于网关层gateway的作用了。
到这里,大家应该大致明白spring cloud gateway的定位是什么了。
- 统一流量入口,方便根据流量进行功能扩展:鉴权、限流、降级、日志监控、错误告警等功能;
- 解放应用层重复造轮子的麻烦,可以专注于各自业务功能实现;
- 依靠Eureka服务注册发现,动态负载均衡;
- 结合springboot生态,配置化实现,开发快速简单;
Gateway代码例子
注:基于springboot进行开发,已有eureka-server服务的情况下(eureka的搭建可以参考:https://blog.csdn.net/u012661488/article/details/106883249);
创建gateway服务
1、pom文件引入gateway的包
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example.gateway</groupId>
<artifactId>gateway-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway-server</artifactId>
<name>gateway-server</name>
<dependencies>
<!-- 引入gateway包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!-- 引入eureka客户端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、编写启动类GatewayServerApplication:添加注解@EnableDiscoveryClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
3、配置文件application.yml
server:
port: 8099
spring:
application:
name: gateway-server
# eureka注册中心
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4、实现请求路由
Gateway路由有两种配置方式:
第一种就是采用上yml配置方式:通过配置文件中的spring.cloud.gateway.routes路由列表,对不同应用进行配置路由规则;
server:
port: 8099
spring:
application:
name: gateway-server
cloud:
gateway:
discovery:
locator:
enabled: true
# 服务名小写
lower-case-service-id: true
# 路由配置列表
routes:
# 需要被访问的应用服务注册到eureka的名称
- id: account-web
# lb代表从注册中心获取服务,且已负载均衡方式转发
uri: lb://account-web
predicates:
- Path=/account/**
filters:
# 这里可以配置对应的过滤器工厂类,如MyTestGatewayFilterFactory
- MyTestGatewayFilterFactory
- id: order-web
uri: lb://order-web
predicates:
- Path=/order/**
filters:
- MyTestGatewayFilterFactory
# eureka注册中心
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
第二种采用代码方式配置路由:(可参考官网配置:https://spring.io/projects/spring-cloud-gateway)
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayRouteConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder){
return builder.routes()
.route("account-web", r -> r.path("/account/**")
.uri("lb://account-web")
.filters(new MyTestFilter())
)
.route("order-web", r -> r.path("/order/**")
.uri("lb://order-web")
.filters(new MyTestFilter())
)
.build();
}
}
创建应用服务
1、引入pom包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
2、配置application.properties
server.port=8082
spring.application.name=account-web
server.servlet.context-path=/account
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
3、编写启动类AccountWebApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class AccountWebApplication {
public static void main(String[] args) {
SpringApplication.run(AccountWebApplication.class, args);
}
}
4、测试TestController类
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test", produces = "application/json; charset=utf-8")
public class TestController {
@GetMapping(value = "/hello")
public String world() {
return "hello world";
}
}
测试
1、启动gateway-server服务
2、启动account-web服务
3、浏览器输入地址 http://localhost:8099/account/test/hello