乐优商城--服务(二) : 网关微服务
网关微服务:LyGateway
在前边,我们使用
- Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现;
- 而服务间通过Ribbon或Feign实现服务的消费以及均衡负载;
- 通过Spring Cloud Config实现了应用多环境的外部化配置以及版本管理;
- 为了使得服务集群更为健壮,使用Hystrix的融断机制来避免在微服务架构中个别服务出现异常时引起的故障蔓延。
在该架构中,我们的服务集群包含:内部服务Service A和Service B,他们都会注册与订阅服务至Eureka Server,而Open Service是一个对外的服务,通过均衡负载公开至服务调用方。我们把焦点聚集在对外服务这块,直接暴露我们的服务地址,这样不太合理,因此避免对外服务地址的暴露,引入网关——Zuul
Zuul加入后的架构:
不管是来自于客户端(PC或移动端)的请求,还是服务内部调用。一切对服务的请求都会经过Zuul这个网关,然后再由网关来实现 鉴权、动态路由等等操作。Zuul就是我们服务的统一入口。
- 添加Zuul依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>leyou</artifactId>
<groupId>com.leyou.parent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.common</groupId>
<artifactId>ly-gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-zuul</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 开启Zuul功能:
@EnableZuulProxy //开启网关功能
@SpringCloudApplication
public class LyGateway {
public static void main(String[] args) {
SpringApplication.run(LyGateway.class,args);
}
}
- 编写配置:
server:
port: 10010 //服务端口
spring:
application:
name: api-gateway //指定服务名
# 添加Eureka配置,获取服务信息
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
# 修改映射配置,通过服务名称获取
zuul:
prefix: /api # 添加路由前缀
routes:
item-service: /item/** # 这里是映射路径
search-service: /search/**
user-service: /user/**
auth-service: /auth/**
cart-service: /cart/**
order-service: /order/**
upload-service:
path: /upload/**
serviceId: upload-service
strip-prefix: false
add-host-header: true
sensitive-headers:
# path: /upload/** 由于网关捕获到upload之后页面的url只剩下/image,但是controller里面是/upload/image,所以不能去除upload的前缀
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMillisecond: 5000 # 熔断超时时长:5000ms
ribbon:
ConnectTimeout: 1000 # 连接超时时间(ms)
ReadTimeout: 3500 # 通信超时时间(ms)
MaxAutoRetriesNextServer: 0 # 同一服务不同实例的重试次数
MaxAutoRetries: 0 # 同一实例的重试次数
ly:
jwt:
pubKeyPath: E:/course/JavaProject/javacode/idea/rsa/rsa.pub # 公钥地址
cookieName: LY_TOKEN
filter:
allowPaths:
- /api/auth
- /api/search
- /api/user/register
- /api/user/check
- /api/user/code
- /api/item
- /api/cart
总结:
- Eureka:注册中心
- Zuul:服务网关
- Ribbon:负载均衡
- Feign:服务调用
- Hystix:熔断器
以上均为SpringCloud的组件。