Stub 存根,可以在dubbo 提供者端实现,也可在调用消费方实现; 如果消费方实现存根,则服务方 存根 将不起作用。
提供者实现:
接口: IStubTestService 对应的stub实现: IStubTestServiceStub
服务类: StubTestServiceImpl
package com.jgq.dubbo_Service.service;
public interface IStubTestService {
public String sayHello(String name) ;
}
package com.jgq.dubbo_Service.service;
public class IStubTestServiceStub implements IStubTestService {
private final IStubTestService _ics;
public IStubTestServiceStub(IStubTestService ics)
{
this._ics = ics;
}
public String sayHello(String name) {
try{
return this._ics.sayHello(name);
}
catch(Exception e)
{
return "提供方容错数据!";
}
}
}
package com.jgq.dubbo_Service.service.impl;
import com.jgq.dubbo_Service.service.IStubTestService;
public class StubTestServiceImpl implements IStubTestService {
public String sayHello(String name) {
int i = (int)(Math.random()*1000);
if(i<600)
{
int ia = Integer.parseInt("ss"); //stub 测试
}
return "提供方服务数据 调用: " + name + String.valueOf(i);
}
}
服务配置: applicationProvider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- Application name -->
<dubbo:application name="hello-world-app" logger="log4j"/>
<!-- registry address, used for service to register itself -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- Default Protocol -->
<!-- expose this service through dubbo protocol, through port 20880 -->
<dubbo:protocol name="dubbo" port="20881" accesslog="true"/>
<!-- Service interface Concurrent Control 提供本地服务 -->
<!-- Stub Runing Client Service 客户端回调服务 -->
<span style="color:#ff0000;"><dubbo:service interface="com.jgq.dubbo_Service.service.IStubTestService" ref="stubService" executes="10" stub="com.jgq.dubbo_Service.service.IStubT</span>estServiceStub" />
<!-- designate implementation -->
<bean id="stubService" class="com.jgq.dubbo_Service.service.impl.StubTestServiceImpl" />
</beans>
场景类:
package com.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
System.out.println("Main Starting...");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("Press any key to exit.");
System.in.read();
}
}
消费者实现:
package com.jgq.service.impl;
import com.jgq.dubbo_Service.service.IStubTestService;
public class StubTestServiceProxy implements IStubTestService {
private final IStubTestService _ics;
public StubTestServiceProxy(IStubTestService ics)
{
this._ics = ics;
}
public String sayHello(String name) {
try{
return this._ics.sayHello(name);
}
catch(Exception e)
{
return "本地Stub 存根数据: " + name;
}
}
}
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- consumer application name -->
<dubbo:application name="consumer-of-helloworld-app" logger="log4j"/>
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:consumer timeout="5000"/>
<!-- which service to consume? -->
<span style="color:#ff0000;"> <dubbo:reference id="stubService" interface="com.jgq.dubbo_Service.service.IStubTestService" stub="com.jgq.service.impl.StubTestServiceProxy"/> </span>
<!-- 本地 bean -->
<bean id ="demoCallback" class = "com.jgq.service.impl.NotifyImpl" />
</beans>
消费服务:
stubService 如果服务异常,则自动返回stub 存根内容。