网上搜dubbo用redis做注册中心的资料很少,所以决定自己实践,自己写一下。有dubbo+redis的,但是没有结合spring。所以在这里整合一下。
对了,这三个结合最麻烦的是各种jar包版本,所以一定注意版本号的事情。以下上代码。
一、jar包依赖
dubbo:dubbo-2.5.3
dubbo需要依赖相关:commons-httpclient-3.1
xstream-1.4.1
jfreechart-1.0.19
hessian-4.0.7
curator-recipes-3.3.0
cxf-rt-frontend-jaxws-2.6.1
libthrift-0.8.0
commons-pool2-2.4.2
jedis:jedis-2.0.0
spring-frammework相关jar包版本4.2.5.RELEASE
二、project结构
本项目分为三层结构,core,service,web。core包放modle和service接口,目前没有使用到表,所以代码中只有service接口;
service是服务发布方或者叫服务器提供方,用来注册服务并且供使用方调用获取服务,具体代码放service接口的实现类,以及dubbo
spring等的配置。web是服务器消费方或者叫使用方,调取service中发布的服务,具体代码放controller,以及dubbo spring等的配置。
三、代码
3.1 代码结构
3.2 core代码
3.2.1 结构
3.2.2 只有一个service接口,具体代码
public interface ArticleService {
public String get() ;
}
public String get() ;
}
3.3 service代码
3.3.1 结构
3.3.2 配置文件provider.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" >
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
< dubbo :application name ="provider-of-helloworld-app" />
<!-- 使用 redis 注册中心暴露发现服务地址 -->
< dubbo :registry address ="redis://redisIP:redisPort" />
< dubbo :protocol name ="dubbo" port ="20880" />
<!-- 生成远程服务代理 -->
< dubbo :service interface ="com.liang.service.ArticleService" ref ="articleService" ></ dubbo :service>
<bean id ="articleService" class ="com.liang.service.impl.articleServiceImpl" ></bean>
</beans>
<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 ="provider-of-helloworld-app" />
<!-- 使用 redis 注册中心暴露发现服务地址 -->
< dubbo :registry address ="redis://redisIP:redisPort" />
< dubbo :protocol name ="dubbo" port ="20880" />
<!-- 生成远程服务代理 -->
< dubbo :service interface ="com.liang.service.ArticleService" ref ="articleService" ></ dubbo :service>
<bean id ="articleService" class ="com.liang.service.impl.articleServiceImpl" ></bean>
</beans>
3.3.3 pom.xml
<?
xml version
="1.0"
encoding
="UTF-8"
?>
<project xmlns ="http://maven.apache.org/POM/4.0.0"
xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<parent>
<artifactId>dobble-redis-demo </artifactId>
<groupId>com.liang </groupId>
<version>1.0-SNAPSHOT </version>
</parent>
<modelVersion>4.0.0 </modelVersion>
<artifactId>dubbo-demo-service </artifactId>
<dependencies>
<dependency>
<groupId>org.apache.commons </groupId>
<artifactId>commons-pool2 </artifactId>
<version>2.4.2 </version>
</dependency>
<dependency>
<groupId>com.liang </groupId>
<artifactId>dubbo-demo-core </artifactId>
<version>1.0-SNAPSHOT </version>
</dependency>
<dependency>
<groupId>org.springframework.data </groupId>
<artifactId>spring-data-redis </artifactId>
<version>1.4.1.RELEASE </version>
</dependency>
</dependencies>
</project>
<project xmlns ="http://maven.apache.org/POM/4.0.0"
xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<parent>
<artifactId>dobble-redis-demo </artifactId>
<groupId>com.liang </groupId>
<version>1.0-SNAPSHOT </version>
</parent>
<modelVersion>4.0.0 </modelVersion>
<artifactId>dubbo-demo-service </artifactId>
<dependencies>
<dependency>
<groupId>org.apache.commons </groupId>
<artifactId>commons-pool2 </artifactId>
<version>2.4.2 </version>
</dependency>
<dependency>
<groupId>com.liang </groupId>
<artifactId>dubbo-demo-core </artifactId>
<version>1.0-SNAPSHOT </version>
</dependency>
<dependency>
<groupId>org.springframework.data </groupId>
<artifactId>spring-data-redis </artifactId>
<version>1.4.1.RELEASE </version>
</dependency>
</dependencies>
</project>
3.3.4 实现类代码
public class articleServiceImpl
implements ArticleService {
public String get() {
return "success" ;
}
}
public String get() {
return "success" ;
}
}
3.3.5 启动代码
public class test {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:provider.xml") ;
context.start() ;
System. in.read() ; // 按任意键退出
}
}
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:provider.xml") ;
context.start() ;
System. in.read() ; // 按任意键退出
}
}