SpringCloud Alibaba micro-service combat TEN - Service Gateway

REVIEW: In SpringCloud architecture, we need to deploy a separate gateway services provide external access to the entrance, and gateway services based on rules configured to forward the request to a specific back-end services, the main content of this chapter is to give our micro plus service gateway SpringCloud gateway.

Foreword

We have three services account-service, product-service, order-service. There are clients WEB应用or APP应用need to access back-end data acquisition service then you need to maintain good access path three services at the client.image.png

This architecture will have the following some typical questions:

  • Each micro-services will need to configure individual access domain, open access to external networks, each service needs to let a new operation and maintenance personnel configure domain mapping
  • The client needs to maintain access to the addresses of all micro services, Imagine if there are tens of hundreds of micro service it?
  • When the service requires access control interface, the user must be authenticated before calling, then all rights to be re-written set of logic on the server side.
  • 。。。

So we need to add a gateway service before micro services to the client as long as the access gateway, which is responsible for forwarding the request; permissions validation logic into the filter gateway, back-end services do not need to concern authority school test code; only need to provide external address of a domain name for external network access, the new service operation and maintenance personnel do not need to let the network configuration, so the above architecture becomes as follows:image.png

Create a gateway module

Build cloud-gateway module in the project, spring-cloud-gatewayas the micro-service system in a loop also need to register themselves and integrate into Nacos Nacos distribution center.

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-aliaba</artifactId>
        <groupId>com.jianzh5.cloud</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-gateway</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

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

    </dependencies>

</project>复制代码

  • Startup class
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }
}复制代码

  • bootstap.yml
spring:
  application:
    name: cloud-gateway
  cloud:
    nacos:
      config:
        server-addr: 10.0.10.48:8848
        file-extension: yml
        namespace: 7e8ccc22-6f51-42fa-bcf1-db45f99dbf57复制代码

  • Establish routing configuration of the gateway in the nacos
server:
  port: 8090
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 10.0.10.48:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: product-service
        uri: lb://product-service
        predicates:
          - Path=/product/**
      - id: account-service
        uri: lb://account-service
        predicates:
          - Path=/account/**
      - id: order-service
        uri: lb://order-service
        predicates:
          - Path=/order/**复制代码

Detailed configuration:

id: need to define all routes only, can not be repeated
uri: LB: // LB: // fixed wording will indicate on load balancing; that is, the service is registered in the name Nacos
predicates: - Path = / product / Use "Path Route Predicate Factory ", rule / product / requests are also forwarded to the micro-service product-service.

The above configuration logic to:
① in http://localhost:8090/product/**the access path are forwarded to product-servicethe micro and services /**
② The http://localhost:8090/account/**access path will be forwarded to account-servicethe micro and services /**
③ The http://localhost:8090/order/**access path will be forwarded to order-servicethe micro and services/**

  • Start all services to confirm whether to forward
    image.png

Well, ladies and friends, the contents of this issue on all over it, to see the students here are excellent students, under a promotion and pay rise is you! If you find this article to help you, then please scan the following QR code to add a concern. "Forward" plus "look", to form good habits! Let's See you next time!image.png

Series of articles

Welcome scan code concern micro-channel public numbers or personal blog

Guess you like

Origin juejin.im/post/5e2256075188254df6437ed5