栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud-gateway实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud-gateway实现

Spring Cloud Gateway 的三大核心概念

Route (路由): 路由是构建网关的基本模块,它由 ID,目标 URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
Predicate(断言):参考的是 java8 的 java.util.function.Predicate 开发人员可以匹配 HTTP 请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
Filter(过滤):指的是 Spring 框架中 GatewayFilter 的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改

Spring Cloud Gateway具体实现

pom文件



    4.0.0
    
        spring-cloud-demo
        com.xx.job
        1.0-SNAPSHOT
    

    cloud-gateway-gateway9527

    
        1.8
    

    
        
            junit
            junit
            4.11
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
      
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
            io.projectreactor.netty
            reactor-netty
            0.9.14.RELEASE
        

    


yml文件

server:
  port: 9527

spring:
  application:
    name: cloud-gateway-gateway9527
  cloud:
    gateway:
      routes:
        - id: payment8001
          uri: lb://SPRING-CLOUD-PAYMENT
          predicates:
            - Path=/payment/selectById/**

eureka:
  instance:
    instance-id: cloud-gateway-gateway9527
  client:
    #表示是否将自己注册进EurekaServer,默认为true
    register-with-eureka: true
    # 是否从EurekaServer抓取已有的注册信息,默认为毛白前,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka

主启动类

package com.xx.job.cloudgatewaygateway9527;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class CloudGatewayGateway9527Application {

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

}

 gateway的两种配置方式

方式一:上面的yml配置方式

方式二:java类配置

package com.xx.job.cloudgatewaygateway9527.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayConfig {


    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        RouteLocator build = routes.route("payment8001", r -> r.path("/payment/selectById/**").uri("lb://SPRING-CLOUD-PAYMENT"))
               // .route("payment8002", r -> r.path("/payment/get/**").uri("http://127.0.0.1:8002"))
                .build();
        return build;
    }
}

git:spring-cloud-demo: spring-cloud-demo试例

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737391.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号