分布式服务框架Dubbo入门案例和项目源码

来源:互联网 发布:地热模拟软件 编辑:程序博客网 时间:2024/04/30 16:01
本项目源代码:http://download.csdn.net/detail/fansunion/9498406
  


Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,

是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
   官方网站: http://dubbo.io/

    本项目代码,根据官方提供的dubbo-ws-demo-master例子,改造而来。
    官网例子源码:https://github.com/dubbo/dubbo-ws-demo

    官方的例子,都放在1个项目中,接口、实现类、Java应用测试例子。
   
    自己给改造了下,方便在项目中直接使用。
    虽说是HelloWorld,也还是要向实际情况靠拢。


   3个项目




1.web-service接口项目, 定义接口和供调用放引入的dubbo配置。


 接口HelloService 
 
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.dubbo.demo;  
  2. blic interface HelloService {  
  3.   String hello(String name);  



接口定义的dubbo配置
  
spring-dubbo.xml
  
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  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:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd  
  6.        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  7.        ">       
  8.     <dubbo:application name="ws-demo" />  
  9.     <dubbo:registry address="N/A" />  
  10.     <dubbo:reference id="helloService" interface="com.dubbo.demo.HelloService" version="1.0.0"  
  11.                    url="webservice://127.0.0.1:9000/com.dubbo.demo.HelloService"/>  
  12. </beans>  


maven配置
 pom.xml
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.shop</groupId>  
  6.     <version>1.0.0-SNAPSHOT</version>  
  7.     <name>web-service</name>  
  8.     <url>http://maven.apache.org</url>;  
  9.     <dependencies>  
  10.         <dependency>  
  11.             <groupId>com.alibaba</groupId>  
  12.             <artifactId>dubbo</artifactId>  
  13.             <version>2.4.10</version>  
  14.         </dependency>  
  15.   
  16.         <dependency>  
  17.             <groupId>org.apache.cxf</groupId>  
  18.             <artifactId>cxf-rt-frontend-simple</artifactId>  
  19.             <version>2.6.1</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.apache.cxf</groupId>  
  23.             <artifactId>cxf-rt-transports-http</artifactId>  
  24.             <version>2.6.1</version>  
  25.         </dependency>  
  26.   
  27.     </dependencies>  
  28.     <build>  
  29.         <finalName>web-service</finalName>  
  30.   
  31.         <plugins>  
  32.             <plugin>  
  33.                 <artifactId>maven-compiler-plugin</artifactId>  
  34.                 <configuration>  
  35.                     <source>1.6</source>  
  36.                     <target>1.6</target>  
  37.                     <encoding>UTF-8</encoding>  
  38.                 </configuration>  
  39.             </plugin>  
  40.   
  41.         </plugins>  
  42.     </build>  
  43.     <artifactId>web-service</artifactId>  
  44. </project>  


2.web-service-impl接口实现项目

  接口实现类HelloServiceImpl 
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. package com.dubbo.demo.impl;  
  2.   
  3. import com.dubbo.demo.HelloService;  
  4.   
  5. public class HelloServiceImpl implements HelloService {  
  6.     @Override  
  7.     public String hello(String name) {  
  8.         return "Hello, " + name + "!";  
  9.     }  
  10. }  

 
接口实现dubbo配置
spring-provider.xml
 
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  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:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd  
  6.        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  7.        ">  
  8.     <dubbo:application name="ws-demo" />  
  9.   
  10.     <dubbo:registry address="N/A" />  
  11.       
  12.     <dubbo:protocol name="webservice" port="9000" server="servlet" />  
  13.   
  14.     <bean id="helloService" class="com.dubbo.demo.impl.HelloServiceImpl"/>  
  15.       
  16.     <dubbo:service interface="com.dubbo.demo.HelloService" version="1.0.0"  
  17.                    protocol="webservice" ref="helloService"/>  
  18. </beans>  


Maven配置
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.shop</groupId>  
  6.     <artifactId>web-service-impl</artifactId>  
  7.     <packaging>war</packaging>  
  8.     <version>1.0.0-SNAPSHOT</version>  
  9.     <name>dubbo-ws Maven Webapp</name>  
  10.     <url>http://maven.apache.org</url>;  
  11.     <dependencies>  
  12.       
  13.         <dependency>  
  14.             <groupId>com.shop</groupId>  
  15.             <artifactId>web-service</artifactId>  
  16.             <version>1.0.0-SNAPSHOT</version>  
  17.         </dependency>  
  18.           
  19.     </dependencies>  
  20.     <build>  
  21.         <finalName>web-service-impl</finalName>  
  22.         <plugins>  
  23.             <plugin>  
  24.                 <artifactId>maven-compiler-plugin</artifactId>  
  25.                 <configuration>  
  26.                     <source>1.6</source>  
  27.                     <target>1.6</target>  
  28.                     <encoding>UTF-8</encoding>  
  29.                 </configuration>  
  30.             </plugin>  
  31.           
  32.         </plugins>  
  33.     </build>  
  34. </project>  

 
   web.xml配置
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.   
  7.     <display-name>web-service</display-name>  
  8.   
  9.     <listener>  
  10.         <listener-class>org.springframework.web.context.ContextLoaderListener  
  11.         </listener-class>  
  12.     </listener>  
  13.     <context-param>  
  14.         <param-name>contextConfigLocation</param-name>  
  15.         <param-value>  
  16.             classpath*:spring-context.xml  
  17.         </param-value>  
  18.     </context-param>  
  19.   
  20.     <servlet>  
  21.         <servlet-name>dubbo</servlet-name>  
  22.         <servlet-class>  
  23.             com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet  
  24.         </servlet-class>  
  25.         <load-on-startup>1</load-on-startup>  
  26.     </servlet>  
  27.     <servlet-mapping>  
  28.         <servlet-name>dubbo</servlet-name>  
  29.         <url-pattern>/*</url-pattern>  
  30.     </servlet-mapping>  
  31. </web-app>  

 

3.接口测试(调用方)项目


接口调用代码
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. import com.dubbo.demo.HelloService;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class ConsumerMain {  
  6.     public static void main(String[] args) {  
  7.         ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-dubbo.xml");  
  8.         classPathXmlApplicationContext.start();  
  9.   
  10.         HelloService helloService = (HelloService) classPathXmlApplicationContext.getBean("helloService");  
  11.         String world = helloService.hello("World");  
  12.   
  13.         System.out.println("=====================================");  
  14.         System.out.println(world);  
  15.         System.out.println("=====================================");          
  16.     }  
  17. }   



maven配置
  类似上面的

Web程序访问
  
ConsumerMain是通过应用程序的方式,访问Dubbo包装的服务。
 而 

  
[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. @Controller  
  2. @RequestMapping("")  
  3. public class HelloWorldController {  
  4.   
  5.     @Autowired  
  6.     private HelloService helloService;  
  7.       
  8.     @ResponseBody  
  9.     @RequestMapping("hello")  
  10.     public String hello(){  
  11.         return helloService.hello("hi dubbo");  
  12.     }  
  13.       
  14. }  



web.xml
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.   
  7.     <display-name>web-service-test</display-name>  
  8.   
  9.     <listener>  
  10.         <listener-class>org.springframework.web.context.ContextLoaderListener  
  11.         </listener-class>  
  12.     </listener>  
  13.     <context-param>  
  14.         <param-name>contextConfigLocation</param-name>  
  15.         <param-value>  
  16.             classpath*:spring-context.xml  
  17.         </param-value>  
  18.     </context-param>  
  19.   
  20.     <servlet>  
  21.         <servlet-name>springMvc</servlet-name>  
  22.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  23.         <init-param>  
  24.             <param-name>contextConfigLocation</param-name>  
  25.             <param-value>classpath:spring-mvc-servlet.xml</param-value>  
  26.         </init-param>  
  27.         <load-on-startup>1</load-on-startup>  
  28.     </servlet>  
  29.     <servlet-mapping>  
  30.         <servlet-name>springMvc</servlet-name>  
  31.         <url-pattern>/</url-pattern>  
  32.     </servlet-mapping>  
  33. </web-app>  




4.项目测试 
   a.启动服务项目
     web-service-imp
     
   b.启动Java应用程序
ConsumerMain,打印结果。
      
   c.启动Web应用程序web-service-test,访问http://localhost:9080/hello
   

本项目源代码:http://download.csdn.net/detail/fansunion/9498406


文章转自:http://blog.csdn.net/FansUnion/article/details/51211364?locationNum=3&fps=1

0 0
原创粉丝点击