阿里sentinel监控客户端配置

第一种配置方式:

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配置


	
	
		com.alibaba.csp
		sentinel-transport-simple-http
		1.6.1
	
	
	
		com.alibaba.csp
		sentinel-annotation-aspectj
		1.6.1
		
	
		com.alibaba.csp
		sentinel-datasource-redis
		1.6.1
	
	
		org.springframework.cloud
		spring-cloud-starter-alibaba-sentinel
		0.9.0.RELEASE
	

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

第二种配置方式:

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

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

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

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

  阿里sentinel监控客户端配置_第1张图片

   3、客户端pom.xml依赖


	
	
		com.alibaba.csp
		sentinel-transport-simple-http
		1.6.1
	
	
	
		com.alibaba.csp
		sentinel-annotation-aspectj
		1.6.1
		
	
		com.alibaba.csp
		sentinel-datasource-redis
		1.6.1
		

如下为公共部分

1、代码结构

阿里sentinel监控客户端配置_第2张图片

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";
    }
}

 

 

你可能感兴趣的:(阿里sentinel监控客户端配置)