spring cloud alibaba+spring cloud gateway

技术选用

  • Spring Cloud Alibaba(nacos)
  • SpringCloud Gateway

版本选用

  1. Spring Cloud Alibaba:2.2.6.RELEASE
  2. SpringCloud:Hoxton.SR2
  3. SpringBoot:2.3.2.RELEASE

实现功能

  1. nacos注册中心
  2. 两个nacos客户端,用于业务需求(server1、server2)
  3. 通过SpringCloud Gateway转发server1、server2请求
  4. server1、server2引入基础平台可进行认证授权、系统管理等

nacos注册中心

下载压缩包

在以下网址下载:https://github.com/alibaba/nacos/releases

启动

  • 先解压:
  • Windows:cmd进入bin目录执行命令:
    startup.cmd -m standalone
  • Linux:进入bin目录执行命令:
    startup.sh -m standalone

至此注册中心启动成功,默认端口为8848,http://127.0.0.1:8848/nacos/#/login登录进入管理界面,其用户名密码均为nacos

在这里插入图片描述

nacos客户端(server1、server2、gateway)

(1)pom文件引入nacos

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

(2)application.yml配置

server:
  port: 9001
spring:
  application:
    name: server1
  cloud:
    nacos:
      discovery:
# nacos注册中心
        server-addr: 127.0.0.1:8848

(3)开启nacos(@EnableDiscoveryClient)

@SpringBootApplication
@EnableDiscoveryClient
public class Application {

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

(4)Gateway配置转发

server:
  port: 9003
spring:
  profiles:
    active: default
  application:
    name: spring-cloud-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true
      defaultFilters:
        - PreserveHostHeader
      routes:
        # server1
        - id: server1
          uri: lb://server1
          predicates:
            - Path=/server1/**
          filters:
#            - StripPrefix=1
#            - TokenRelay=
        # server2
        - id: server2
          uri: lb://server2
          predicates:
            - Path=/server2/**

遇到的问题

  • gateway配置成以下这样访问server1一直重定向到http://localhost:9003/server1/sys/login
- id: SERVER1
  uri: lb://server1
  predicates:
    - Path=/server1/**
换成以下配置问题解决:
- id: SERVER1
  uri: lb://server1
  predicates:
    - Path=/server1/{segment}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a_sunny_a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值