Dubbo Stub 操作实例

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值