有关ali sofa生成服务代理类的代理类源码+入门Demo

先来将provider和consumer的代码贴下,以便需要的能把程序跑起来,本文重点在最后。

facade层

接口类

public interface HelloSyncService {
    String saySync(String string);
}

provider层

实现类

import com.your-package.HelloSyncService;

// 实现类
public class HelloSyncServiceImpl implements HelloSyncService {

    @Override
    public String saySync(String string) {
        System.out.println("client invoked...........");
        return "provider tell you : this is your say: " +  string;
    }
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sofa="http://sofastack.io/schema/sofaboot"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
       default-autowire="byName">
    <!-- dubbo -->
    <bean id="dubboServiceImpl" class="com.your-package.DubboServiceImpl"/>
    <sofa:service ref="dubboServiceImpl" interface="com.your-package.DubboService">
        <sofa:binding.bolt/>
    </sofa:service>
</beans>

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
 * you should set a valid zk address first
 * @author <a href="mailto:leizhiyuan@gmail.com">leizhiyuan</a>
 */
@ImportResource({ "classpath:dubbo-server-example.xml" })
@SpringBootApplication
public class DubboServerApplication {

    public static void main(String[] args) {

        SpringApplication springApplication = new SpringApplication(DubboServerApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);
    }
}


consumer层

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sofa="http://sofastack.io/schema/sofaboot"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
       default-autowire="byName">
    <sofa:reference jvm-first="false" id="dubboServiceReference" interface="com.your-package.DubboService">
        <sofa:binding.bolt/>
    </sofa:reference>
</beans>

启动类

import com.your-package.DubboService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

@ImportResource({ "classpath:dubbo-client-example.xml" })
@SpringBootApplication
public class DubboClientApplication {
    public static void main(String[] args) {

        //change port to run in local machine
        System.setProperty("server.port", "8081");

        SpringApplication springApplication = new SpringApplication(DubboClientApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);

        DubboService directService = (DubboService) applicationContext.getBean("dubboServiceReference");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String result = dubboService.sayDubbo("dubbo");
        System.out.println("invoke result:" + result);
        if ("dubbo".equalsIgnoreCase(result)) {
            System.out.println("dubbo invoke success");
        } else {
            System.out.println("dubbo invoke fail");
        }
    }
}

分别启动provider和consumer的启动类两端控制台输出结果如下

provider

        string=dubbo

consumer

        invoke result:dubbo

        dubbo invoke success

表示调用成功~~~~~~~congratulation!!!!!!!!!!!!!!!!!!!!!

以下是本文重点

        在consumer端获取service引用时其实引用的是在容器启动时根据接口描述而生成的一个代理类

        DubboService dubboService = (DubboService) applicationContext.getBean("dubboServiceReference");
        String result = dubboService.sayDubbo("dubbo");

dubboService代理类的代码如下:

import com.alipay.sofa.rpc.common.utils.ReflectUtils;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.message.MessageBuilder;
import com.alipay.sofa.rpc.proxy.javassist.JavassistProxy;
import com.alipay.sofa.rpc.proxy.javassist.UselessInvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DubboService_proxy_0 extends Proxy implements DubboService {
    public Invoker proxyInvoker = null;
    private Method method_1 = ReflectUtils.getMethod(DubboService.class, "sayDubbo", new Class[]{String.class});

    public DubboService_proxy_0() {
        super(new UselessInvocationHandler());
    }

    public String sayDubbo(String var1) {
        Class var2 = DubboService.class;
        Method var3 = this.method_1;
        Class[] var4 = new Class[1];
        Object[] var5 = new Object[]{var1};
        var4[0] = String.class;
        SofaRequest var6 = MessageBuilder.buildSofaRequest(var2, var3, var4, var5);
        SofaResponse var7 = this.proxyInvoker.invoke(var6);
        if (var7.isError()) {
            throw new SofaRpcException(199, var7.getErrorMsg());
        } else {
            Object var8 = var7.getAppResponse();
            if (var8 instanceof Throwable) {
                throw (Throwable)var8;
            } else {
                return (String)var8;
            }
        }
    }

    public String toString() {
        return this.proxyInvoker.toString();
    }

    public int hashCode() {
        return this.proxyInvoker.hashCode();
    }

    public boolean equals(Object var1) {
        return this == var1 || this.getClass().isInstance(var1) && this.proxyInvoker.equals(JavassistProxy.parseInvoker(var1));
    }
}

负责生成代理类的类为JavassistProxy


本文全属个人理解,如有不全不周道之处,请取可用之处~








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值