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);
    }
}

代理效果

 
原文地址:https://www.cnblogs.com/cyh1282656849/p/14128294.html