# dubbo-study **Repository Path**: linestyle007/dubbo-study ## Basic Information - **Project Name**: dubbo-study - **Description**: dubbo-study - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-27 - **Last Updated**: 2021-11-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # dubbo-study dubbo-demo ## 准备工作 五个子模块应用 * 接口api * 服务端 * 客户端 * watch线程池 * monitor监听 ### 服务端 #### 引导类 ```java public class DubboServerBoostrap { public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class); context.start(); System.in.read(); } @Configuration @EnableDubbo(scanBasePackages = "org.example.service.impl") @PropertySource("classpath:/dubbo-provider.properties") static class ProviderConfiguration{ } } ``` #### 配置信息 * dubbo-provider.properties ```properties dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.application.name=dubbo-study-provider dubbo.protocol.name=dubbo dubbo.protocol.port=20881 dubbo.application.owner=test dubbo.provider.threadpool=monitor ``` ### 客户端 #### 引导类 ```java public class DubboClientBoostrap { public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); context.start(); // ... } @Configuration @PropertySource("classpath:/dubbo-consumer.properties") @ComponentScan(basePackages = "org.example.service.call") @EnableDubbo static class ConsumerConfiguration{ } } ``` #### 配置信息 * dubbo-consumer.properties ```java dubbo.application.name=dubbo-study-consumer dubbo.registry.valid=false dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.application.qosEnable=true dubbo.application.qosPort=33333 #dubbo.application.qosAcceptForeignIp=false ``` ### watch线程池 ```java public class WatchingThreadPool extends FixedThreadPool implements Runnable { public WatchingThreadPool(){ //每个三秒钟打印线程使用情况 Executors.newSingleThreadScheduledExecutor() .scheduleWithFixedDelay(this, 1, 3, TimeUnit.SECONDS); } @Override public Executor getExecutor(URL url) { final Executor executor = super.getExecutor(url); if (executor instanceof ThreadPoolExecutor){ THREAD_POOLS.put(url, (ThreadPoolExecutor) executor); } return executor; } @Override public void run() { //遍历线程池 for (Map.Entry entry: THREAD_POOLS.entrySet()){ final URL url = entry.getKey(); final ThreadPoolExecutor executor = entry.getValue(); //计算相关指标 final int activeCount = executor.getActiveCount(); final int corePoolSize = executor.getCorePoolSize(); double usedPercent = (double) activeCount / corePoolSize; log.info("线程池执行状态: {}/{} = {}%", activeCount, corePoolSize, usedPercent*100); if (usedPercent > ALARM_PERCENT){ log.error("超出警戒线! host: {}, 当前使用率是: {}, URL: {}", url.getIp(), usedPercent, url); } } } } ``` #### 配置信息 * META-INF.dubbo/org.apache.dubbo.common.threadpool.ThreadPool ```scala monitor=org.example.threadpool.WatchingThreadPool ``` ### monitor监听 ```java /** * CommonConstants.CONSUMER => 监测客户端 * CommonConstants.PROVIDER => 监测服务端 */ @Activate(group = {CommonConstants.CONSUMER}) public class TPMonitorFilter implements Filter,Runnable { //定义线程池使用的阈值 private final Map> TP_MAPS = new ConcurrentHashMap<>(); public TPMonitorFilter(){ //每个5秒钟打印tp90, tp99使用情况 Executors.newSingleThreadScheduledExecutor() .scheduleWithFixedDelay(this, 1, 5, TimeUnit.SECONDS); } @Override public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { long startTime = System.currentTimeMillis(); String methodName = invocation.getMethodName(); try { return invoker.invoke(invocation); }finally { long endTime = System.currentTimeMillis(); long times = endTime - startTime; // log.info("invoke time: {} 毫秒" , times); KeyVal keyVal = new KeyVal(endTime, times); Vector keyVals = TP_MAPS.get(methodName); if (keyVals == null){ Vector objects = new Vector<>(); objects.add(keyVal); TP_MAPS.put(methodName, objects); }else { keyVals.add(keyVal); } } } @Override public void run() { TP_MAPS.forEach((k, v) -> { List collect = v.stream().filter(a -> System.currentTimeMillis() - a.getTime() > 60000).collect(Collectors.toList()); v.removeAll(collect); tp(v, 90, k); tp(v, 99, k); }); } } ``` #### 配置信息 * META-INF.dubbo/org.apache.dubbo.rpc.Filter ```scala timeFilter=com.ghc.filter.TPMonitorFilter ``` ## 实现步骤 * 启动服务端 * 启动客户端