package com.lovnx.zuul;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
/**
* Created by xujingfeng on 2017/4/1.
*/
public class CustomRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
public final static Logger LOGGER = LoggerFactory.getLogger(CustomRouteLocator.class);
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private ZuulProperties properties;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
LOGGER.info("执行了: setJdbcTemplate");
this.jdbcTemplate = jdbcTemplate;
}
public CustomRouteLocator(String servletPath, ZuulProperties properties) {
super(servletPath, properties);
LOGGER.info("执行了: CustomRouteLocator");
this.properties = properties;
LOGGER.info("servletPath:{}", servletPath);
}
// 父类已经提供了这个方法,这里写出来只是为了说明这一个方法很重要!!!
// @Override
// protected void doRefresh() {
// super.doRefresh();
// }
@Override
public void refresh() {
LOGGER.info("执行了: refresh");
doRefresh();
}
@Override
protected Map<String, ZuulRoute> locateRoutes() {
LOGGER.info("执行了: locateRoutes");
LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulRoute>();
// 从application.properties中加载路由信息
routesMap.putAll(super.locateRoutes());
// 从db中加载路由信息
routesMap.putAll(locateRoutesFromDB());
// 优化一下配置
LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>();
for (Map.Entry<String, ZuulRoute> entry : routesMap.entrySet()) {
String path = entry.getKey();
// Prepend with slash if not already present.
if (!path.startsWith("/")) {
path = "/" + path;
}
if (StringUtils.hasText(this.properties.getPrefix())) {
path = this.properties.getPrefix() + path;
if (!path.startsWith("/")) {
path = "/" + path;
}
}
values.put(path, entry.getValue());
}
return values;
}
private Map<String, ZuulRoute> locateRoutesFromDB() {
LOGGER.info("执行了: locateRoutesFromDB");
Map<String, ZuulRoute> routes = new LinkedHashMap<>();
List<ZuulRouteVO> results = jdbcTemplate.query("select * from gateway_api_define where enabled = true ",
new BeanPropertyRowMapper<>(ZuulRouteVO.class));
for (ZuulRouteVO result : results) {
if (org.apache.commons.lang3.StringUtils.isBlank(result.getPath())
/*|| org.apache.commons.lang3.StringUtils.isBlank(result.getUrl())*/) {
continue;
}
ZuulRoute zuulRoute = new ZuulRoute();
try {
org.springframework.beans.BeanUtils.copyProperties(result, zuulRoute);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
LOGGER.error("=============load zuul route info from db with error==============", e);
}
routes.put(zuulRoute.getPath(), zuulRoute);
}
return routes;
}
public static class ZuulRouteVO {
/**
* The ID of the route (the same as its map key by default).
*/
private String id;
/**
* The path (pattern) for the route, e.g. /foo/**.
*/
private String path;
/**
* The service ID (if any) to map to this route. You can specify a
* physical URL or a service, but not both.
*/
private String serviceId;
/**
* A full physical URL to map to the route. An alternative is to use a
* service ID and service discovery to find the physical address.
*/
private String url;
/**
* Flag to determine whether the prefix for this route (the path, minus
* pattern patcher) should be stripped before forwarding.
*/
private boolean stripPrefix = true;
/**
* Flag to indicate that this route should be retryable (if supported).
* Generally retry requires a service ID and ribbon.
*/
private Boolean retryable;
private String apiName;
private Boolean enabled;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isStripPrefix() {
return stripPrefix;
}
public void setStripPrefix(boolean stripPrefix) {
this.stripPrefix = stripPrefix;
}
public Boolean getRetryable() {
return retryable;
}
public void setRetryable(Boolean retryable) {
this.retryable = retryable;
}
public String getApiName() {
return apiName;
}
public void setApiName(String apiName) {
this.apiName = apiName;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Spring Cloud Zuul动态路由demo
共9个文件
java:5个
gitkeep:1个
properties:1个
1星 需积分: 24 37 下载量 55 浏览量
2017-09-21
09:18:28
上传
评论
收藏 7KB RAR 举报
温馨提示
基于Spring Cloud Zuul的动态路由实现demo,实现动态配置,实时刷新
资源推荐
资源详情
资源评论
收起资源包目录
Spring Cloud Zuul动态路由.rar (9个子文件)
ZuulDemo
src
test
resources
.gitkeep 0B
java
main
resources
db
schema.sql 703B
application.properties 600B
java
com
lovnx
zuul
config
CustomZuulConfig.java 1KB
CustomRouteLocator.java 5KB
GatewayApplication.java 752B
event
RefreshRouteService.java 965B
web
DemoController.java 1KB
pom.xml 2KB
共 9 条
- 1
资源评论
- u0116743322017-12-21github上有源码: https://github.com/lexburner/zuul-gateway-demo 你这个copy过来居然用5积分???????
ZhiYuanYe
- 粉丝: 965
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功