当前位置:网站首页>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();
}
边栏推荐
- Oppo find x2 series release: 3k+120hz curved screen, DxO score first, top version 6999 yuan!
- You don't know about redis. Let me explain the underlying data structure of redis in detail
- Promise解决异步
- Chapter 8 using web sessions through rest
- JVM composition and memory model
- Exam summary on May 13, 2022
- Do you want to be dismissed? Let's take a look at the "exit tips" of programmers
- Take you to master makefile analysis
- 三星存储工厂又发生火灾!
- Feed stream application reconfiguration - Architecture
猜你喜欢

解决ip地址访问末位奇数通偶数不通,或者偶数通奇数不通的问题(云加密机连接云服务器时遇到的问题,全程记录,希望能给大佬们灵感)

Direct insertion sort of seven sorts

Parameter transmission of components

Jumpserver learning

带你掌握 Makefile 分析

20 character short domain name bypass replication

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

智能家居浪潮来袭,如何让机器看懂世界 【结尾有资料】

Bluetooth framework summary

Take byte offer in four rounds and answer the interview questions
随机推荐
If there is no reference ground at all, guess if you can control the impedance?
Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)
2022/3/10 考试总结
leetcode-470.用 Rand7() 实现 Rand10()
美国官员建议特朗普阻止英飞凌收购赛普拉斯
2022/4/11 考试总结
Quartus:Instantiation of ‘sdram_ model_ plus‘ failed. The design unit was not found.
Exam summary on May 31, 2022
Promise解决异步
Six employees have been confirmed! Samsung closed the turtle tail mobile phone factory for the third time!
Feed流应用重构-架构篇
Window localstorage properties and location objects
2022/4/8 exam summary
解决ip地址访问末位奇数通偶数不通,或者偶数通奇数不通的问题(云加密机连接云服务器时遇到的问题,全程记录,希望能给大佬们灵感)
浅谈数仓的数据治理
技术生涯10年,那些让我心动的技术书
leetcode-461.汉明距离
MeterSphere金融公司落地经验分享
51单片机内部外设:实时时钟(SPI)
2022/3/10 exam summary