Zuul、Fegin、Rebbion、Eureka一條龍
很多人都自然而然把zuul當成nginx那樣來在配置寫死節(jié)點配置來。實際上zuul和eureka的是可以完美配合,完全可以基于服務名
的注冊和發(fā)現(xiàn),來動態(tài)的實現(xiàn)服務的動態(tài)負載
和動態(tài)注冊
,從而達到不需要重啟zuul即可達到動態(tài)擴容的
。下文將介紹靜態(tài)配置路由表
和從Eureka發(fā)現(xiàn)服務
的兩種方式來加強Zuul的使用。
項目地址:
AG-Admin:http://git.oschina.net/geek_qi/ace-security
GateWay搭建
img
Maven依賴
<dpendency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
基于Eureka的application.yml(關(guān)鍵)
spring:
application:
name: ace-gate
thymeleaf:
mode: LEGACYHTML5
cache: false
redis:
database: 1
host: 127.0.0.1
pool:
max-active: 20
server:
port: 8765 #啟動端口
#開啟動態(tài)網(wǎng)關(guān)服務發(fā)現(xiàn)
ribbon:
eureka:
enabled: true
#配置zuul路由表,靜態(tài)路由
#zuul:
# #prefix: /techouse #為zuul設置一個公共的前綴
# #ignoredServices: '*'
# routes:
# ace-admin: #隨便定義,當不存在serviceId時,默認該值為serviceId(就是注冊服務的名稱,屬性spring.application.name)
# path: /test/** #匹配/test/** 均路由到cloud-client
#基于靜態(tài)路由節(jié)點的ribbon負載均衡配置
#ace-admin:
# ribbon:
# listOfServers: 127.0.0.1:8767
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
security:
user:
name: admin
password: admin
role: USER
#避免第一次調(diào)用失敗
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
#請求和響應GZIP壓縮支持
feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
啟用Zuul、Fegin、Eureka注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableZuulProxy
@EnableRedisHttpSession(redisFlushMode = RedisFlushMode.IMMEDIATE)
public class GateBootstrap {
public static void main(String[] args) {
SpringApplication.run(GateBootstrap.class, args);
}
}
Zuul filter攔截示例代碼
@Component
public class SessionAccessFilter extends ZuulFilter {
private final Logger log = LoggerFactory.getLogger(SessionAccessFilter.class);
@Autowired
private SessionRepository<?> repository;
@Autowired
private IUserService userService;
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpSession httpSession = ctx.getRequest().getSession();
if (不符合權(quán)限){
setFailedRequest("<h1>Forbidden!</h1>",403);
}
return null;
}
/**
* 路由重定向
*
* @param body
* @param code
*/
private void setFailedRequest(String body, int code) {
log.debug("Reporting error ({}): {}", code, body);
RequestContext ctx = RequestContext.getCurrentContext();
ctx.setResponseStatusCode(code);
if (ctx.getResponseBody() == null) {
ctx.setResponseBody(body);
ctx.setSendZuulResponse(false);
throw new RuntimeException("Code: " + code + ", " + body); //optional
}
}
}
訪問網(wǎng)關(guān)地址示例
[ip]:[zuul port]/[Eureak 注冊服務]/[資源路徑]
Zuul兼容動態(tài)注冊和靜態(tài)路由表
核心思路
基于Eureka上注冊另一個zuul,通過主的zuul gate way來分流到另外一個zuul,從而兼容靜態(tài)路由的情況。
img