当前位置:网站首页>Unified gateway

Unified gateway

2022-06-26 00:47:00 Free dream programmer

Gateway

effect

 Insert picture description here
 Insert picture description here
 Insert picture description here

Quick start

 Insert picture description here
New module gateway, Add dependency

<dependencies>
        <!--nacos Service registration found dependencies -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- gateway gateway rely on -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

Create startup class


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

 Insert picture description here
PS:lb Namely loadBalance( Load balancing ) Abbreviation
Modify the configuration

server:
  port: 10010
logging:
  level:
    cn.itcast: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: nacos:8848 # nacos Address 
    gateway:
      routes:
        - id: user-service #  Route marking , Must be unique 
          uri: lb://userservice #  The destination address of the route 
          predicates: #  Route assertion , Determine whether the request meets the rules 
            - Path=/user/** #  Path assertion , Determine whether the path is based on /user start , If yes, it meets 
		- id: order-service
          uri: lb://orderservice
          predicates:
            - Path=/order/**

function gateway modular
 Insert picture description here
 Insert picture description here

Route assertion factory

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

Filter configuration for routing

 Insert picture description here
 Insert picture description here

 Insert picture description here
modify usercontroller file
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

Global filter

 Insert picture description here
 Insert picture description here
stay gateway Create a new class under the package AuthorizeFilter

// @Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
        // 1. Get request parameters 
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String, String> params = request.getQueryParams();
        // 2. Get... In the parameter  authorization  Parameters 
        String auth = params.getFirst("authorization");
        // 3. Judge whether the parameter value is equal to  admin
        if ("admin".equals(auth)) {
    
            // 4. yes , release 
            return chain.filter(exchange);
        }
        // 5. no , Intercept 
        // 5.1. Set status code 
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        // 5.2. Intercept request 
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
    
        return -1;
    }
}

Enter url localhost:10010/user/1?authorization=admin You can enter the page
 Insert picture description here

Filter chain connection sequence

 Insert picture description here
 Insert picture description here

The gateway cors Cross domain configuration

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Free dream programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252236138587.html