Dubbo Stub 操作实例
来源:互联网 发布:淘宝哪一家mlb是正品 编辑:程序博客网 时间:2024/06/10 13:59
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 存根内容。
0 0
- Dubbo Stub 操作实例
- Dubbo Cache 操作实例
- Dubbo 切面监控 操作实例
- Dubbo实例
- Dubbo实例
- Dubbo实例
- Dubbo实例
- dubbo实例
- Dubbo实例
- Dubbo实例
- dubbo实例
- stub
- Dubbo 回声调用 检测提供者是否正常 操作实例
- Dubbo入门实例
- Dubbo学习教程实例
- 阿里dubbo实例测试
- dubbo 实例详解
- Dubbo简介及实例
- Java线程(二):线程同步synchronized和volatile
- Android活动Acitivity启动模式之standard
- Android 下拉刷新框架实现
- 全表扫描是指整个表的数据检索一次
- Java enum的用法详解
- Dubbo Stub 操作实例
- .Net Framework 2.0 使用WCF
- android NFC的读写
- Nodejs 异步 I/O
- 图片内存溢出时的处理
- js+css中的clip进行的简单的缩略图裁剪
- Android活动Acitivity启动模式之singleTop
- HDOJ 1715 大菲波数
- Windows下HVR配置基础环境变量