首页 >> 大全

SpringCloud无介绍快使用,sentinel服务熔断功能(二十四)

2023-11-12 大全 32 作者:考证青年

无介绍快使用,服务熔断功能(二十四)

问题背景

从零开始学微服务项目

注意事项:

整合++

1 新建两个微服务

2 选择jdk1.8

3 输入服务名:-- 和 --

4 引入pom依赖,-- 和 --两个服务名不同


<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud2022artifactId><groupId>com.yggroupId><version>1.0-SNAPSHOTversion>parent><modelVersion>4.0.0modelVersion><artifactId>cloudalibaba-provider-payment9003artifactId><dependencies><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>dependency><dependency><groupId>com.yggroupId><artifactId>cloud-api-commonsartifactId><version>${project.version}version>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-actuatorartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-devtoolsartifactId><scope>runtimescope><optional>trueoptional>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><optional>trueoptional>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency>dependencies>project>

5 添加.yml文件,-- 和 --端口不同

server:port: 9003spring:application:name: nacos-payment-providercloud:nacos:discovery:server-addr: localhost:8848 #配置Nacos地址management:endpoints:web:exposure:include: '*'

6 添加启动类,-- 和 --类名不同

package com.yg.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @Author suolong* @Date 2022/6/22 11:33* @Version 2.0*/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003 {public static void main(String[] args) {SpringApplication.run(PaymentMain9003.class, args);}}

7 添加

package com.yg.springcloud.controller;import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;/*** @Author suolong* @Date 2022/6/22 11:35* @Version 2.0*/
@RestController
public class PaymentController {@Value("${server.port}")private String serverPort;public static HashMap<Long, Payment> hashMap = new HashMap<>();static {hashMap.put(1L, new Payment(1L, "28a8c1e3bc2742d8848569891fb42181"));hashMap.put(2L, new Payment(2L, "bba8c1e3bc2742d8848569891ac32182"));hashMap.put(3L, new Payment(3L, "6ua8c1e3bc2742d8848569891xt92183"));}@GetMapping(value = "/paymentSQL/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {Payment payment = hashMap.get(id);CommonResult<Payment> result = new CommonResult(200, "from mysql,serverPort:  " + serverPort, payment);return result;}
}

8 创建--nacos-微服务,pom依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud2022artifactId><groupId>com.yggroupId><version>1.0-SNAPSHOTversion>parent><modelVersion>4.0.0modelVersion><artifactId>cloudalibaba-consumer-nacos-order84artifactId><dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-starter-openfeignartifactId>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-sentinelartifactId>dependency><dependency><groupId>com.yggroupId><artifactId>cloud-api-commonsartifactId><version>${project.version}version>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-actuatorartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-devtoolsartifactId><scope>runtimescope><optional>trueoptional>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><optional>trueoptional>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency>dependencies>project>

9 添加.yml文件

server:port: 84spring:application:name: nacos-order-consumercloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:#配置Sentinel dashboard地址dashboard: localhost:8080#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口port: 8719#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:nacos-user-service: http://nacos-payment-provider# 激活Sentinel对Feign的支持
feign:sentinel:enabled: true

10 添加启动类

package com.yg.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @Author suolong* @Date 2022/6/22 11:38* @Version 2.0*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderNacosMain84 {public static void main(String[] args) {SpringApplication.run(OrderNacosMain84.class, args);}}

11 添加接口和实现类

package com.yg.springcloud.service;import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.impl.PaymentFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;/*** @Author suolong* @Date 2022/6/22 11:45* @Version 2.0*/
@FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class)//调用中关闭9003服务提供者
public interface PaymentService {@GetMapping(value = "/paymentSQL/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);}

package com.yg.springcloud.service.impl;import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import org.springframework.stereotype.Component;/*** @Author suolong* @Date 2022/6/22 11:45* @Version 2.0*/
@Component
public class PaymentFallbackService implements PaymentService {@Overridepublic CommonResult<Payment> paymentSQL(Long id){return new CommonResult<>(444,"服务降级返回,没有该流水信息",new Payment(id, "errorSerial......"));}}

12 添加配置类

package com.yg.springcloud.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/*** @Author suolong* @Date 2022/6/22 11:39* @Version 2.0*/@Configuration
public class ApplicationContextConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();}}

13 添加

