zoukankan      html  css  js  c++  java
  • Nacos集成Spring Cloud Gateway 基础使用

    项目结构

    项目 端口 描述
    nacos-provider
    8000 服务
    nacos-getway
    8001 网关
    nacos-provider项目
    依赖
    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

    启动类

    package com.example.nacosprovider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosProviderApplication.class, args);
        }
    
    }

    配置文件

    server:
      port: 8000
    spring:
      application:
        name: nacos-provider
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848

    创建控制类ProviderController,http://localhost:8000/provider/helloProvider

    package com.example.nacosprovider.controller;
    
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/provider")
    public class ProviderController {
    
        @GetMapping("/helloProvider")
        public String helloProvider(){
            return "你好,我是服务提供者";
        }
    }
    nacos-getway项目: 在这里用的是yml配置文件的方法
    依赖
    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.2.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
        </dependencies>

    启动类

    package com.example.nacosgetway;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosGetwayApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosGetwayApplication.class, args);
        }
    }

    配置文件

    server:
      port: 8001
    spring:
      application:
        name: nacos-getway
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
        gateway:
          discovery:
            locator:
              enabled: true
              lower-case-service-id: true
          routes:
          - id: nacos-getway-provider
            uri: lb://nacos-provider
            predicates:
            - Path=/provider/**
          - id: nacos-getway-consumer
            uri: lb://nacos-consumer
            predicates:
            - Path=/**
    各字段含义如下:
    
    id:我们自定义的路由 ID,保持唯一
    uri:目标服务地址,以lb://开头(lb代表从注册中心获取服务)
    predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。
    该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
    filters:过滤规则,本示例暂时没用。

    启动测试

    启动Nacos-server服务,nacos-provider服务以及nacos-getway服务

    http://localhost:8000/provider/helloProvider

    http://localhost:8001/provider/helloProvider

    出现问题

    1,nacos-getway服务启动不了

    原因: 父工程的pom文件中引入了spring-boot-starter-web依赖

     因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。

    解决方法: 将父工程中的spring-boot-starter-web依赖去掉,在需要这个依赖的子工程中单独添加

    2,访问网关地址http://localhost:8001/provider/helloProvider返回404

    原因: getway配置写的不对,将routes写成了route

  • 相关阅读:
    nginx 命令
    nginx 配置文件(windows)
    nginx 配置文件(linux)
    nginx 安装
    什么是REST架构
    名词解释
    建造者模式
    单例模式
    工厂模式
    赋值运算符,拷贝构造函数,clone()方法总结
  • 原文地址:https://www.cnblogs.com/cailijuan/p/12024570.html
Copyright © 2011-2022 走看看