1.在微服务架构中,需要几个基础的服务治理组件
(1)服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等
(2)这几个基础组件相互协作,共同组建了一个简单的微服务系统
(3)一个简答的微服务系统流程如下:
<1>客户端的请求首先经过负载均衡(Ngnix)
<2>再到达服务网关(zuul集群),然后再到具体的服务
<3>服务统一注册到高可用的服务注册中心集群
<4>服务的所有的配置文件由配置服务管理(下一篇文章讲述)
<5>配置服务的配置文件放在git仓库,方便开发人员随时改配置
2.Zuul简介回顾:
(1)Zuul的主要功能是路由转发和过滤器
<1>路由转发功能是微服务的一部分
1.1.比如/api/user转发到到user服务,/api/shop转发到到shop服务
<2>zuul默认和Ribbon结合实现了负载均衡的功能
(2)zuul有以下功能:
<1>Authentication:身份验证
<2>Stress Testing:压力测试
<3>Dynamic Routing:动态路由
<4>Service Migration:服务迁移
<5>Load Shedding:减载
<6>Security:安全
<7>Static Response handling:静态响应处理
<8>Active traffic management :主动交通管理
3.准备工作
(1)继续使用上面的工程,启动相应的工程
<1>启动EurkaServer,EurkaClient,RibbonService,FeignServer
<2>启动顺序第一个是注册中心EurkaServer,后续工程顺序随意
(2)在原有的工程基础上,继续新建一个工程ZuulService
4.创建ZuulService工程
(1)配置pom.xml文件如下:
<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>
<artifactId>ZuulService</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.bisien</groupId>
<artifactId>SpringCloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
(2)编写启动类ZuulServiceApplication,代码示例如下:
@SpringBootApplication
//通过注解@EnableZuulProxy,开启zuul的功能
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class ZuulServiceApplication {
public static void main(String[] args) {
SpringApplication.run( ZuulServiceApplication.class, args );}}
(3)编写配置文件application.properties加上以下的配置代码如下:
#端口
server.port=8005
eureka.instance.hostname=localhost
#指定服务注册中心的地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8000/eureka/
#服务名
spring.application.name:ZuulService
#以/api-a/开头的请求都转发给RibbonService服务
zuul.routes.api-a.path:/api-a/**
zuul.routes.api-a.serviceId:RibbonService
#以/api-b/开头的请求都转发给FeignServer服务
zuul.routes.api-b.path:/api-b/**
zuul.routes.api-b.serviceId:FeignServer
(4)依次运行EurkaServer,EurkaClient,RibbonService,FeignServer,ZuulService五个工程
<1>打开浏览器访问:http://localhost:8005/api-a/hi?name=张三
1.1.浏览器界面显示:hi 张三,i am from port:8001
<2>打开浏览器访问:http://localhost:8005/api-b/hi?name=张三
2.1.1.1.浏览器界面显示:hi 张三,i am from port:8001
<3>这说明zuul起到了路由的作用
4.Zuul网关服务过滤
(1)zuul不仅只是路由,并且还能过滤,做一些安全验证
(2)编写自定义过滤器继承自ZuulFilter代码示例如下:
@Component
public class MyFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
/**
* filterType:返回一个字符串代表过滤器的类型
* (1)在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
* <1>pre:路由之前
* <2>routing:路由之时
* <3>post: 路由之后
* <4>error:发送错误调用
*/
@Override
public String filterType() {return "pre";}
//filterOrder:过滤的顺序
@Override
public int filterOrder() {return 0;}
//shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤
@Override
public boolean shouldFilter() {return true;}
//run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
if(accessToken == null) {log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {ctx.getResponse().getWriter().write("token is empty");}
catch (Exception e){}
return null;}log.info("ok");return null;}}
(3)这时访问:http://localhost:8005/api-a/hi?name=张三
<1>网页显示:token is empty
(4)访问 http://localhost:8769/api-a/hi?name=张三&token=22
<2>网页显示:hi 张三,i am from port:8001
(5)说明Zuul网关服务过滤功能实现完成