dubbo简单实现

一、配置zk


1、安装zk 下载地址http://www.apache.org/dyn/closer.cgi/zookeeper/

下载ZK后解压,然后进行启动。此处的zk部署在192.168.0.190机器上,使用默认端口2181

2、启动zk

  
  
  1. bin/zkServer.sh start

3、测试zk是否启动

  
  
  1. bin/zkCli.sh -server 192.168.0.190:2181
出现  Welcome to ZooKeeper! 启动成功

 二、新建服务端项目


1、新建项目  dubboProvide 配置服务端

pom.xml 配置如下

  
  
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>dubboProvide</groupId>
  5. <artifactId>dubbo-providerDemo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <properties>
  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.alibaba</groupId>
  14. <artifactId>dubbo</artifactId>
  15. <version>2.4.10</version>
  16. <exclusions>
  17. <exclusion>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring</artifactId>
  20. </exclusion>
  21. </exclusions>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.101tec</groupId>
  25. <artifactId>zkclient</artifactId>
  26. <version>0.4</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-context</artifactId>
  31. <version>3.2.8.RELEASE</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-core</artifactId>
  36. <version>3.2.8.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-beans</artifactId>
  41. <version>3.2.8.RELEASE</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>junit</groupId>
  45. <artifactId>junit</artifactId>
  46. <version>4.12</version>
  47. <scope>test</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-test</artifactId>
  52. <version>3.2.8.RELEASE</version>
  53. <scope>test</scope>
  54. </dependency>
  55. </dependencies>
  56. </project>

spring-dubbo-provide.xml配置如下

  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
  11. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
  13. default-lazy-init="false" >
  14. <!-- 提供方应用信息,用于计算依赖关系 -->
  15. <dubbo:application name="dubboProvide" />
  16. <!-- 使用zookeeper注册中心暴露服务地址 -->
  17. <dubbo:registry address="zookeeper://192.168.0.190:2181" />
  18. <!-- dubbo协议在20880端口暴露服务 -->
  19. <dubbo:protocol name="dubbo" port="20880" />
  20. <!-- 声明需要暴露的服务接口 -->
  21. <dubbo:service interface="com.lin.provider.DemoService" ref="demoServiceImpl" />
  22. <!-- 和本地bean一样实现服务 -->
  23. <bean id="demoServiceImpl" class="com.lin.provider.DemoServiceImpl" />
  24. </beans>

DemoServcie 如下:

  
  
  1. package com.lin.provider;
  2. public interface DemoService {
  3. public String sayHello(String name);
  4. }

DemoServiceImpl 如下:

  
  
  1. package com.lin.provider;
  2. public class DemoServiceImpl implements DemoService{
  3. @Override
  4. public String sayHello(String name) {
  5. return "Hello Dubbo,Hello " + name;
  6. }
  7. }

服务端配置完毕,接下来启动项目,提供接口

Provider  如下:

  
  
  1. package com.lin.provider;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class Provider {
  4. public static void main(String[] args) throws Exception {
  5. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-provider.xml"});
  6. context.start();
  7. System.out.println("---开启端口-----");
  8. System.in.read(); // 按任意键退出
  9. }
  10. }

运行main方法,提供接口;此时服务端启动完毕。消费者就可以调用这个暴露的接口。


2、新建dubboCustom项目,实现消费者调用服务端。

pom.xml如上。

spring-dubbo-custoom.xml如下:

  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jee="http://www.springframework.org/schema/jee"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
  11. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
  13. default-lazy-init="false" >
  14. <!-- 提供方应用信息,用于计算依赖关系 -->
  15. <dubbo:application name="dubboCustom" />
  16. <!-- 使用zookeeper注册中心暴露服务地址 -->
  17. <dubbo:registry address="zookeeper://192.168.0.190:2181" />
  18. <!-- dubbo协议在20880端口暴露服务 -->
  19. <dubbo:protocol name="dubbo" port="20880" />
  20. <!-- 声明需要暴露的服务接口 -->
  21. <dubbo:reference id="demoServiceImpl" interface="com.lin.provider.DemoService" />
  22. </beans>

消费者测试类,custom类如下:

  
  
  1. package dubboCustom;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.lin.provider.DemoService;
  4. public class Custom {
  5. public static void main(String[] args) throws Exception {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  7. new String[] { "spring-dubbo-custom.xml" });
  8. context.start();
  9. DemoService demoService = (DemoService) context.getBean("demoServiceImpl"); //
  10. String hello = demoService.sayHello("lin"); // ִ
  11. System.out.println(hello); //
  12. System.in.read();
  13. }
  14. }

运行main方法,可以看到,控制台输出

  
  
  1. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
  2. log4j:WARN Please initialize the log4j system properly.
  3. Hello Dubbo,Hello tom
调用成功!。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值