Zookeeper+Dubbo+Spring整合

请尊重他人的劳动成果,转载请务必注明出处.谢谢!

zookeeper 安装配置 传送门

dubbo 安装配置 传送门

服务提供者

  • 创建maven工程,添加必要jar包

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.9</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
  • Spring 配置略
  • 项目结构
    目录结构
  • 服务端测试类

    public interface TestService {
        String test();
        String hello(String s);
    }
    @Service("testService")
    public class TestServiceImpl implements TestService {
        public String test() {
            return "test";
        }
        public String hello(String s) {
            return "hello " + s;
        }
    }
    public class App {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("spring.xml", "spring-dubbo.xml");
            context.start();
            System.out.println("Press any key to exit.");
            System.in.read();
        }
    }
  • dubbo配置

    <?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"
           default-lazy-init="false">
        <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
        <dubbo:application name="dubbo-search"/>
        <!-- 使用zookeeper注册中心暴露服务地址
         dubbo:registry 标签一些属性的说明:
         1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。
         2)check注册中心不存在时,是否报错。
         3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。
         4)timeout注册中心请求超时时间(毫秒)。
         5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。-->
        <dubbo:registry address="zookeeper://192.168.109.132:2181"/>
        <!-- 要暴露的服务接口
         dubbo:service标签的一些属性说明:
         1)interface服务接口的路径
         2)ref引用对应的实现类的Bean的ID
         3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A
         4)register 默认true ,该协议的服务是否注册到注册中心。-->
        <dubbo:service interface="com.encyclopedia.search.service.TestService" ref="testService"/>
    </beans>
  • 启动服务端

    启动顺序 zookeeper -> dobbo -> run APP:main
    
  • 验证

    登录 dubbo http://192.168.109.132:8080/
    

    结果

服务消费者

  • 创建maven web工程,添加必要jar包

    <dependency>
        <groupId>com.encyclopedia</groupId>
        <artifactId>dubbo-search</artifactId><!-- 引入服务提供者-->
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.9</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
  • Spring 配置略
  • 项目结构
    结构
  • 消费者代码

    @Controller
    public class IndexController {
        private Logger logger = Logger.getLogger(this.getClass());
        @Autowired
        private TestService testService;
    
        @RequestMapping("/hello")
        public String index() {
            logger.info(testService.hello("zz"));
            return "index";
        }
    }
    
  • 启动web程序(略)
  • 访问程序 localhost:8080/hello
    成功输出hello zz,大功告成
    成功

总结

启动顺序 zookeeper->dubbo->服务提供程序->服务消费程序

完整项目 传送门

  • 服务端启动脚本

    #!/bin/bash
    #Build home
    export BUILD_HOME=/root/project/encyclopedia
    cd $BUILD_HOME
    git fetch
    git checkout master
    git pull origin master
    mvn clean install
    cd $BUILD_HOME/search
    mvn exec:java
    
  • 消费者为web项目 请自行部署

注意事项

    dubbo-admin开放2181端口 服务提供程序开放20880端口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值