package com.yg.springcloud.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import com.yg.springcloud.service.PaymentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;/*** @Author suolong* @Date 2022/6/22 11:40* @Version 2.0*/
@RestController
public class CircleBreakerController {public static final String SERVICE_URL = "http://nacos-payment-provider";@Resourceprivate RestTemplate restTemplate;@RequestMapping("/consumer/fallback/{id}")@SentinelResource(value = "fallback", fallback = "handlerFallback") //fallback负责业务异常public CommonResult<Payment> fallback(@PathVariable Long id) {CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);if (id == 4) {throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");} else if (result.getData() == null) {throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");}return result;}public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {Payment payment = new Payment(id, "null");return new CommonResult<>(444, "兜底异常handlerFallback,exception内容  " + e.getMessage(), payment);}//==================OpenFeign@Resourceprivate PaymentService paymentService;@GetMapping(value = "/consumer/openfeign/{id}")public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {if (id == 4) {throw new RuntimeException("没有该id");}return paymentService.paymentSQL(id);}}

钱脉通x6功能使用介绍_使用ipad各种功能介绍_

14 只有配置说明

15 只配置说明

16 和都配置

若 和 都进行了配置,则被限流降级而抛出 时只会进入 处理逻辑

17 忽略属性

18 熔断框架对比

持久化

1 更改--的pom


<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud2022artifactId><groupId>com.yggroupId><version>1.0-SNAPSHOTversion>parent><modelVersion>4.0.0modelVersion><artifactId>cloudalibaba-sentinel-service8401artifactId><dependencies><dependency><groupId>com.alibaba.cspgroupId><artifactId>sentinel-datasource-nacosartifactId>dependency><dependency><groupId>com.yggroupId><artifactId>cloud-api-commonsartifactId><version>${project.version}version>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>dependency><dependency><groupId>com.alibaba.cspgroupId><artifactId>sentinel-datasource-nacosartifactId>dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-sentinelartifactId>dependency><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-starter-openfeignartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-actuatorartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-devtoolsartifactId><scope>runtimescope><optional>trueoptional>dependency><dependency><groupId>cn.hutoolgroupId><artifactId>hutool-allartifactId><version>4.6.3version>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><optional>trueoptional>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency>dependencies>project>

2 更改

server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:#Nacos服务注册中心地址server-addr: ${REGISTER_HOST:10.10.196.247}:${REGISTER_PORT:8848} #配置Nacos地址sentinel:transport:#配置Sentinel dashboard地址dashboard: localhost:8080#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口port: 8719datasource:ds1:nacos:server-addr: ${REGISTER_HOST:10.10.196.247}:${REGISTER_PORT:8848}dataId: cloudalibaba-sentinel-servicegroupId: DEFAULT_GROUPdata-type: jsonrule-type: flowmanagement:endpoints:web:exposure:include: '*'

3 在nacos上添加配置文件

[{"resource": "/rateLimit/byUrl","limitApp": "default","grade": 1,"count": 1,"strategy": 0,"controlBehavior": 0,"clusterMode": false}
]

:资源名称;

:来源应用;

grade:阈值类型,0表示线程数,1表示QPS;

count:单机阈值;

:流控模式,0表示直接,1表示关联,2表示链路;

:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;

:是否集群

_使用ipad各种功能介绍_钱脉通x6功能使用介绍

4 启动8401后刷新发现业务规则有了,注意注意注意:需要调用一次才会显示

无介绍快使用,Seata处理分布式事务(二十五

无介绍快使用,服务熔断功能(二十四)

无介绍快使用,注解@的基本使用(二十三)

无介绍快使用,热点key限流与系统规则的基本使用(二十二)

无介绍快使用,熔断降级和限流的基本使用(二十一)

无介绍快使用,Nacos集群和Nginx代理(二十)

无介绍快使用,nacos配置中心的基本使用(十九)

无介绍快使用,nacos注册中心的基本使用(十八)

无介绍快使用,通过微服务名实现动态路由(十七)

无介绍快使用,的基本使用(十六)

无介绍快使用,负载均衡工具与的使用(十五)

无介绍快使用,使用替换服务注册与发现(十四)

无介绍快使用,服务发现和自我保护(十三)

无介绍快使用,集群cloud--搭建(十二)

无介绍快使用,集群服务注册中心cloud--搭建(十一)

无介绍快使用,单机服务注册中心cloud--搭建(十)

无介绍快使用,新建cloud-api-公共模块(九)

无介绍快使用,新建子消费者订单模块(八)

无介绍快使用,热部署配置(七)

无介绍快使用,子提供者支付微服务业务开发(六)

无介绍快使用,新建子提供者支付微服务yml整合和新建启动类(五)

无介绍快使用,新建子提供者支付微服务pom整合(四)

无介绍快使用,父工程pom文件整理(三)

无介绍快使用,IDEA新建父工程(二)

无介绍快使用,与之间的兼容版本选择(一)

作为程序员第 191 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间, …

Lyric: 换个乐器就像换个兵器

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了