本篇博客主要是分享Sentinel服务熔断功能,通过demo的演示,提高自己技术编写能力的同时也想通过博客分享,帮助需要帮助的人o( ̄︶ ̄)o
server:
port: 84
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
<?xml version="1.0" encoding="UTF-8"?>
<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>com.zcw.springcloud2020508</artifactId>
<groupId>com.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-consumer-nacos-order84</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zcw</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--SpringBoot 整合web组件+actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.zcw.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @ClassName : OrderNacosApplication
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 11:27
*/
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosApplication {
public static void main(String[] args) {
SpringApplication.run(OrderNacosApplication.class,args);
}
}
package com.zcw.springcloud.alibaba.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName : ApplicationContextConfig
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 14:28
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>com.zcw.springcloud2020508</artifactId>
<groupId>com.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-provider-payment9003</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zcw</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--SpringBoot 整合web组件+actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
server:
port: 9003
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
package com.zcw.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @ClassName : PaymentApplication
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 11:39
*/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
package com.zcw.springcloud.alibaba.controller;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.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;
/**
* @ClassName : PaymentController
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 11:30
*/
@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,"24242344525252352355423424"));
hashMap.put(2L,new Payment(2L,"4324234234234234234234234234"));
hashMap.put(3L,new Payment(3L,"23452342342323424fgdsggdgfgdgf"));
}
@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,severPort:"+serverPort,payment);
return result;
}
}
@SentinelResource(value = “fallbakc”,fallback =“handlerFallback”)// fallback只负责业务异常
//本案例是fallback
public CommonResult handlerFallback(@PathVariable Long id,Throwable e){
Payment payment = new Payment(id,"null");
return new CommonResult<>(444,"兜底异常hadlerFallback,exception内容"+e.getMessage(),payment);
}
public CommonResult blockHandler(@PathVariable Long id , BlockException blockException){
Payment payment = new Payment(id,"null");
return new CommonResult<>(445,
"blockHandler-sentinel限流,无此流水:blockException"
+blockException.getMessage());
}
服务熔断fallback和blockHandler都配置
通过测试结论为:
若blockHandler和fallback都进行了配置,则被限流降级而抛出BlockException时只会进入blockhandler处理逻辑。
<!--SpringCloud openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
#激活Sentinel对Feign的支持
feign:
sentinel:
enabled: true
package com.zcw.springcloud.alibaba.service;
import com.zcw.springcloud.alibaba.service.impl.PaymentServiceImpl;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @ClassName : PaymentService
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 16:29
*/
@FeignClient(value = "nacos-payment-provider",fallback = PaymentServiceImpl.class)
public interface PaymentService {
@GetMapping(value = "/paymentSql/{id}")
public CommonResult<Payment> paymentSql(@PathVariable("id") Long id);
}
package com.zcw.springcloud.alibaba.service.impl;
import com.zcw.springcloud.alibaba.service.PaymentService;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import org.springframework.stereotype.Service;
/**
* @ClassName : PaymentServiceImpl
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 16:34
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
public CommonResult<Payment> paymentSql(Long id) {
return new CommonResult<>(4444444,"服务降级返回-----PaymentServiceImpl",
new Payment(id,"error"));
}
}
@Resource
private PaymentService paymentService;
@GetMapping(value = "/consumer/paymentSql/{id}")
public CommonResult<Payment> paymentSql(@PathVariable("id") Long id){
return paymentService.paymentSql(id);
}