SpringCloud之服务降级、Sentinel流量限流等(基于服务通信之上)

一、什么是服务降级?

服务之间相互调用,服务之间相互通信、数据传输,一个服务往往会因为另一个服务挂掉从而跟着不可用,为了保持服务的高可用性,可以采用服务降级的方式来提高服务的可用性。一句话总结:服务降级就是在一个服务挂掉后,给出一个解决方案,比如一些提示之类,从而保证在这个服务挂掉后调用者的那个服务仍然可以正常运行!

二、如何实现服务降级?—>服务降级demo

1> 导入相关依赖(下面yml还配了mybatis相关配置信息,因为我自己pom是继承了父pom有mybatis相关依赖的,根据自己实际情况配)
pom.xml

    <dependencies>
        <!--服务注册依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>
        <!-- 服务通信 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

        <!-- sentinel流量控制相关依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>
    </dependencies>

2> 编写application.yml配置文件
application.yml

server:
  port: 9191

spring:
  application:
    name: server-sentinel
  # 数据源配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      url: jdbc:mysql://ip/db_test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
      username: root
      password: root
  cloud:
    nacos:
      discovery:
        server-addr: ip:8848
    # 启用sentinel
    sentinel:
      # sentinel后台端口号
      transport:
        dashboard: ip:8178
      enabled: true
# 启用feign对sentinel的支持
feign:
  sentinel:
    enabled: true
mybatis:
  mapper-locations: classpath:mapper/**/*.xml

3> 编写应java代码
主程序入口:

@SpringBootApplication
//开启服务注册
@EnableDiscoveryClient
//开启服务通信
@EnableFeignClients
public class ServerSentinelApplication {

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

}

StudnetService:

import com.springcloudalibaba.serversentinel.service.impl.StudentServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

//服务调用注册中心服务名为server-provider的服务数据,fallback启动服务降级回调StudentServiceImpl.class
@FeignClient(value = "server-provider",fallback = StudentServiceImpl.class)
public interface StudentService {
    @RequestMapping("/provideServer/{id}")
    String getProviderData(@PathVariable int id);
}

StudentServiceImpl(当服务提供模块挂掉后,启动服务降级调用的类)

import com.springcloudalibaba.serversentinel.service.StudentService;
import org.springframework.stereotype.Component;
//服务降级具体实现
@Component
public class StudentServiceImpl implements StudentService {
    @Override
    public String getProviderData(int id) {

        return "获取数据失败";
    }
}

StudentController(对前端或者其他模块暴露的数据接口)

import com.springcloudalibaba.serversentinel.service.StudentService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class StudentController {
    @Resource
    StudentService studentService;

    @RequestMapping("/getProviderData/{id}")
    public String getProviderData(@PathVariable Integer id){
        String providerData = studentService.getProviderData(id);
        return providerData;
    }
}

启动两个服务注册到nacos服务注册中心后台
在这里插入图片描述
去访问StudentController,成功拿到server-provider的数据!
在这里插入图片描述

现在关闭server-provider服务模块
再去访问StudentController,发现执行了StudentServiceImpl里的内容
在这里插入图片描述
查看Sentinel后台,发现server-sentinel服务已经在后台了,可以根据自己的需求对其进行流量监控,限流等一些操作了!
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值