仅使用,无多少技术含量,权记于此以备忘。
微服务架构下的主要组件
服务注册和发现的组件:Consul、Etcd等
网关:Zuul、Spring Cloud Gateway等
容错框架:Hystrix
负载均衡器:Ribbon
Web服务调用客户端:Feign
以上组件在Spring Cloud中均有集成,很容易上手使用。虽然组件层出不穷,有的过时、又有新的类似的产生,但万变不离其宗,掌握一个后,同类通过类比可以迅速掌握。
what
微服务的网关,相当于一个反向代理服务器(此时与Nginx、Spring Cloud Gateway的功能类似),Netflix开发。这里记录Spring Cloud中集成Zuul、Consul的使用。
how it works
主要原理图如下,可见其主要是在请求前后进行一系列的filter;作为gateway,在最前方接收前端对各服务的请求,因此压力较大,Zuul用了netty来实现高并发能力。
how to use
引入依赖(这里服务注册组件用consul):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 如下依赖自身包含了spring boot starter web、actuator等依赖,故引如下依赖就是个SpringBoot项目了 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
主类:
@SpringBootApplication @EnableZuulProxy public class ZuulMain { public static void main(String[] args) { SpringApplication.run(ZuulMain.class, args); } }
配置:
server: port: 8084 spring: application: name: sensestudy-zuul cloud: consul: enabled: true host: localhost port: 8500 discovery: # serviceName: ${spring.application.name} tags: sensestudy, zuul healthCheckInterval: 15s fail-fast: false prefer-ip-address: true # health-check-path: ${server.servlet.context-path}/actuator/health zuul: # prefix: /sensestudy # path统一加前缀 ignored-services: '*' # 默认路由规则为按服务id来查找目标服务的地址,此配置禁用该默认行为,并由下面的routes配置路由规则 routes: sensestudy-acl: /acl/** app1: path: /coursecenter/** serviceId: sensestudy-coursecenter # url: http://www.baidu.com