搭建Zuul API网关微服务
今天就亲手搭建一个简单的zull API网关微服务服务吧。
- Zuul是Netfiix OSS中的一员,是一个基于JVM路由和服务端的负载均衡器。提供路由、监控、弹性、安全等方面的服务框架。Zuul能够与Eureka、Ribbon、Hystrix等组件配合使用。
- API(接口) Gateway(网关)接口没有界面接口的
- 产生背景:
(1) 在面向服务架构和微服务背景下产生的,目的都是为了解耦,rpc远程调用产生的。
(2) 在设计API时需要考虑幂等性、安全性(https)防止篡改数据(验证签名)、使用网关拦截。
(3) 接口实现白名单和黑名单、接口使用http协议+json格式restful,目的为了跨平台。
(4) 要考虑高并发对接口服务实现保护,服务降熔断、隔离之类、最后使用同一API管理平台api swagger。
- 网关的作用:
(1) 网关可以拦截客户端的所有请求,对该请求进行权限控制、负载均衡、路由转发.
(2) 日志管理、接口调用监控等。
一、在spring-demo项目中新建一个cloud-Gateway模块
- 步骤和 搭建Eureka为注册中心的SpringCloud项目的步骤相同,可以参考eureka模块。
- 连接地址: https://blog.csdn.net/qq_38066812/article/details/109426982
二、添加gateway的pom依赖文件
<?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>cloud-demo</artifactId>
<groupId>yooo.yun.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-gateway</artifactId>
<dependencies>
<--! zuul依赖-->
<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-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.14</version>
<configuration>
<imageName>kun-gateway</imageName>
<dockerDirectory>${basedir}/src/main/docker</dockerDirectory>
<dockerHost>http://${docker.host}:2375</dockerHost>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、新建一个application.yml文件
server:
port: 5070
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka # eureka注册中心地址,每个微服务都需要先注册到注册中心上去,实现服务治理。
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
spring:
datasource:
url: jdbc:mysql://localhost:3306/tb_demo?characterEncoding=utf8&generateSimpleParameterMetadata=true&connectTimeout=5000&useSSL=false
zuul:
host:
socket-timeout-millis: 600000
connect-timeout-millis: 600000
# zull路由配置,实现路由转发
routes:
cloud-order:
path: /order/** # 路由转发到order模块
serviceId: cloud-order # 微服务模块ID,与新建的微服务模块名相同
strip-prefix: false
sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
cloud-user:
path: /user/** #路由转发到user模块
serviceId: cloud-user
strip-prefix: false
sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
四、新建GatewayApplication启动类
package yooo.yun.com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* @author WangJiao
* @since 2020/10/14
*/
@EnableZuulProxy //开启网关
@EnableEurekaClient // 将服务注册到注册中心去
@SpringBootApplication // 启动一个springBoot项目
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
五、启动gateway网关
- 启动顺序:
- 1、eureka, 2、gateway, 3、user和order模块
六、通过网关访问接口
- 默认访问规则:
(1)API网关地址:http://localhost:5070
(2)用户服务名称:user
(3)获取用户信息详情接口:/saas/user/{id} - 那么通过Zuul访问获取用户信息详情接口的规则是 http://localhost:5070/user/saas/user/123
- 访问结果
user模块接口
package yooo.yun.com.user;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author WangJiao
* @since 2020/10/14
*/
@RequestMapping(value = "/saas/user")
@RestController("userC")
public class UserController {
@GetMapping("/{id}")
public String detail(@PathVariable("id") long id) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("msg", "user 模块");
return jsonObject.toString();
}
}
order模块接口
-: 到这里说明你已经Get到了最简单的Zuul API网关搭建了。