当前位置:网站首页>The prefix is not removed when zuul gateway automatically routes
The prefix is not removed when zuul gateway automatically routes
2022-07-27 22:57:00 【Xiao Wang with one year's experience】
Zuul The prefix is not removed when the gateway automatically routes
background :
Recently, all micro services have to add context-path, Cause if used zuul When the gateway accesses the service, it needs to write two microservice path prefixes :
http://localhost:8017/demoxx/demo-xx/web/findname
Normal should be :/demo-xx
http://localhost:8017/demoxx/web/findname
server:
servlet:
context-path: /demoxx
spring:
application:
name: demoxx
Why is that? ?
because zuul The gateway does not have all services configured separately service-id The prefix is automatically removed by default , But I don't use the configuration file to configure all ServiceId, Because it's not flexible , Every time you add a micro service, you need to modify the configuration file and restart the gateway service . Therefore, the registry service is used to discover automatic routes , But using automatic routing will remove the prefix , Look at the source code , The switch that removes the prefix is only configured separately ServiceId Can only be configured when , So it doesn't apply , But the default source code is true, So rewrite it CustomRouteLocator Of locateRoutes Method , And will zuulRoute.setStripPrefix(false) So you can succeed .
Configure all services separately service-id;
routes:
demoxx:
strip-prefix: false
path: /demo-xx
demoxx2:
strip-prefix: false
path: /demo-xx2
rewrite CustomRouteLocator, Be careful DiscoveryClient, This is pulled from the Registration Center serviceid:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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.util.StringUtils;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class CustomRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
public final static Logger logger = LoggerFactory.getLogger(CustomRouteLocator.class);
@Autowired
private DiscoveryClient discoveryClient;
public CustomRouteLocator(String servletPath, ZuulProperties properties) {
super(servletPath, properties);
logger.info("servletPath:{}", servletPath);
}
@Override
public void refresh() {
doRefresh();
}
@Override
protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
LinkedHashMap<String, ZuulProperties.ZuulRoute> values = new LinkedHashMap<>();
// Get all the services
List<String> services = discoveryClient.getServices();
int id = 0;
for (int i = 0; i < services.size(); i++) {
String s = services.get(i);
List<ServiceInstance> instances = discoveryClient.getInstances(s);
for (int j = 0; j < instances.size(); j++) {
ServiceInstance serviceInstance = instances.get(j);
ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
zuulRoute.setId(id + "");
zuulRoute.setServiceId(serviceInstance.getServiceId());
zuulRoute.setUrl(serviceInstance.getUri().toString());
zuulRoute.setRetryable(false);
// Here's the key , Set to false
zuulRoute.setStripPrefix(false);
String path = "/" + s + "/**";
zuulRoute.setPath(path);
values.put(path, zuulRoute);
id++;
}
}
return values;
}
}
ZuulConfig:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ZuulConfig {
@Autowired
ZuulProperties zuulProperties;
@Autowired
ServerProperties server;
@Bean
public CustomRouteLocator routeLocator() {
CustomRouteLocator routeLocator = new CustomRouteLocator(server.getServlet().getServletPrefix(), this.zuulProperties);
return routeLocator;
}
}esh();
}
边栏推荐
- Take byte offer in four rounds and answer the interview questions
- 2022年软件开发的趋势
- Nodejs NPM common instructions summary
- JVM composition and memory model
- 图论的小技巧以及扩展
- 你不知道的Redis那些事,我来详解Redis底层数据结构
- Preparation of peptide kc2s modified albumin nanoparticles / targeting peptide GX1 modified human serum albumin nanoparticles probe
- Cy3 fluorescent labeling antibody / protein Kit (10~100mg labeling amount)
- Jumpserver learning
- 组件的传参
猜你喜欢

Object creation process and object layout

It's time to say goodbye gracefully to nullpointexception

Do you want to be dismissed? Let's take a look at the "exit tips" of programmers

Build your own website (22)

Chrome realizes automated testing: recording and playback web page actions

网络开发套接字以及UDP、TCP协议

Data warehouse project is never a technical project

一篇搞定Redis中的BigKey问题

The follow-up is coming. Whether it's OK without reference, let's make it clear to everyone at once!

物联网架构完全指南
随机推荐
SparkSQL的UDF及分析案例,220726,,
2022/6/5考试总结
我与消息队列的八年情缘
Promise解决异步
Two dimensional code generation based on MCU and two dimensional code display on ink screen
传华为再度砍单!供应链厂商更难了
Analysis on data collection and analysis of network security competition in national vocational college skill competition
PyQt5快速开发与实战 4.10 窗口绘图类控件
leetcode-470.用 Rand7() 实现 Rand10()
Kubernetes binary deployment - theoretical part
一篇搞定Redis中的BigKey问题
Oppo find x2 series release: 3k+120hz curved screen, DxO score first, top version 6999 yuan!
Direct insertion sort of seven sorts
TFRecord的Shuffle、划分和读取
Parameter transmission of components
Take byte offer in four rounds and answer the interview questions
ADI, Shijian and Junlong technology jointly donated 2.3 million yuan to help fight the epidemic in Hubei
2022/4/11 考试总结
2022年软件开发的趋势
2022/4/8 exam summary