dubbo 我们提供给了结果缓存功能,只要进行简单的配置就能实现结果缓存功能!
一、服务提供者
1、服务提供者接口
package com.test.dubboser;
public interface CacheService {
String findCache(String id);
}
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;
}
}
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>
二、服务消费者
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>
dubbo配置结果缓存还是很简单的……
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);
}
}
3、服务端接口实现代码
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;
}
}
第一次启动服务提供者,消费端运行结果:
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