在微服务架构中,须要多个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能 路由、配置管理等,由这个基础组件相互协做,共同组建了一个简单的微服务系统。一个简单的微服务系统以下 图java
总结:在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求先先通过负载均衡(zuul、 Ngnix),再到达服务网关(zuul集群),而后再到具体的服务。服务统一注册到高可用的服务注册中心集群,服务的全部的配置文件由配置服务管理,配置服务的配置文件仓库,方便开发人员随时改配置。spring
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,好比/api/user转发到到user服 务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能api
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
@EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(ServiceZuulApplication.class, args); } }
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/ server.port: 8769 spring.application.name: service-zuul #关闭安全认证 management.security.enabled=false #设置断路时间 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
注意:zuul中默认有hystrix断路器,若是响应时间超过1秒,断路器打开,这几乎是不可能的,因此必须调整断路时间 使用:当前zuul使用默认路由规则,访问:http:zuul端口/服务实例名/控制器方法 这是单节点的主流配置,若是须要配置zuul集群,须要配置Nginx代理,配置分发规则。从图一能够看出微服务当前的架构通过三次的负载均衡:安全