静 2019-06-27
在微服务架构中,一个系统会有多个微服务,例如service-user, service-shop,一个功能往往需要调用多个微服务来完成,zuul实现的网关类似于设计模式中的门面模式Facade,通常在网关层实现功能:
- 认证 来源的合法性校验
- 动态路由 隐藏内部接口,通过调用所配置路径转发至各微服务接口
- 安全 权限校验
- 服务迁移 简单化因业务变化而需要拆分/整合服务的工作
zuul 像其他微服务一样作为一个微服务向eureka server注册,并且能够通过注册列表获取所有的可用服务,其内部默认实现了ribbon,可达到负载均衡的目的,此时的微服务架构图如下:
无论是pc还是移动端调用首先经过zuul网关层,在这里会有一系列的filter(例如权限控制,接口认证等),验证合法后zuul放行并调用相关的微服务接口,在此架构中,需要说明通常的部署方法:zuul网关在系统的最外层暴露映射后的地址供外部客户端调用,而使用内部私有地址调用各微服务
zuul的配置还是放到配置中心统一管理,因此需要加入配置客户端、eureka客户端和zuul 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <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>
spring: application: name: service-zuul cloud: config: discovery: enabled: true service-id: service-config label: master profile: dev
server: port: 1100 zuul: ignoredServices: '*' #忽略所有未配置的service host: connect-timeout-millis: 20000 socket-timeout-millis: 20000 routes: user-service: #自定义名称 path: /user/** serviceId: service-user #/user/开头的路径转发至service-user微服务 hystrix: #hystrix配置 command: default: execution: isolation: thread: timeoutInMilliseconds: 2500 ribbon: #ribbon负载均衡参数配置 ReadTimeout: 5000 ConnectTimeout: 5000 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
在启动类CommonserviceZuulApplication添加注解@EnableZuulProxy以开启zuul特性:
package com.mayi.springcloud; 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; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class CommonserviceZuulApplication { public static void main(String[] args) { SpringApplication.run(CommonserviceZuulApplication.class, args); } }
创建一个熔断类:UserFallbackProvider, 代码取自官方文档最简单实现
package com.mayi.springcloud.fallback; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; @Component public class UserFallbackProvider implements FallbackProvider{ @Override public String getRoute() { return "service-user"; } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { return new ClientHttpResponse() { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("fallback".getBytes()); } @Override public String getStatusText() throws IOException { return "OK"; } @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public void close() { } }; } }
依次启动eureka、config、user、zuul ,user启动两个节点
访问:http://localhost:1100/user/listUsers
发现映射到了service-user微服务的listUsers接口,并实现了负载均衡。
此时,断掉其中一个service-user节点,继续访问以上地址
数秒后,故障节点被剔除
源码下载:https://github.com/tianyana/s...
欢迎加入JAVA架构师QQ群(初建):618578034
关注 微信公众号:java架构师修行
按照JAVA高级软件架构师实战培训的路线发布一期完整的架构文章,难度由浅入深,适合有一定开发基础想转架构和正在做初级架构开发的人员学习(springcloud更新完毕即开始)