Spring Cloud Gateway 整合 nacos

pom.xml

<?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>springcloudalibaba</artifactId>
        <groupId>com.wsm.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <dependencies>
        <!-- gateway的依赖 springcloud开发 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!-- nacos 服务注册发现  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

    </dependencies>


</project>

 

package com.wsm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayApplication {

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

}

 

application.yml

server:
  port: 8060
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route # 路由的唯一标识, 路由到 order
#          uri: http://localhost:8020 # 需要转发的地址
          uri: lb://order-nacos-service # 需要转发的地址  lb:使用nacos中的本地负载均衡策略
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
              # http://localhost:8060/order-serv/order/add 路由转到
              # http://localhost:8020/order-serv/order/add
          filters:
            - StripPrefix=1  # 转发之前去掉第一层路径
              # http://localhost:8020/order-serv/order/add 过虑成
              # http://localhost:8020/order/add
    # 配置 Nacos
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
#        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        namespace: public

 

 

 

 

 

 

server:
  port: 8060
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      discovery:
        locator:
          enabled: true #是否启动自动识别 nacos 服务
    # 配置 Nacos
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
#        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        namespace: public

 

 

posted @ 2022-01-08 23:08  残星  阅读(962)  评论(0编辑  收藏  举报