SpringBoot+Dubbo+Zookeeper示例

系统: win8
IDE: eclipse

  一直想了解学习一下远程调用服务框架(关于RPC),然后看到了阿里的 Dubbo,看了很多的教程自己实现了一下,感觉很棒!

安装Zookeeper

tickTime=2000
initLimit=10
syncLimit=5
dataDir=E:\zookeeper-data
clientPort=2181
  • 为了方便,也可以把 bin下的命令加入到系统环境中

provider项目

  • 使用 STS(Spring Tool Suite) 插件,新建 SpringBoot 项目,不需要额外的扩展
  • 在 resource 下 新建 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="dubbo-provider" owner="dubbo-provider"/>
    <dubbo:registry address="zookeeper://localhost:2181" check="false" subscribe="false"/>
    <dubbo:service interface="com.provider.TestService" ref="testService"/>
    <bean id="testService" class="com.provider.TestServiceImpl"></bean>
</beans>
  • 新建 TestService 接口
package com.provider;

public interface TestService {
    String sayHello(String name);
}
  • 新建 TestServiceImpl 实现类
package com.provider;

public class TestServiceImpl implements TestService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name + " !";
    }

}
  • 修改 默认的项目启动 类
package com.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:providers.xml"})
public class DubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}
  • 修改默认的启动端口
server.port=8081

consumer项目

  • 使用 STS(Spring Tool Suite) 插件,新建 SpringBoot 项目,不需要额外的扩展
  • 在 resource 下 新建 consumer.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="dubbo-consumer"/>
    <dubbo:registry check="false" address="zookeeper://localhost:2181"/>
    <dubbo:reference interface="com.provider.TestService" id="testService"/>
</beans>
  • 修改默认端口
server.port=8082
  • 修改 默认启动类
package com.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:consumer.xml"})
public class DubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}
  • 新建 IndexController 类
package com.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.provider.TestService;

@Controller
public class IndexController {

    @Autowired
    private TestService testService;

    @RequestMapping("/hello/{name}")
    @ResponseBody
    public String test(@PathVariable("name") String name) {
        return testService.sayHello(name);
    }
}

启动

  • 先启动 ZooKeeper
  • 启动 provider
  • 启动 consumer

参考:
http://www.cnblogs.com/think-in-java/p/6611249.html

0

一个有关“SpringBoot+Dubbo+Zookeeper示例”的想法

  1. 有的教程在zoo.cfg内加了dataLogDir,经过测试以后发现,在zookeeper3.3.6这个版本中,会报错。建议,按照解压后的 zoo_sample.cfg 进行配置

    0

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.