一、网关介绍
1.1 微服务网关
1.2 介绍
API Gateway,是系统的唯一对外的入口,介于客户端和服务器端之间的中间层,处理非业务功能 提供路由请求、鉴权、监控、缓存、限流等功能
统一接入
智能路由
AB测试、灰度测试
负载均衡、容灾处理
日志埋点(类似Nignx日志)
流量监控
限流处理
服务降级
安全防护
鉴权处理
监控
机器网络隔离
1.3 主流的网关
- zuul:是Netflix开源的微服务网关,和Eureka,Ribbon,Hystrix等组件配合使用,Zuul 2.0比1.0的性能提高很多
- kong: 由Mashape公司开源的,基于Nginx的API gateway
- nginx+lua:是一个高性能的HTTP和反向代理服务器,lua是脚本语言,让Nginx执行Lua脚本,并且高并发、非阻塞的处理各种请求
二、网关配置
2.1 搭建springboot模板项目
2.2 修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hanergy</groupId>
<artifactId>out</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zuul-server</name>
<description>zuul project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<docker.image.prefix>zuul-server</docker.image.prefix>
</properties>
<dependencies>
<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>
</dependency>
<!-- zipkin链路追踪 包含了sleuth包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--配置中心客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</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>
<finalName>zuul-server</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.3</version>
<!--将插件绑定在某个phase执行-->
<executions>
<execution>
<id>build-image</id>
<!--将插件绑定在package这个phase上。也就是说,用户只需执行mvn package ,就会自动执行mvn docker:build-->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<dockerHost>http://10.4.237.123:2375</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3 启动类添加网关注解
2.4 服务配置
网关整合git配置中心
本地项目配置:bootstrap.yml
----------------------------------------------------------------------------------------------------------------------------------------
#指定注册中心地址
eureka:
instance:
# 配置注册中心Status显示,否则会出现读取了docker配置中心id,无法读取到配置中心配置
instance-id: ${spring.cloud.client.ip-address}:${server.port}
# 打开注册中心Status显示修改
prefer-ip-address: true
hostname: ${spring.cloud.client.ip-address}
hystrix:
enabled: true
client:
serviceUrl:
# 注册中心地址
#defaultZone: http://localhost:8761/eureka/
defaultZone: http://10.4.237.123:8761/eureka/
spring:
application:
# 服务名称
name: getway
# 配置中心
cloud:
config:
#discovery:
#第一种集成配置中心的方式,通过config的service-id映射 这种方式本地环境无法连接阿里配置中心服务 因为映射的是内网ip 本地无法访问
# 配置中心注册名称 默认去{label}/getway-{profile}.yml文件注册中心命名规则{name}-{profile}.yml /{name}/{profile}/{label}
# 配置中心注册名称 默认去{label}getway-{profile}.yml文件
#service-id: SPRINGCLOUD-CONFIG
#enabled: true
# 后缀 dev|prod
profile: dev
# dev|master分支,建议使用它区分生产、测试环境
label: dev
# 第二种集成配置中心的方式,这里写配置中心服务的地址信息
uri: http://39.106.68.199:8991
----------------------------------------------------------------------------------------------------------------------------------------
网关会去git配置中心 {label}分支读取 getway-{profile}.yml配置文件
测试环境 gatway网关git配置
dev分支
getway-dev.yml
----------------------------------------------------------------------------------------------------------------------------------------
# 端口
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 9000
connection-timeout: 5000ms
# 服务名称
spring:
zipkin:
# zipkin链路追踪地址,一般是网上下载zipkin jar包 java -jar启动服务
base-url: http://localhost:9411/
sleuth:
sampler:
probability: 1 #采样百分比设置为1表示100%,生产环境可以设置为0.1
# 网关配置访问服务名 访问oameeting的时候自动访问order服务 可以配置工具项目访问方式
zuul:
routes:
#order: /oameeting/**
#配置gatway拦截, 访问地址带oameeting的会自动调用注册名称为oa的接口
oa: /oameeting/**
ignored-services: order # 取消跳过网关直接调用服务接口方式。取消整个oa访问方式 /*-service/**
host:
connect-timeout-millis: 8000 #zuul默认超时时长 默认2s
socket-timeout-millis: 10000 # socket链接超时 默认10s
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
----------------------------------------------------------------------------------------------------------------------------------------
用户通过网关访问服务规则:
http://gateway:port/service-id/**
例子:默认 /order-service/api/v1/order/save?user_id=2&product_id=1