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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM