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

SpringCloudGateWay+SpringCloudAlibabaNacos讲解

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

SpringCloudGateWay+SpringCloudAlibabaNacos讲解

SpringCloud官方地址 Spring Cloud

源码地址:https://gitee.com/datadogapache/dashboard/projects

一.添加Nacos依赖:

  

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
            0.2.2.RELEASE
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.1.RELEASE
        

二.添加GateWay依赖:

           
                 org.springframework.cloud
                 spring-cloud-starter-gateway
                   
                      
                          org.springframework.boot
                          spring-boot-starter-web
                      
                      
                          org.springframework.boot
                          spring-boot-starter-webflux
                      
                   
            

注意:坑!坑!坑!

springcloudgateway的底层实现是基于webflux,而webflux和传统的webmvc冲突,所以当项目中存在spring-boot-starter-web依赖时,需排除webmvc,且在spring-cloud-starter-gateway依赖中排除spring-boot-starter-web和spring-boot-starter-webflux,单独引入spring-boot-starter-webflux依赖替代webmvc,pom.xml中完整的依赖如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
         
    
    com.example
    springcloudgateway
    0.0.1-SNAPSHOT
    springcloudgateway
    Demo project for Spring Boot
    
        1.8
        Hoxton.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework
                    spring-webmvc
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
            0.2.2.RELEASE
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.1.RELEASE
        
                 
                   org.springframework.cloud
                    spring-cloud-starter-gateway
                     
                         
                             org.springframework.boot
                             spring-boot-starter-web
                         
                         
                             org.springframework.boot
                             spring-boot-starter-webflux
                         
                     
               
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring.cloud.version}
            pom
            import
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

最后也是最重要的一点就是!!!

1.springboot的版本和springcloud的版本对应一定要一致,否者项目将报各种错误。spring官网中有其对应的版本,上面代码中spingboot的版本为2.3.3.RELEASE,springcloud的版本为Hoxton.RELEASE.

2.因为webflux基于netty,所以还需在spring-boot-starter-web中排除tomcat依赖:

        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework
                    spring-webmvc
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        

三.配置bootstrap.yml

spring:
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: 192.168.91.1:8848
        file-extension: yml
      discovery:
        server-addr: 192.168.91.1:8848
    gateway:
      httpclient:
        connect-timeout: 3000
        response-timeout: 10s
      routes:
        - id: test
          uri: lb://gateway
          predicates:
            - Path=/345/123
          filters:
           - StripPrefix=1


各字段含义如下:

id:我们自定义的路由 ID,保持唯一

uri:目标服务地址,lb为自定义nacos中的服务

predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。

filters:过滤器,有多种过滤条件,其中StripPrefix为跳过路径数。例如:访问localhost:8090/test/api/home,当StripPrefix=2时,/test/api被跳过,直接访问localhost:8090/home

上面这段配置的意思是,配置了一个 id 为test的URI代理规则,路由的规则为:

当访问地址http://localhost:80890/345/123时,

会路由到上游地址http://localhost:80890/123。

四.建立controller测试路径

package com.example.springcloudgateway.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class gatecontroller {
    @RequestMapping(value = "/123",method = RequestMethod.GET)
    public String tsetGate() {
      return "welcome to my house!";
    }
}

浏览器中输入localhost:8090/345/123

 当输入localhost:8090/123时,由于网关没有断言(predicates)设置路由规则为真,即可直接访问

 完毕!后期还会更更多关于网关的知识点,特推荐一篇关于SpringCloud gateway的博文:SpringCloud gateway (史上最全) - 疯狂创客圈 - 博客园 (cnblogs.com)

希望多多交流指正!

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

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

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