sentinel 测试熔断
搭建消费者 pomcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-alibaba-sentinelorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
application.yml
server: port: 8004 spring: application: name: sentilel-consumer cloud: nacos: discovery: #Nacos服务注册中心地址 server-addr: localhost:8848 sentinel: transport: #配置Sentinel dashboard地址 dashboard: localhost:8080 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 port: 8719 management: endpoints: web: exposure: include: '*'启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class Consumer8004 { public static void main(String[] args) { SpringApplication.run(Consumer8004.class,args); } }配置类
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; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }业务类
import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; 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; @RestController public class TestController { public static final String SERVICE_URL = "http://sentinel-provider"; @Resource private RestTemplate restTemplate; @RequestMapping("/consumer/fallback/{id}") // @SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback负责业务异常 //@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler负责在sentinel里面配置的降级限流 @SentinelResource(value = "fallback",blockHandler = "blockHandler",fallback = "handlerFallback") //blockHandler负责在sentinel里面配置的降级限流 public String fallback(@PathVariable Long id) { String result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,String.class,id); if (id == 4) { throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常...."); } return result; } public String handlerFallback(@PathVariable Long id,Throwable e) { return "自定义异常"; } public String blockHandler(@PathVariable Long id, BlockException blockException) { return "sentinel 自定义异常"; } }搭建生产者 pom
aoolication.ymlcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoveryorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
server: port: 9003 spring: application: name: sentinel-provider cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址 management: endpoints: web: exposure: include: '*'启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Provider9003 { public static void main(String[] args) { SpringApplication.run(Provider9003.class,args); } }业务类
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; @RestController public class TestController { @Value("${server.port}") private String serverPort; public static HashMap启动与测试 启动hashMap = new HashMap<>(); static { hashMap.put(1L,"28a8c1e3bc2742d8848569891fb42181"); hashMap.put(2L,"bba8c1e3bc2742d8848569891ac32182"); hashMap.put(3L,"6ua8c1e3bc2742d8848569891xt92183"); } @GetMapping(value = "/paymentSQL/{id}") public String paymentSQL(@PathVariable("id") Long id) { String str = hashMap.get(id); return str; } }
启动nacos
启动sentinel
启动生产者
启动消费者
测试在页面配置熔断进行测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)