java小课堂
== 和 equals 的区别是什么?
- 对于基本类型,== 比较的是值;
- 对于引用类型,==比较的是地址;
- equals不能用于基本类型的比较;
- 如果没有重写equals, equals就相当于 ==;
- 如果重写了 equals方法,equals比较的是对象的内容;
目录
1.@SentinelResource
2. Ribbon系列
3.Fegin系列
4.规则持久化
在对sentinel进行流控设置时,资源名称应与@SentinelResource的value值保持一致,设置blockHander返回兜底方法,否则返回sentinel默认的
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandler = "defaultException")
public CommonResult byResource(){
return new CommonResult(200,"按资源名称限流",new Payment(2023,"serial001"));
}
public CommonResult defaultException(BlockException exception){
return new CommonResult(404, exception.getClass().getCanonicalName()+"服务不可用");
}
只设置@SentinelRsource的value值,使用url地址进行限流,返回sentinel默认的
@GetMapping("/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl(){
return new CommonResult(200,"按url限流",new Payment(2023,"serial001"));
}
使用@SentinelResource的blockHandlerClass属性,定义兜底类,在使用blockHandler定义兜底的方法
@GetMapping("/payment/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException2")
public CommonResult customerBlockHandler(){
return new CommonResult(200,"按自定义限流",new Payment(2023,"serial003"));
}
准备:服务提供者9003,9004;服务消费者84
1.再父工程下分别创建9003,9004服务提供者
2.注意jdk和maven版本
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
org.projectlombok
lombok
org.example
cloud-api-commons
1.0-SNAPSHOT
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
server:
port: 9003
#服务名名称
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
#nacos地址
server-addr: 192.168.20.50:1111
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9004.class);
}
}
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
public static HashMap hashMap = new HashMap<>();
static {
hashMap.put(1L, new Payment(111, "小张"));
hashMap.put(2L, new Payment(222, "小六"));
hashMap.put(3L, new Payment(333, "小李"));
}
@GetMapping("/payment/{id}")
public CommonResult getPayment(@PathVariable("id") Long id) {
Payment payment = hashMap.get(id);
CommonResult result = new CommonResult(200, "服务器端口:" + serverPort, payment);
return result;
}
}
1.在父工程下创建服务84
2.注意jdk和maven版本
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
org.projectlombok
lombok
org.example
cloud-api-commons
1.0-SNAPSHOT
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
org.springframework.cloud
spring-cloud-starter-openfeign
server:
port: 84
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.20.50:1111
sentinel:
transport:
dashboard: localhost:8080
prot: 8719
@SpringBootApplication
@EnableDiscoveryClient
public class Order84 {
public static void main(String[] args) {
SpringApplication.run(Order84.class);
}
}
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@RestController
public class ConsumerController {
private static final String SERVER_NAME="http://nacos-payment-provider";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback")
public CommonResult fallback(@PathVariable("id")Long id){
CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
if (result.getData()==null){
throw new NullPointerException("没有记录");
}
return result;
}
}
- fallback只管业务异常
- @SentinelResource的fallBack属性,当有异常时,返回兜底方法handlerFallBack
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", fallback = "handlerFallBack")
public CommonResult fallback(@PathVariable("id") int id) {
CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
if (result.getData() == null) {
throw new NullPointerException("没有记录");
}
return result;
}
public CommonResult handlerFallBack(@PathVariable("id") int id, Throwable e) {
Payment payment=new Payment(id,"null");
return new CommonResult(404, "兜底方法," + e.getMessage(),payment);
}
- blockhandler只负责Sentinel控制台配置违规
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", blockHandler = "blockHandler")
public CommonResult fallback(@PathVariable("id") int id) {
CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
if (result.getData() == null) {
throw new NullPointerException("没有记录");
}
return result;
}
public CommonResult blockHandler(@PathVariable("id") int id, BlockException blockException) {
Payment payment = new Payment(id, "null");
return new CommonResult(404, "blockHandler:" + blockException.getMessage(),payment);
}
若blockHandler和fallback都配置了,则被限流降级而抛出BlockException时只会进入blodkHandler处理逻辑。
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", blockHandler = "blockHandler", fallback = "handlerFallBack")
public CommonResult fallback(@PathVariable("id") int id) {
CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
if (result.getData() == null) {
throw new NullPointerException("没有记录");
}
return result;
}
public CommonResult blockHandler(@PathVariable("id") int id, BlockException blockException) {
Payment payment = new Payment(id, "null");
return new CommonResult(404, "blockHandler:" + blockException.getMessage(), payment);
}
public CommonResult handlerFallBack(@PathVariable("id") int id, Throwable e) {
Payment payment = new Payment(id, "null");
return new CommonResult(404, "兜底方法," + e.getMessage(), payment);
}
添加依赖
org.springframework.cloud
spring-cloud-starter-openfeign
激活feign
#激活Sentinel对Feign的支持
feign:
sentinel:
enabled: true
添加feign接口,与调用方法一致
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackFeign.class)
public interface PaymentFeign {
@GetMapping("/payment/{id}")
public CommonResult getPayment(@PathVariable("id") Long id);
}
兜底方法
@Component
public class PaymentFallbackFeign implements PaymentFeign{
@Override
public CommonResult getPayment(Long id) {
return new CommonResult<>(404,"服务降级返回----PaymentFallbackFeign");
}
}
添加@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Order84 {
public static void main(String[] args) {
SpringApplication.run(Order84.class);
}
}
@Autowired
private PaymentFeign paymentFeign;
@GetMapping("/consumer/payment/{id}")
public CommonResult getPayment(@PathVariable("id") Long id){
return paymentFeign.getPayment(id);
}
关闭服务提供者9003,9004,消费者84不会被耗死
一旦我们重启应用,sentinel规则将消失,生产环境需要将配置规则进行持久化
com.alibaba.csp
sentinel-datasource-nacos
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#nacos服务注册中心
server-addr: 192.168.20.50:1111
sentinel:
transport:
#sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
#将Sentinel流控规则持久化到nacos
datasource:
ds1:
nacos:
server-addr: 192.168.20.50:1111
dataId: ${spring.application.name}
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
management:
endpoints:
web:
exposure:
include: '*'
- resource:资源名称;
- limitApp: 来源应用;
- grade:阈值类型, 0表示线程数, 1表示QPS;
- count:单机阈值;
- strategy:流控模式,0表示直接,1表示关联,2表示链路;
- controlBehavior:流控效果, 0表示快速失败,1表示Warm Up, 2表示排队待;
- clusterMode:是否集群。
启动8401后,流控规则生效