在微服务架构中,须要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协做,共同组建了一个简单的微服务系统。一个简单的微服务系统以下图:
html
Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先通过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),而后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的全部的配置文件由配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。java
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,好比/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。
zuul有如下功能:git
继续使用上一节的工程。在原有的工程上,建立一个新的工程。web
其pom.xml文件以下:spring
<?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>sc-f-chapter1</artifactId> <groupId>com.tugohost</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.tugohost</groupId> <artifactId>service-zuul</artifactId> <version>1.0-SNAPSHOT</version> <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>
在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:sql
/** * @author: Tu9ohost */ @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @EnableDiscoveryClient public class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(ServiceZuulApplication.class,args); } }
加上配置文件application.yml加上如下的配置代码:apache
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8769 spring: application: name: service-zuul zuul: routes: api-a: path: /api-a/** serviceId: service-ribbon api-b: path: /api-b/** serviceId: service-feign
首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;api
依次运行这五个工程;打开浏览器访问:http://localhost:8769/api-a/hi?name=tugohost ;浏览器显示:浏览器
hi tugohost,i am from port:8762安全
打开浏览器访问:http://localhost:8769/api-b/hi?name=forezp ;浏览器显示:
hi tugohost,i am from port:8762
这说明zuul起到了路由的做用
zuul不只只是路由,而且还能过滤,作一些安全验证。继续改造工程;
/** * @author: Tu9ohost */ @Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s >>> %s",request.getMethod(),request.getRequestURI().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; } }
这时访问:http://localhost:8769/api-a/hi?name=tugohost ;网页显示:
token is empty
访问 http://localhost:8769/api-a/hi?name=tugohost&token=22 ; 网页显示:
hi tugohost,i am from port:8762