实战Dubbo与Zookeeper、SpringMVC整合使用

10 篇文章 0 订阅
9 篇文章 0 订阅
实战Dubbo与Zookeeper、SpringMVC整合使用

dubbo-admin-2.5.3.war

CentOS 6.5

zookeeper-3.4.6.tar.gz

jdk-7u25-linux-x64.gz

Dubbo与Zookeeper、SpringMVC整合使用

1、zookeeper集群搭建

关于zookeeper集群搭建,可以参考我之前写的博客。

2、配置dubbo-admin的管理页面

(1):复制local文件夹dubbo-admin-2.5.3.war包,放在tomcat的webapps/ROOT下,然后进行解压:

拷贝:

[root@wbx1 local]# cp /usr/local/dubbo-admin-2.5.3.war /usr/local/apache-tomcat-7.0.68/webapps/ROOT/

解压:

 jar -xvf dubbo-admin-2.5.3.war

(2):然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:

(3) 然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:

3:SpringMVC与Dubbo的整合

(1)Maven构建的多模块项目

这里不介绍了,大家可以参考我之前博客   

(2)我们先开发服务注册的,就是提供服务,项目结构如图所示:

a:SDK项目里加入一个服务接口,代码如下:

b:Service项目里在pom.xml加入Dubbo和Zookeeper的jar包代码如下:

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.wangbx</groupId>
    <artifactId>project-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>project-service</artifactId>
  <packaging>jar</packaging>
  <name>project-service</name>
  <dependencies>
        <dependency>
            <groupId>com.wangbx</groupId>
            <artifactId>project-SDK</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.wangbx</groupId>
            <artifactId>project-mybatis</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>  
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>  
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
             <version>2.5.3</version>  
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

c:service项目实现具体的服务,代码如下:

package com.wbx.project.service.information;


import org.springframework.stereotype.Service;

import com.wbx.project.dto.RealNameRegDto;
import com.wbx.project.interfaces.information.TestRegistryService;

@Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService{

    public String userBind(RealNameRegDto realNameRegDto) {
        // TODO Auto-generated method stub
        return "hello"+realNameRegDto.getFullName();
    }

}
d:我们服务以及实现好了,这时要暴露服务,代码如下:

dubbo-tfs-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:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:application.properties</value>
                </list>
            </property>
        </bean>        
    <dubbo:application name="${dubbo.application.name}" owner="${dubbo.application.owner}"  />
    <dubbo:registry protocol="${dubbo.monitor.protocol}" address="${dubbo.registry.address}" />
    <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />

    <dubbo:service interface="com.wbx.project.interfaces.information.TestRegistryService" ref="testRegistryService"  timeout="100000" owner="${dubbo.application.owner}" group="${dubbo.service.group}"/>
    
</beans>

 

applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
    default-lazy-init="true">
    <context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true"
        location="classpath*:/application.properties" />
        
    <!-- 扫包 包含Service 注解 ,排除其他注解 -->
    <context:component-scan base-package="com.wbx.project">
    </context:component-scan>
    
</beans>

e:编写服务启动类DemoProvider

f:启动项目,然后我们在Dubbo管理页面上显示,已经暴露的服务,但显示还没有消费者,因为我们还没实现消费者服务,如图所示:

 第二:我们在开发服务消费者,就是调用服务

 

 

还未完成,有待更新

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值