仅使用,无多少技术含量,权记于此以备忘。
Zuul
what:微服务的网关,相当于一个反向代理服务器(此时与Nginx、Spring Cloud Gateway的功能类似)。这里记录Spring Cloud中集成Zuul、Consul的使用。
how it works:
引入依赖(这里服务注册组件用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
说明:
默认路由规则:对于http://localhost:8080/sensestudy/myacl?userId=1,zuul会尝试从服务注册中心找id为myacl的服务并向其发起请求。通常通过ignored-services禁用默认规则并自己指定路由规则。
自定义路由规则:
sensestudy-acl: /acl/** :zuul会匹配符合后者指定的路径的请求,并转发到id为前者的服务上。这是最简写法,也可如上面配置中所示,用多行指定两者对应关系。
参考资料: