美文网首页
java版spring cloud 多租户社交电子商务平台-Ga

java版spring cloud 多租户社交电子商务平台-Ga

作者: IT达人Q | 来源:发表于2019-07-17 09:05 被阅读0次

    电子商务平台源码请加企鹅求求:三伍三六二四柒二五玖。Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

    入门案例

    接下来,介绍一下简单使用SpringCloud Gateway路由功能

    创建项目

    新建一个项目,项目种加入SpringCloud Gateway依赖,完整pom如下:

    <?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">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.dalaoyang</groupId>
        <artifactId>springcloud_gateway</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
     
        <name>springcloud_gateway</name>
        <description>springcloud_gateway</description>
     
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
     
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.RC1</spring-cloud.version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
     
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
     
     
    </project>
    
    

    SpringBoot主程序

    在类中配置路由

    主程序中加入了一种配置路由的方法,利用@Bean的方式自定义RouteLocator。

    package com.dalaoyang;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.gateway.route.RouteLocator;
    import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
    import org.springframework.context.annotation.Bean;
     
     
    @SpringBootApplication
    public class SpringcloudGatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringcloudGatewayApplication.class, args);
        }
     
     
     
        @Bean
        public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
            return builder.routes()
                    .route(r -> r.path("/jianshu")
                            .uri("http://www.jianshu.com/u/128b6effde53")
                    ).build();
        }
     
    }
    
    

    在配置文件中配置:

    server:
      port: 8888
     
    spring:
      application:
        name: gateway-service
      cloud:
        gateway:
          routes:
          - id: dalaoyang
            uri: http://www.dalaoyang.cn/
            predicates:
              - Path=/dalaoyang/**
          - id: juejin
            uri: https://juejin.im/user/5aa50b96f265da23866f836e
            predicates:
              - Path=/juejin/**
     
    
    

    到这里配置完成了。

    相关文章

      网友评论

          本文标题:java版spring cloud 多租户社交电子商务平台-Ga

          本文链接:https://www.haomeiwen.com/subject/ryuflctx.html