1. 程式人生 > 實用技巧 >spring cloud nacos 平臺搭建——閘道器搭建

spring cloud nacos 平臺搭建——閘道器搭建

spring cloud nacos 平臺搭建——nacso註冊中心搭建
spring cloud nacos 平臺搭建——服務註冊

官方demo:https://github.com/nacos-group/nacos-examples

注意版本對應版本說明 Wiki

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"> <parent> <artifactId>parent</artifactId> <groupId>indi.cyh.platform</groupId> <version>0.1</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>indi.cyh.platform.gateway</groupId> <artifactId>gateway</artifactId> <properties> <maven.compiler.source>8
</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${spring-cloud-alibaba.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>indi.cyh.platform.common</groupId> <artifactId>log</artifactId> <version>0.1
</version> </dependency> </dependencies> </project>

bootstrap.yml配置檔案

各種代理效果 參考:https://blog.csdn.net/qq_38380025/article/details/102968559

server:
  port: 80
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8888
        group: base-service
    gateway:
      discovery:
        locator:
          #是否與服務註冊於發現元件進行結合,通過 serviceId 轉發到具體的服務例項。
          #預設為 false,設為 true 便開啟通過服務中心的自動根據 serviceId 建立路由的功能
          enabled: true
          ##表示將請求路徑的服務名配置改成小寫
          lower-case-service-id: true
      routes:
         #自定義的路由 ID,保持唯一性
        - id: base
          order: 0
          # #代表從註冊中心獲取服務,且以lb(load-balance)負載均衡方式轉發
          uri: lb://base-provider
          #斷言
          predicates:
            - Path=/base/**
          filters:
            #當請求路徑匹配到/base/**會將包含hello和後邊的字串接去掉轉發,
            #StripPrefix=1就代表擷取路徑的個數,這樣配置後當請求/base/aaa後端匹配到的請求路徑,
            #就會變成http://localhost:800/base/aaa
            - StripPrefix=1

啟動類(和註冊服務一樣)

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

代理效果