One, foreword

In production, all request entries are unified, usually using Nginx->Gateway. The microservices call each other through the Intranet. Gateway is the Gateway for all services. Gateway allows permission authentication and interception.

Maven dependency


      
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.llh</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>gateway</artifactId>
    <version>1.0.0</version>
    <name>gateway</name>
    <description>gateway description</description>
    <packaging>jar</packaging>

    <dependencies>
        <! Service registration and Discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <! - the gateway -- -- >
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <! -- Load balancer -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
    </dependencies>
</project>

Copy the code

Properties configuration file application

Server. port=20000 # application name=gateway # nacOS spring.cloud.nacos.discovery.server-addr=http://localhost:8848 spring.cloud.nacos.discovery.namespace=83e9d384-d49e-4f40-84bf-c25612883dcc spring.cloud.nacos.discovery.username=nacos Spring. Cloud. Nacos. Discovery. Password = # nacos gateway related spring. Cloud. Gateway. Discovery. A locator. Enabled = true spring.cloud.gateway.discovery.locator.lower-case-service-id=true spring.cloud.gateway.routes[0].id=consumer spring.cloud.gateway.routes[0].uri=lb://consumer spring.cloud.gateway.routes[0].predicates[0]=Path=/order/** spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1Copy the code
  • A routing change has been made so that all interfaces starting with /order will be routed toconsumerInterfaces on the service.

4. Customize Filter

MyFilter class

package com.llh.gateway;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;


/ * * *@authorTiger's tech blog */
@Component
public class MyFilter implements WebFilter{
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getPath().toString();
        // Print the request path, where the production environment can do some authentication, etc
        System.out.println(path);
        returnchain.filter(exchange); }}Copy the code
  • All requests will pass through this interceptor, which simply prints out the request path where the production environment can do some authentication and so on

Five, run,

Interfaces that request gateways are forwarded to specific services

Request interface: Gateway IP: Gateway port/ service name/internal service interface

Such as http://localhost:20000/consumer/consumer/buy/1/1

Because the route is forwarded

http://localhost:20000/order/consumer/buy/1/1 is http://localhost:20000/consumer/consumer/buy/1/1

Six, the concluding

Source code address: github.com/tigerleeli/…

Synchronize wechat official account: Little Tiger’s technology blog