先來將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:[email protected]">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
本文全屬個人理解,如有不全不周道之處,請取可用之處~