In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use API Gateway in Spring Cloud". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the API gateway in Spring Cloud.
First, why do you need a gateway?
Security:
1. The most important point is that the gateway can uniformly aggregate and expose the API interfaces of all services. When the external system calls the API interface, it is the API interface exposed by the gateway, so the external system does not need to know the complexity of calling each other among the services in the micro-service system. The micro-service system also protects the API interface of the seven internal micro-service units from being called directly from the outside, resulting in the disclosure of sensitive information of the service.
two。 The gateway can do user identity authentication and authority authentication, prevent illegal requests to operate the API interface, and protect the server.
Performance:
The combination of 1.zuul, ribbon and eureka can achieve routing and load balancing. Zuul can distribute request traffic to different service instances in the cluster according to the default polling policy.
Function:
1. It can realize load balancing, routing forwarding, real-time log output, authority control, system monitoring and so on.
two。 It can realize traffic monitoring and downgrade the service in the case of high traffic.
II. Gateway framework classification
1.Netflix Zuul,zuul is a recommended component of spring cloud, https://github.com/Netflix/zuul
-- this blog explains Zuul
2.Kong kong is a secondary development scheme based on Nginx+Lua, https://konghq.com/
3.orange, this open source program is developed by Chinese people, http://orange.sumory.com/
3. How to use the gateway (Zuul) = = > actual combat?
1.jar package dependency
Com.suning.drp
Drp-gateway
0.0.1
War
Drp-parent
Com.suning.drp
0.0.1
.. / drp-parent/pom.xml
Org.springframework.cloud
Spring-cloud-starter-zuul
Org.springframework.cloud
Spring-cloud-starter-netflix-zuul
Org.springframework.cloud
Spring-cloud-starter-netflix-eureka-client
two。 Configuration file
# eureka Registration Service Center
Server.port=80
Eureka.client.serviceUrl.defaultZone=$ {eurekaServiceDefaultZone}
# eurekaServiceDefaultZone values in different environments, example values in production, vars.prd.properties
EurekaServiceDefaultZone= http://10.235.5.28:8080/eureka,http://10.235.5.29:8080/eureka,http://10.235.5.30:8080/eureka
# Gateway name
Spring.application.name=tigbs-gateway
# reverse proxy, hide the IP of the service and access it through the project name
# forward to the management end with the request address of / cms/ access
Zuul.routes.cms.path=/cms/**
Zuul.routes.cms.serviceId=drp-cms
# forward to the management end with the request address of / portal/ access
Zuul.routes.portal.path=/portal/**
Zuul.routes.portal.serviceId=drp-portal
3. Startup class
/ / EnableZuulProxy enables zuul function
@ EnableZuulProxy
/ / EnableEurekaClient registers the gateway service as a client to the eureka service center
@ EnableEurekaClient
@ SpringBootApplication
Public class GateWayServiceApplication extends SpringBootServletInitializer {
Public static void main (String [] args) {
SpringApplication.run (GateWayServiceApplication.class, args)
}
@ Override
Protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {
Return builder.sources (GateWayServiceApplication.class)
}
}
4. Business code-verify whether the user has logged in on the client
@ Component
Public class TokenFilter extends ZuulFilter {
/ / the logic of specific filtering
Public Object run () throws ZuulException {
/ / get the context
RequestContext currentContext = RequestContext.getCurrentContext ()
HttpServletRequest request = currentContext.getRequest ()
String userToken = request.getParameter ("userToken")
If (StringUtils.isEmpty (userToken)) {
CurrentContext.setSendZuulResponse (false)
CurrentContext.setResponseStatusCode (401)
CurrentContext.setResponseBody ("userToken is empty")
Return null
}
/ / otherwise, the business logic is executed normally.
Return null
}
/ / determine whether the filter is effective. If true, it means filtering is required, then execute run ()
Public boolean shouldFilter () {
Return true
}
/ / the order in which the filter is executed. When a request has multiple filters in one phase, it needs to be executed sequentially according to the return value of the method.
/ / priority is 0. The smaller the number, the earlier the filter will be executed.
Public int filterOrder () {
Return 0
}
/ / filter types: four types of pre,post,routing,error. Pre means to intercept before the request
Public String filterType () {
Return "pre"
}
}
At this point, I believe you have a deeper understanding of "how to use the API gateway in Spring Cloud". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.