Yyds dry goods inventory springboot integrates gateway to realize gateway function

Grey Wolf_ cxh 2022-01-26 11:20:57 阅读数:60

yyds dry goods inventory springboot

1. Use scenarios :

The gateway can provide request routing and combination 、 Protocol conversion 、 Safety certification 、 Service authentication 、 Flow control and log monitoring services . There are many optional gateways , such as Nginx、、Linkerd 、eureka、 Spring Cloud Gateway、consul etc. .

Spring Cloud Gateway Make various judgments and handle incoming requests , For example, judge the validity of the request 、 Authority verification , Request address rewrite , Request parameters 、 Header information 、cookie Analysis and rewriting of information , Request rate control , Keep a log, etc . And these can be easily passed through Predicate and GatewayFilter To combine the implementation .

2. Code implementation

1 establish gateway-service service

Introduce dependencies

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.4</version>
</dependency>
<!-- Service registration / Discovery center dependency -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- The configuration center of the service depends on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!--fegin Components -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Client load balancing loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

yml To configure

server:
port: 8001

spring:
application:
name: gateway-service # service name
profiles:
active: dev # Environment settings
cloud:
gateway:
routes:
# Transparent transmission service
- id: gateway-client # Set the routing id( In theory, it can be written casually )
uri: lb://gateway-client # To set up a route url lb://nacos Service registration name
predicates:
- Path=/client/** # Path matching rules
filters:
- StripPrefix=1
- id: gateway-consumer
uri: lb://gateway-consumer
predicates:
- Path=/consumer/**
filters:
- StripPrefix=1
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

Cross domain configuration

@Configuration
public 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);
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

2 establish gateway-client service

Introduce dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Service registration -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<!-- The service call -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

yml To configure

server:
port: 8002

spring:
application:
name: gateway-client # service name
profiles:
active: dev # Environment settings
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos Service registration
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

The control layer requests

@RestController
public class TestController {

@RequestMapping("/index")
public String index(){
return "gateway-client";
}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Start class

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {

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

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

3. Realization effect

use nacos As a registry , start-up nacos Start after gateway-service, gateway-client project

stay nacos Discovery service registration succeeded

#yyds Dry inventory # springboot Integrate gateway Realize the gateway function _springboot

Make a request in the browser

 ​http://localhost:8001/client/index​

Send the request to gateway-client service , Return results

#yyds Dry inventory # springboot Integrate gateway Realize the gateway function _springboot_02

copyright:author[Grey Wolf_ cxh],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261120544834.html