spring cloud gateway 集成 sentinel

spring cloud gateway 集成 sentinel

1.版本

名称版本
jdk11
spring cloud alibaba2021.0.4.0
spring cloud gateway3.1.6
sentinel1.8.5

2.应用背景

  1. 项目中使用了nacos 作为注册中心及配置中心
  2. 已经使用了 openfeign 来进行远程调用,
  3. gateway 作为入口,现在使用 sentinel 来对入口进行管理

3.实际应用

这里其他的微服务就不列举了只关注gateway中 sentinel的使用
  1. 启动sentinel-dashboard 服务
    #1.解压
    tar -zxvf sentinel-dashboard-1.8.5.jar
    #2. 添加启动脚本
    	#2.1 新建文件
    	vim sentinel_start.sh
    	#2.2 将下列命令添加进去 port:自己之地给你端口  日志路径
    	nohup java -jar -Dserver.port=20670 sentinel-dashboard-1.8.5.jar >/opt/logs/sentinel.log 2>&1 &
    	#2.3 赋予执行权限
    	chmod 755 sentinel_start.sh
    #3. 启动服务
    ./sentinel_start.sh
    #4.查看启动日志
    tail -f /opt/logs/sentinel.log
    
  2. gateway 项目依赖
    <dependencies>
        <!-- 引入gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>3.1.6</version>
        </dependency>
        <!-- 引入sentiel-gateway ,这里继承了所需的依赖无需其他依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>
        <!-- 引入sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId> 
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- 需要使用loadbalancer 来进行负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.1.6</version>
        </dependency>
	</dependencies>

  1. application.yml配置
spring:
  application:
    name: gateway
  cloud:
    sentinel:
      enabled: true
      transport:
        port: 8719 #这个端口需要开放,否则无法与微服务进行心跳检测
        dashboard: xxxx:20670  #这里的端口 自己开启sentinel-dashboard 服务时自己指定
        clientIp: 127.0.0.1 #可不填,默认本机ip
      eager: true  #关闭懒加载,提起扫描端口
  1. 启动gateway项目

  2. 打开sentinel控制台,用户名和密码都是 sentinel,进入后如图:
    这里多了gateway 就是监控到了gateway网关,使用网关进行查询后,会有监控

  3. 添加流控配置
    流控配置操作

  4. 进行访问时,会提示错误
    这个提示表示控制住了

结束了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值