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
    评论
根据提供的引用内容,springcloud nacos gateway knife的问题可能是关于如何在Spring Cloud中使用Nacos作为注册中心和配置中心,以及如何引入Knife4j的相关依赖的。下面是一个可能的回答: 在你的Spring Cloud项目中,你可以通过引入Nacos的相关依赖来将Nacos作为注册中心和配置中心。具体来说,你需要在pom.xml中引入以下依赖: ```xml <!-- Nacos注册中心依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${nacos.version}</version> </dependency> <!-- Nacos配置中心依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>${nacos.version}</version> </dependency> ``` 请在pom.xml中添加上述依赖以启用Nacos作为注册中心和配置中心。 另外,如果你还想在服务和网关中使用Knife4j,你需要在pom.xml中引入以下依赖: ```xml <!-- Knife4j依赖 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.3</version> </dependency> ``` 请在pom.xml中添加上述依赖以启用Knife4j。 此外,你还需要在配置文件中进行相应的配置。例如,你可以在application.yml或application.properties中添加以下配置: ```yaml spring: profiles: active: dev application: name: bssp-gateway-service cloud: nacos: config: server-addr: 127.0.0.1:8848 namespace: 3a44ef28-b35c-44bb-8cd2-873c14ebc911 group: DEFAULT_GROUP file-extension: yml discovery: server-addr: 127.0.0.1:8848 namespace: 3a44ef28-b35c-44bb-8cd2-873c14ebc911 gateway: discovery: locator: enabled: true ``` 请根据你的具体需求修改上述配置,确保Nacos和Knife4j的配置正确。 希望以上信息对你有帮助。如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

a_sunny_a

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

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

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

打赏作者

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

抵扣说明:

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

余额充值