In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how springboot integrates gateway to achieve gateway function". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how springboot integrates gateway to achieve gateway function".
1. Use the scene:
The gateway can provide services such as request routing and composition, protocol conversion, security authentication, service authentication, flow control and log monitoring. There are many gateways to choose from, such as Nginx, Linkerd, eureka, Spring Cloud Gateway, consul and so on.
Spring Cloud Gateway makes all kinds of judgment and processing for incoming requests, such as judging the validity of requests, authority verification, request address rewriting, request parameters, header information, cookie information analysis and rewriting, request rate control, log retention, and so on. These can be easily realized through the combination of Predicate and GatewayFilter.
two。 Code implementation 1 creates a gateway-service service
Introduce dependency
Org.springframework.cloud spring-cloud-starter-gateway 3.0.4 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config Org.springframework.cloud spring-cloud-starter-openfeign 3.0.2 org.springframework.cloud spring-cloud-loadbalancer 3.0.2 org.springframework.cloud spring-cloud-starter-loadbalancer
Yml configuration
Server: port: 8001spring: application: name: gateway-service # Service name profiles: active: dev # Environment Settings cloud: gateway: routes: # transparent transfer Service-id: gateway-client # set routing id (in theory it can be written casually) uri: lb://gateway-client # set the url lb://nacos service registration name of the route Predicates:-Path=/client/** # path matching rules filters:-StripPrefix=1-id: gateway-consumer uri: lb://gateway-consumer predicates:-Path=/consumer/** filters:-StripPrefix=1
Cross-domain configuration
@ Configurationpublic class CorsConfig {@ Bean public CorsWebFilter corsFilter () {CorsConfiguration config = new CorsConfiguration (); config.addAllowedMethod ("*"); config.addAllowedOrigin ("*"); config.addAllowedHeader ("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (new PathPatternParser ()); source.registerCorsConfiguration ("/ *", config); return new CorsWebFilter (source);}} 2 create gateway-client service
Introduce dependency
Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.1.RELEASE org. Springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web
Yml configuration
Server: port: 8002spring: application: name: gateway-client # Service name profiles: active: dev # Environment Settings cloud: nacos: discovery: server-addr: 127.0.0.1 discovery 8848 # nacos Service Registration
Control layer request
@ RestControllerpublic class TestController {@ RequestMapping ("/ index") public String index () {return "gateway-client";}}
Startup class
@ SpringBootApplication@EnableDiscoveryClientpublic class GatewayClientApplication {public static void main (String [] args) {SpringApplication.run (GatewayClientApplication.class, args);}} 3. Realize the effect
Use nacos as the registration center, start nacos and then start the gateway-service and gateway-client project
Successfully registered with nacos Discovery Service
Initiate a request in the browser
Http://localhost:8001/client/index
In fact, the gateway sends the request to the gateway-client service and returns the result
Thank you for your reading. the above is the content of "how springboot integrates gateway to achieve gateway function". After the study of this article, I believe you have a deeper understanding of how springboot integrates gateway to achieve gateway function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.