(一)dubbo+redis注册中心+spring

网上搜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() ;
}
          
     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>

       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>

      3.3.4 实现类代码
            
public class articleServiceImpl  implements ArticleService {
    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() //  按任意键退出
    }
}
 


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值