dubbo+zk+dubbo_admin学习

一、  安装zookeeper

1、安装包在网上能下到(建议官网下载,使用成熟的版本)

           教程网上都有:注意点:更改conf下的配置文件zoo(怎么改网上也有)

二、  Dubbo_admin

1、安装部署

           简单点的方法是将下载dubbo_admin 直接放在tomcat下webapps中,改名ROOT    》》》》不必按照网络上的方式打包war什么的

监控问题:目前正在处理,后续分享

           使用注意:jdk版本匹配问题(我的8不可,7可以,具体情况具体对待)

三、 Dubbo编码(privater+customer多线程)

dubbo具有两种编码方式:

1、Spring依赖,XML配置方式    这个较为简单,但是不适用多线程使用  ,网上较详细,此处不多介绍

2、直接调用dubbo底层方法加载api,构造配置文件进行注册,进而实现多线程

多线程具体步骤:

准备工作,仔细了解dubbo中这几个类

一、privater

1、构造封装接口及实现类

2、加载所需要的类,dubbo中的,及构造的接口实现类

3、实现服务(实现XML配置的内容,使用映射的方法)

// 服务实现
 Object dubboAPI =cls.newInstance();

 // 当前应用配置
 Object application = ApplicationConfig.newInstance();
 Method setName = ApplicationConfig.getDeclaredMethod("setName", new Class[]{String.class});
 setName.invoke(application, "dubboAPI-Test");

 // 连接注册中心配置
 Object registry = RegistryConfig.newInstance();
 Method setAddress = RegistryConfig.getDeclaredMethod("setAddress", new Class[]{String.class});
 setAddress.invoke(registry, "zookeeper://127.0.0.1:2181");//172.31.12.107:13188

 // 服务提供者协议配置
 Object protocol = ProtocolConfig.newInstance();
 Method psetName = ProtocolConfig.getDeclaredMethod("setName", new Class[]{String.class});
 Method setPort = ProtocolConfig.getDeclaredMethod("setPort", new Class[]{Integer.class});
 psetName.invoke(protocol, "dubbo");
 setPort.invoke(protocol,ParamProtocol);


 // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
 // 服务提供者暴露服务配置
 Object service = ServiceConfig.newInstance();// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
 Method setVersion = ServiceConfig.getMethod("setVersion", new Class[]{String.class});
 Method setApplication = ServiceConfig.getMethod("setApplication", new Class[]{ApplicationConfig});
 Method setRegistry = ServiceConfig.getMethod("setRegistry", new Class[]{RegistryConfig});
 Method setProtocol = ServiceConfig.getMethod("setProtocol", new Class[]{ProtocolConfig});
 Method setInterface = ServiceConfig.getMethod("setInterface", new Class[]{Class.class});
 Method setRef = ServiceConfig.getMethod("setRef", new Class[]{Object.class});

 Method export = ServiceConfig.getMethod("export");
 setApplication.invoke(service, application);
 setRegistry.invoke(service, registry);
 setProtocol.invoke(service, protocol);
 // 配置dubbo服务内容, 一、接口;二、具体实现
 setInterface.invoke(service, DubboAPI);
 setRef.invoke(service, dubboAPI);
 setVersion.invoke(service, "1.0.0");

 // 暴露及注册服务
// System.out.println(service);
 export.invoke(service);

注意:多线程如果需要监控每个线程的运行情况,建议使用Vector(线程安全)

二、customer(和privater类似)

public void run(){
            Thread.currentThread().setContextClassLoader(this.myLoader);
 try {
                String interfaceName="dubbo_liu.ServiceInterface"+i;
 try {
                    //生成接口就ok了
 generateService(interfaceName);
 } catch (Exception e) {
                    // TODO Auto-generated catch block
 OneCustomer.addElement(i);
 ListCustomer.addElement(i);
 e.printStackTrace();
 }
  //加载类
 Class<?> ServiceInterface = myLoader.loadClass(interfaceName);
 Class<?> ApplicationConfig = myLoader.loadClass("com.alibaba.dubbo.config.ApplicationConfig");
 Class<?> RegistryConfig = myLoader.loadClass("com.alibaba.dubbo.config.RegistryConfig");

 Class<?> ReferenceConfig = myLoader.loadClass("com.alibaba.dubbo.config.ReferenceConfig");

 // 当前应用配置
 Object application = ApplicationConfig.newInstance();
 Method setName = ApplicationConfig.getDeclaredMethod("setName", new Class[]{String.class});
 setName.invoke(application, "dubbo_customer");

 // 连接注册中心配置
 Object registry = RegistryConfig.newInstance();
 Method setAddress = RegistryConfig.getDeclaredMethod("setAddress", new Class[]{String.class});
 setAddress.invoke(registry, "zookeeper://127.0.0.1:2181");

 // 消费者者暴露服务配置
 Object reference = ReferenceConfig.newInstance();
 Method setId = ReferenceConfig.getMethod("setId", new Class[]{String.class});
 Method setInterface = ReferenceConfig.getMethod("setInterface", new Class[]{Class.class});
 Method setVersion = ReferenceConfig.getMethod("setVersion", new Class[]{String.class});
 Method setApplication = ReferenceConfig.getMethod("setApplication", new Class[]{ApplicationConfig});
 Method setRegistry = ReferenceConfig.getMethod("setRegistry", new Class[]{RegistryConfig});
 Method getService = ReferenceConfig.getMethod("get");
 setId.invoke(reference,"demoService");
 setInterface.invoke(reference,ServiceInterface);
 setVersion.invoke(reference,"1.0.0");
 setRegistry.invoke(reference,registry);
 setApplication.invoke(reference, application);
 // 消费服务
 Object realService = getService.invoke(reference);// 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用

 Method sayHello = ServiceInterface.getMethod("sayHello", new Class[]{String.class});
 System.out.println(sayHello.invoke(realService, "dubbo test"));
 if (sayHello.invoke(realService, "dubbo test").equals("Hello dubbo test")){
                        OneCustomer.addElement(i);
 PortList.addElement(i);//记录
 ListCustomer.addElement(i);//记录
 }
      System.in.read();
 }catch (Exception e) {          
 e.printStackTrace();
 }
 public static void generateService(String interfaceName) throws CannotCompileException {
 // 使用线程的classloader, 保证和main方法里的使用的那个classloader一致
 ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
 // dubbo提供的,static Map<ClassLoader, ClassPool>;
 ClassPool classPool = ClassGenerator.getClassPool(classLoader);
 CtClass DymaticServiceInterface;
 try{
                classPool.get(interfaceName);
 return;
 } catch (NotFoundException e){
                DymaticServiceInterface = classPool.makeInterface(interfaceName);
 DymaticServiceInterface.addMethod(CtMethod.make("public String sayHello(String name);", DymaticServiceInterface));
 //加载到jvm中
 DymaticServiceInterface.toClass();
 }}}

注意点:1、需要熟悉dubbo下conf中的类

               2、注意,多线程类的加载,必须保证每个类的加载器,都不相同,Classloader不支持同步,多线程使用,会出现问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值