Dubbo-结果缓存

Dubbo为我们提供给了结果缓存功能,只要进行简单的配置就能实现结果缓存功能!

1.服务提供者

1.1 服务提供者接口

package com.test.dubboser;
 
public interface CacheService {
String findCache(String id);
}

1.2 服务提供者接口实现类

package com.test.dubboser;
 
import java.util.concurrent.atomic.AtomicInteger;
 
public class CacheServiceImp implements CacheService{
	private final AtomicInteger i = new AtomicInteger();
	public String findCache(String id) {
		// TODO Auto-generated method stub
		 String result = "request: " + id + ", response: " + i.getAndIncrement();
	     System.out.println(result);
	     return result;
	}
	 
}

1.3 服务端配置文件

<?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
        ">     
     <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
	<dubbo:application name="hello-world-app-my" />
	<!-- 多注册中心配置 -->
   <dubbo:registry  protocol="zookeeper"  address="192.168.0.27:2181"/>
     <!-- 用dubbo协议在20880端口暴露服务 -->
   	<dubbo:protocol name="dubbo" port="20880"/>  
   	 <!-- 声明需要暴露的服务接口 -->
	<dubbo:service  interface="com.test.dubboser.ServiceDemo"
		ref="demoService"/> 
	<dubbo:service  interface="com.test.dubboser.ServiceDemo2"
		ref="demoService2"/> 
	<dubbo:service  interface="com.test.dubboser.CacheService"
		ref="cacheService"/> 
    <!--和本地bean一样实现服务 -->
	<bean id="demoService" class="com.test.dubboser.ServiceImp"/>
	<bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
	<!-- 结果缓存 -->
	<bean id="cacheService" class="com.test.dubboser.CacheServiceImp"/>
	
</beans>


2.服务消费者

2.1 服务消费者配置文件

<?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
        ">     
	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="consumer-of-helloworld-app-my" />     
	<!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
	<dubbo:registry  protocol="zookeeper"  address="192.168.0.27:2181"/>  
	<!--获取服务  -->
    <dubbo:reference id="demoServicemy"    interface="com.test.dubboser.ServiceDemo"/>
    <dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2"/>
    <!--结果缓存配置  -->
    <dubbo:reference id="cacheService"   interface="com.test.dubboser.CacheService"  cache="true"/>
</beans>

2.2 客户端调用代码

package com.test.dubbocli;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.test.dubboser.CacheService;
import com.test.dubboser.ServiceDemo;
import com.test.dubboser.ServiceDemo2;
 
public class Main {
 
    public static void main(String[] args) throws InterruptedException {
    	run();
    }
     public static void run() throws InterruptedException{
    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
    	context.start();
    	//ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");
    	//ServiceDemo2 demoServer2 = (ServiceDemo2) context.getBean("demoServicemy2");
    	CacheService cacheService=(CacheService)context.getBean("cacheService");
    	/*ServiceDemo demoServer3 = (ServiceDemo) context.getBean("demoServicemy3");*/
    	/*String str=demoServer.say("java ---->>>");
    	String str2=demoServer2.say("java ---->>>");*/
    	String test=null;
    	for(int i=0;i<10;i++){
    		String caches=cacheService.findCache("0");
    		if(test==null||test.equals(caches)){
    			System.out.println("i="+i +" ok:"+caches);
    		}else{
    			 System.err.println("i=" + i + " ERROR: " + caches);
    		}
    		test= caches;
    		Thread.sleep(500);
    	}
    	String caches=cacheService.findCache("1");
    	System.out.println("last:"+caches);
    	/*String str3=demoServer3.say("java ---->>>");*/
    	/*System.err.println("res: "+str);
    	System.err.println("res: "+str2);*/
    	//System.err.println("cache res"+caches);
    	//System.err.println("res: "+str3);
     }
}

第一次启动服务提供者,消费端运行结果:

i=0 ok:request: 0, response: 0
i=1 ok:request: 0, response: 0
i=2 ok:request: 0, response: 0
i=3 ok:request: 0, response: 0
i=4 ok:request: 0, response: 0
i=5 ok:request: 0, response: 0
i=6 ok:request: 0, response: 0
i=7 ok:request: 0, response: 0
i=8 ok:request: 0, response: 0
i=9 ok:request: 0, response: 0
last:request: 1, response: 1

 

服务端没有重新启动,而客户端再次访问结果:

i=0 ok:request: 0, response: 2
i=1 ok:request: 0, response: 2
i=2 ok:request: 0, response: 2
i=3 ok:request: 0, response: 2
i=4 ok:request: 0, response: 2
i=5 ok:request: 0, response: 2
i=6 ok:request: 0, response: 2
i=7 ok:request: 0, response: 2
i=8 ok:request: 0, response: 2
i=9 ok:request: 0, response: 2
last:request: 1, response: 3

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值