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.0modelVersion>
  4. <groupId>dubboProvidegroupId>
  5. <artifactId>dubbo-providerDemoartifactId>
  6. <version>0.0.1-SNAPSHOTversion>
  7. <packaging>jarpackaging>
  8. <properties>
  9. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  10. properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.alibabagroupId>
  14. <artifactId>dubboartifactId>
  15. <version>2.4.10version>
  16. <exclusions>
  17. <exclusion>
  18. <groupId>org.springframeworkgroupId>
  19. <artifactId>springartifactId>
  20. exclusion>
  21. exclusions>
  22. dependency>
  23. <dependency>
  24. <groupId>com.101tecgroupId>
  25. <artifactId>zkclientartifactId>
  26. <version>0.4version>
  27. dependency>
  28. <dependency>
  29. <groupId>org.springframeworkgroupId>
  30. <artifactId>spring-contextartifactId>
  31. <version>3.2.8.RELEASEversion>
  32. dependency>
  33. <dependency>
  34. <groupId>org.springframeworkgroupId>
  35. <artifactId>spring-coreartifactId>
  36. <version>3.2.8.RELEASEversion>
  37. dependency>
  38. <dependency>
  39. <groupId>org.springframeworkgroupId>
  40. <artifactId>spring-beansartifactId>
  41. <version>3.2.8.RELEASEversion>
  42. dependency>
  43. <dependency>
  44. <groupId>junitgroupId>
  45. <artifactId>junitartifactId>
  46. <version>4.12version>
  47. <scope>testscope>
  48. dependency>
  49. <dependency>
  50. <groupId>org.springframeworkgroupId>
  51. <artifactId>spring-testartifactId>
  52. <version>3.2.8.RELEASEversion>
  53. <scope>testscope>
  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. <dubbo:application name="dubboProvide" />
  15. <dubbo:registry address="zookeeper://192.168.0.190:2181" />
  16. <dubbo:protocol name="dubbo" port="20880" />
  17. <dubbo:service interface="com.lin.provider.DemoService" ref="demoServiceImpl" />
  18. <bean id="demoServiceImpl" class="com.lin.provider.DemoServiceImpl" />
  19. 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. <dubbo:application name="dubboCustom" />
  15. <dubbo:registry address="zookeeper://192.168.0.190:2181" />
  16. <dubbo:protocol name="dubbo" port="20880" />
  17. <dubbo:reference id="demoServiceImpl" interface="com.lin.provider.DemoService" />
  18. 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
调用成功!。



你可能感兴趣的:(java)