当前位置:网站首页>Zuul gateway use
Zuul gateway use
2022-07-25 12:14:00 【dengjili】
Zuul Introduce
Zuul And Spring Cloud Gateway It's about the same , Recommend or use Spring Cloud Gateway, After all, Spring The family's , Higher priority . They are all with Nginx equally , It is mainly used for the reverse proxy of the server ; As long as it is reverse proxy , It takes so long to provide routing 、 monitor 、 elastic 、 Safety and other functions ; Generally speaking, it is a gateway , Because the data flows in and out from this way .
Zuul The core of this is the filter , Through these filters, we can extend a lot of functions , such as :
- Dynamic routing
Dynamically route client requests to different back-end services , Do some logical processing , For example, aggregating data returns from multiple services . - Request monitoring
It can monitor the requests of the whole system , Record detailed request response logs , Can be real-time statistics of the current system access and monitoring status . - Authentication
Authenticate every access request , Refuse illegal request , Protect the back-end services . - Pressure test
Stress testing is a very important job , For example, some e-commerce companies need to simulate more real user concurrency to ensure the stability of the system during major events . adopt Zuul Requests can be forwarded dynamically to the cluster of back-end services , You can also identify test traffic and real traffic , So as to do some special treatment . - Grayscale Publishing
Gray level release can guarantee the stability of the whole system , It can be found in the initial grayscale 、 Adjustment issues , In order to ensure its impact .

Zuul Easy to use
Add dependency configuration , modify pom.xml file
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Add configuration information , Modify file application.properties
spring.application.name=zuul-demo
server.port=2103
zuul.routes.test.path=/test/* zuul.routes.test.url=http://192.168.1.244:8081 Activate zuul, Add startup comment @EnableZuulProxy
@EnableZuulProxy
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
test , Application host rest Service interface 
Test the proxy gateway 
Integrate Eureka, Forward with the registration service name
Modify file application.properties
spring.application.name=zuul-demo
server.port=2103
#zuul.routes.test.path=/test/* #zuul.routes.test.url=http://192.168.1.244:8081 eureka.client.serviceUrl.defaultZone=http://192.168.1.244:8761/eureka/ start-up Eureka
Test gateway 
Zuul Routing configuration
The default forwarding rule is API default gateway + The name of the service accessed + Interface URI
Default rule example :
- API default gateway :http://localhost:2103.
- User service name :user-service.
- User login interface :/user/login.
eg. The above test address we visited :http://localhost:2103/eureka-provider-app/hello
- Specify specific service routes
zuul.routes.test.path=/test/* zuul.routes.test.url=http://192.168.1.244:8081 We will serve /test The first interfaces are forwarded to http://192.168.1.244:8081 On
- Route prefix
The destination address after forwarding , Add the prefix , Such as
zuul.prefix=v1.0

Zuul filter
be based on Servlet Implement filters , Implement reference classes com.netflix.zuul.http.ZuulServlet, Method service, The flow is shown in the following figure 
The corresponding code
try {
preRoute();
} catch (ZuulException e) {
error(e);
postRoute();
return;
}
try {
route();
} catch (ZuulException e) {
error(e);
postRoute();
return;
}
try {
postRoute();
} catch (ZuulException e) {
error(e);
return;
}
For example , Define a token filter ( Casually found an implementation class , Such as DebugFilter, Copy it )
public class TokenFilter extends ZuulFilter {
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
String token = request.getHeader("H-TOKEN-X");
if (StringUtils.isBlank(token) || token.length() < 10) {
currentContext.setSendZuulResponse(false);
currentContext.getResponse().setContentType("application/json; charset=utf-8");
currentContext.setResponseBody("invalide token.");
}
return null;
}
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 1;
}
}
Inject objects into spring In the container
@Configuration
public class FilterConfig {
@Bean
public TokenFilter tokenFilter() {
return new TokenFilter();
}
}
test ,Token Success stories 
test ,Token Failure cases 
explain error type , You can handle exceptions , But it will eventually jump to /error uri
In the actual development, refer to the example development , More into understanding can study classes FilterProcessor
边栏推荐
- 【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
- Learning to Pre-train Graph Neural Networks(图预训练与微调差异)
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置add参数在小提琴内部添加抖动数据点以及均值标准差竖线(jitter and mean_sd)
- 【GCN多模态RS】《Pre-training Representations of Multi-modal Multi-query E-commerce Search》 KDD 2022
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
- 【高并发】高并发场景下一种比读写锁更快的锁,看完我彻底折服了!!(建议收藏)
- aaaaaaaaaaA heH heH nuN
- Hystrix使用
- 【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
- Client open download, welcome to try
猜你喜欢
![[untitled]](/img/83/9b9a0de33d48f7d041acac8cfe5d6a.png)
[untitled]

scrapy爬虫爬取动态网站

Eureka使用记录

From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"

对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)

Musk's "eternal soul": half hype, half flicker

Unexpected rollback exception analysis and transaction propagation strategy for nested transactions

NLP知识----pytorch,反向传播,预测型任务的一些小碎块笔记

和特朗普吃了顿饭后写下了这篇文章

【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
随机推荐
'C:\xampp\php\ext\php_zip.dll' - %1 不是有效的 Win32 应用程序 解决
scrapy 设置随机的user_agent
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
Intelligent information retrieval(智能信息检索综述)
Solutions to the failure of winddowns planning task execution bat to execute PHP files
Start with the development of wechat official account
web编程(二)CGI相关
【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
利用wireshark对TCP抓包分析
Knowledge maps are used to recommend system problems (mvin, Ctrl, ckan, Kred, gaeat)
Hydrogen entrepreneurship competition | Liu Yafang, deputy director of the science and Technology Department of the National Energy Administration: building a high-quality innovation system is the cor
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化点状条带图、设置palette参数配置不同水平数据点的颜色、设置add参数在点状条带图中添加均值标准差竖线
Go 垃圾回收器指南
Brpc source code analysis (II) -- the processing process of brpc receiving requests
【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
客户端开放下载, 欢迎尝鲜
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
dirReader.readEntries 兼容性问题 。异常错误DOMException
基于TCP/IP在同一局域网下的数据传输