Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Gateway by integrating gateway with springcloud

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how springcloud integrates gateway to achieve the gateway. The content is detailed and easy to understand, easy to operate, and has a certain reference value. I believe you will gain something after reading this article on how to integrate springcloud and gateway to realize the gateway. Let's take a look.

1. Project catalog:

Create the project gateway as the parent class

two。 Code implementation:

Parent class dependency

Org.springframework.boot spring-boot-starter-parent 2.6.2 com.cxh gateway 0.0.1-SNAPSHOT gateway Demo project for Spring Boot pom 8 2021.1 2021.0.0 org.springframework.cloud spring-cloud-dependencies $ {spring-cloud-dependencies.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba-dependencies.version} pom import

Create a module project gateway-client

Add dependency

Com.cxh gateway 0.0.1-SNAPSHOT com.cxh gateway-client 0.0.1-SNAPSHOT gateway-client Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test Test 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

@ RestControllerpublic class ClientController {@ Value ("${server.port}") private String port; @ RequestMapping ("/ index") public String index () {return "gateway-client Port:" + port;}}

Start the class to add comments

@ SpringBootApplication@EnableDiscoveryClientpublic class GatewayClientApplication {public static void main (String [] args) {SpringApplication.run (GatewayClientApplication.class, args);}}

Create a module project gateway-service

Add dependency

Com.cxh gateway 0.0.1-SNAPSHOT com.cxh gateway-service 0.0.1-SNAPSHOT gateway-service Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test Test 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-feign 1.4.3.RELEASE org.springframework.cloud spring-cloud-loadbalancer org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.2.10.RELEASE

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 # Settings routing id uri: lb://gateway-client # setting url lb://nacos Service Registration name for routes predicates:-Path=/ Client/** # path matching rule 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);}} 3. Achieve results:

After starting nacos, start the gateway-client, gateway-service project, and open the browser http://localhost:8001/client/index

Return information: gateway-client port: 8002

This is the end of the article on "how springcloud integrates gateway to achieve Gateway". Thank you for reading! I believe you all have a certain understanding of the knowledge of "springcloud Integration gateway how to achieve Gateway". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report