阿里sentinel监控客户端配置

本文介绍如何通过Spring Cloud Alibaba Sentinel实现微服务的流量控制和熔断,包括配置方式、依赖引入、代码示例等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种配置方式:

1、客户端application.properties文件配置

spring.application.name=sentinel-example
server.port=8900

##Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
##客户端监控API的端口
spring.cloud.sentinel.transport.port=8721
##取消Sentinel控制台懒加载
spring.cloud.sentinel.eager=true

###服务注册到eureka地址
eureka.client.service-url.defaultZone: http://127.0.0.1:8100/eureka

# Redis服务器地址
spring.redis.host=127.0.0.1
spring.redis.port=6379

2、客户端pom.xml配置

<dependencies>
	<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-transport-simple-http -->
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-transport-simple-http</artifactId>
		<version>1.6.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-annotation-aspectj -->
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-annotation-aspectj</artifactId>
		<version>1.6.1</version>
	</dependency>	
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-datasource-redis</artifactId>
		<version>1.6.1</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
		<version>0.9.0.RELEASE</version>
	</dependency>
</dependencies>

特别注意spring-cloud-starter-alibaba-sentinel依赖和application.properties配置,必须联合使用才能生效

第二种配置方式:

   1、 客户端application.properties不用配置;

   2、客户端启动时加入 JVM 参数 

 -Dcsp.sentinel.dashboard.server=consoleIp:port 指定控制台地址和端口;

 -Dcsp.sentinel.api.port=xxxx 指定客户端监控 API 的端口;

  

   3、客户端pom.xml依赖

<dependencies>
	<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-transport-simple-http -->
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-transport-simple-http</artifactId>
		<version>1.6.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-annotation-aspectj -->
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-annotation-aspectj</artifactId>
		<version>1.6.1</version>
	</dependency>	
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-datasource-redis</artifactId>
		<version>1.6.1</version>
	</dependency>	
</dependencies>

如下为公共部分

1、代码结构

2、AopConfiguration配置类

package com.it.sentinel.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;

/**
 * @author Eric Zhao
 */
@Configuration
public class AopConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

3、ExceptionUtil类

package com.it.sentinel.demo.service;

import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * @author Eric Zhao
 */
public final class ExceptionUtil {

    public static void handleException(BlockException ex) {
        // Handler method that handles BlockException when blocked.
        // The method parameter list should match original method, with the last additional
        // parameter with type BlockException. The return type should be same as the original method.
        // The block handler method should be located in the same class with original method by default.
        // If you want to use method in other classes, you can set the blockHandlerClass
        // with corresponding Class (Note the method in other classes must be static).
        System.out.println("Oops: " + ex.getClass().getCanonicalName());
    }
}

4、使用注解@SentinelResource

package com.it.sentinel.demo.service;

import org.springframework.stereotype.Service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;

/**
 * @author Eric Zhao
 */
@Service
public class TestServiceImpl implements TestService {

    @Override
    @SentinelResource(value = "test", blockHandler = "handleException",fallback = "helloFallback", blockHandlerClass = {ExceptionUtil.class})
    public void test() {
        System.out.println(">>>>>Test");
    }
    @Override
    @SentinelResource(value = "test1", blockHandler = "handleException",fallback = "helloFallback", blockHandlerClass = {ExceptionUtil.class})
    public String test1() {
    	try {
			Thread.sleep(99);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	System.out.println("Test111");
    	return "Test111";
    }

    @Override
    @SentinelResource(value = "hello", fallback = "helloFallback")
    public String hello(long s) {
        if (s < 5) {
            throw new IllegalArgumentException("invalid arg");
        }
        return String.format("Hello at %d", s);
    }

    @Override
    @SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",
        exceptionsToIgnore = {IllegalStateException.class})
    public String helloAnother(String name) {
        if (name == null || "bad".equals(name)) {
            throw new IllegalArgumentException("oops");
        }
        if ("foo".equals(name)) {
            throw new IllegalStateException("oops");
        }
        return "Hello, " + name;
    }

    public String helloFallback(long s, Throwable ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + s;
    }

    public String defaultFallback() {
        System.out.println("Go to default fallback");
        return "default_fallback";
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值