惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - Lil-K

VM-16-pro 安装Centos Stream 9 基于CentOS7.x安装Nginx-1.18.0 Nginx在Windows下的基本介绍安装以及基本使用 window7下 cmd命令行 Mysql导出表结构 + 表数据 【一】Spark基础 【二】Spark 核心 【八】将日志写入log(glog) - Lil-K - 博客园 【七】ab压测 【六】tf和cgi进行联合试验,完成日志服务器 - Lil-K - 博客园 【五】安装fcig - Lil-K - 博客园 【四】搭建Nginx服务器 - Lil-K - 博客园 【三】多语言互通 - Lil-K - 博客园 【二】调通单机版的thrift-C++版本 - Lil-K - 博客园 【一】调通单机版的thrift-python版本 - Lil-K - 博客园 Spark在实际项目中分配更多资源 - Lil-K - 博客园 Spark实际项目中调节并行度 - Lil-K - 博客园 IDEA中大小写转换快捷键 使用maven下载cdh版本的大数据jar包 【Hive六】Hive调优小结 - Lil-K - 博客园
spring-cloud-alibaba-整合spring-cloud-gateway-3.1.4
Lil-K · 2022-12-14 · via 博客园 - Lil-K

spring-cloud-alibaba-整合spring-clouid-gateway-3.1.4

前言

最近使用较新版本的spring-cloud-alibaba搭建微服务框架, 在整合spring-cloud-gateway时遇到了一些问题, 所以记录一下, 如有不妥当的地方欢迎留言, 我会及时更正

版本说明

组件依赖 版本说明 官网地址
spring-boot 2.6.11 https://spring.io/projects/spring-boot#learn
spring-cloud-netflix 2021.0.4 https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
spring-cloud-alibaba 2021.0.4 https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
nacos-server 2.0.4 https://github.com/alibaba/nacos/releases/tag/2.0.4
nacos-client 2021.0.4.0
spring-cloud-starter-loadbalancer 3.1.5
spring-cloud-gateway 3.1.4

引入spring-cloud-gateway

  • pom.xml
<dependencies>
  <!--SpringBoot-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
  </dependency>

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

  <!-- nacos 配置中心 -->
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>

  <!-- openfeign 具备RPC通信能力 -->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>

  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  </dependency>

  <!--spring cloud gateway 网关-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
      <exclusions>
          <exclusion>
              <groupId>org.hibernate.validator</groupId>
              <artifactId>hibernate-validator</artifactId>
          </exclusion>
      </exclusions>
  </dependency>
</dependencies>

注意: 需要将pom中 spring-cloud-starter-gateway 依赖排除 hibernate-validator这个包, 不然会报错

Gateway(SrpingBoot)项目配置

  • application.yml
server:
  port: 8010
  tomcat:
    uri-encoding: UTF-8
  max-http-header-size: 80KB

spring:
  application:
    name: gateway-server-01
  profiles:
    active: dev
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    default-property-inclusion: non_null
    time-zone: GMT-8
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
    property-naming-strategy: SNAKE_CASE
  main:
    web-application-type: reactive
  • application-dev.yml
spring:
  cloud:
    ## gateway 注册到nacos
    nacos:
      discovery:
        server-addr: http://192.168.88.100:8848
        username: nacos
        password: nacos
        group: DEFAULT_GROUP
        service: ${spring.application.name}
        register-enabled: true
      config:
        group: DEFAULT_GROUP
        share-data-ids: ${spring.application.name}-${spring.profiles.active}
        server-addr: http://192.168.88.100:8848
        namespace: public
    ## gateway config
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        ## 需要路由到的 server name
        - id: blog-admin-01
          # 采用 LoadBalanceClient 方式请求, 以 lb:// 开头, 后面的是注册在 Nacos 上的服务名
          uri: lb://blog-admin-01
          # Predicate 翻译过来是"谓词"的意思,必须,主要作用是匹配用户的请求, 有很多种用法
          predicates:
            - Path=/blog-admin/**
          filters:
            # 前缀过滤, 默认配置下, 我们的请求路径是 http://localhost:8888/business-oauth2/** 这时会路由到指定的服务
            # 此处配置去掉 1 个路径前缀, 再配置上面的 Path=/api/**, 就能按照 http://localhost:8888/api/** 的方式访问了
            - StripPrefix=1
  config:
    import: nacos:application-dev.yml
  • SpringBoot启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }
}
  • Nacos服务列表

image.png

验证

需要通过gateway转发到具体的应用服务, 这里通过访问gateway, 转发到 blog-admin-01这个服务, 下面运行结果说明是没有问题的

image.png