上面的是一个最基本的微服务架构
1:Eureka Server注册中心
2:服务集群里面的应用ServiceA,ServiceB向注册中心进行注册和订阅服务
3:Open ServiceA 和Open ServiceB是RestFullAPI服务,通过负载均衡等,对外提供接口的调用
基本功能都能实现,但实际上还是有很多缺陷
1:运维:系统的路由等配置需要运维人员手工配置,易出错
2:开发:每个服务都要有相应的权限控制,当接口数量比较大的时候,权限控制会产生很大的代码量
3:接口无法实现复用
springCloud的服务网关可以解决上面的问题SpringCloud Zuul
@SpringCloudApplication 注解在zuul依赖包下面 可以完美代替@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker三个注解
1:创建项目添加pom.xml依赖
4.0.0 com.smart zuul 0.0.1-SNAPSHOT jar zuul Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE UTF-8 UTF-8 1.8 Finchley.RC1 org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-zuul org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
2:springCloud主程序部分
package com.smart;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;//@SpringBootApplication@EnableZuulProxy@SpringCloudApplicationpublic class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); }}
3:配置文件
spring.application.name=api-gatewayserver.port=5555# routes to serviceIdzuul.routes.api-a.path=/api-a/**zuul.routes.api-a.serviceId=service-Azuul.routes.spi-a.name=service-Azuul.routes.api-b.path=/api-b/**zuul.routes.api-b.serviceId=service-B# routes to urlzuul.routes.api-a-url.path=/api-a-url/**zuul.routes.api-a-url.url=http://localhost:2222/eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
配置文件这里映射两个服务,service-A service-B,
4:启动eureka,service-A ,service-B,zuul
访问http://localhost:5555/api-a-url/add?a=1&b=2,
映射方式url映射,serviceId映射,这里发现serviceId映射方式并不能实现,报错,然而我并不清楚发生了什么
注:关于映射
这里就说下url映射吧
# routes to url
zuul.routes.api-a-url.path=/api-a-url/**
zuul.routes.api-a-url.url=http://localhost:2222/
当访问http://localhost:5555/api-a-url/add?a=1&b=2会涉及到一个转发,实际访问的是http://localhost:2222/add?a=1&b=2