feat(customer):新增查询地址和服务接口的熔断策略类

This commit is contained in:
JIAN 2024-09-02 23:14:15 +08:00
parent 2781689e77
commit 27dc6c16af
3 changed files with 66 additions and 1 deletions

View File

@ -97,6 +97,10 @@
<artifactId>jzo2o-es</artifactId>
</dependency>
<dependency>
<groupId>com.jzo2o</groupId>
<artifactId>jzo2o-sentinel</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,59 @@
package com.jzo2o.orders.manager.service.client;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 自定义Feign客户端用于熔断降级
* @author JIAN
*/
@Slf4j
@Component
@SuppressWarnings("unused")
public class CustomerClient {
@Resource
private ServeApi serveApi;
@Resource
private AddressBookApi addressBookApi;
@SentinelResource(value = "addressBookDetail",
fallback = "getAddressDetailFallback", blockHandler = "getAddressDetailBlockHandler")
public AddressBookResDTO getAddressDetail(Long id) {
return addressBookApi.getById(id);
}
public AddressBookResDTO getAddressDetailFallback(Long id, Throwable throwable) {
log.warn("地址簿接口异常(未触发熔断), 地址簿id: {}", id, throwable);
return new AddressBookResDTO();
}
public AddressBookResDTO getAddressDetailBlockHandler(Long id, BlockException blockException) {
log.warn("地址簿接口异常(触发熔断降级), 地址簿id: {}", id, blockException);
return new AddressBookResDTO();
}
@SentinelResource(value = "serveAggregation",
fallback = "getServeAggregationDetailFallback", blockHandler = "getServeAggregationDetailBlockHandler")
public ServeAggregationResDTO getServeAggregation(Long id) {
return serveApi.findById(id);
}
public ServeAggregationResDTO getServeAggregationDetailFallback(Long id, Throwable throwable) {
log.warn("服务接口异常(未触发熔断), 服务id: {}", id, throwable);
return new ServeAggregationResDTO();
}
public ServeAggregationResDTO getServeAggregationDetailBlockHandler(Long id, BlockException blockException) {
log.warn("服务接口异常(触发熔断降级), 服务id: {}", id, blockException);
return new ServeAggregationResDTO();
}
}

View File

@ -42,6 +42,8 @@ spring:
refresh: false
- data-id: shared-mysql.yaml # mysql配置
refresh: false
- data-id: shared-sentinel.yaml # sentinel配置
refresh: false
# - data-id: shared-spring-seata.yaml # seata
# refresh: false
@ -73,4 +75,4 @@ logging:
level:
com.jzo2o: debug
io.seata: debug
org.apache.shardingsphere: debug
org.apache.shardingsphere: debug