zoukankan      html  css  js  c++  java
  • spring整合Dubbo

    pom.xml

      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/xsd/maven-4.0.0.xsd">
      4     <modelVersion>4.0.0</modelVersion>
      5     <groupId>io.guangsoft</groupId>
      6     <artifactId>dubbo</artifactId>
      7     <version>0.1</version>
      8     <packaging>war</packaging>
      9     <properties>
     10         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     11         <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
     12     </properties>
     13     <dependencies>
     14         <dependency>
     15             <groupId>junit</groupId>
     16             <artifactId>junit</artifactId>
     17             <version>3.8.1</version>
     18             <scope>test</scope>
     19         </dependency>
     20         <dependency>
     21             <groupId>javax.servlet</groupId>
     22             <artifactId>servlet-api</artifactId>
     23             <version>2.5</version>
     24             <scope>provided</scope>
     25         </dependency>
     26         <dependency>
     27             <groupId>javax.servlet</groupId>
     28             <artifactId>jstl</artifactId>
     29             <version>1.2</version>
     30         </dependency>
     31         <dependency>
     32             <groupId>org.springframework</groupId>
     33             <artifactId>spring-context</artifactId>
     34             <version>${org.springframework.version}</version>
     35         </dependency>
     36         <dependency>
     37             <groupId>org.springframework</groupId>
     38             <artifactId>spring-context-support</artifactId>
     39             <version>${org.springframework.version}</version>
     40         </dependency>
     41         <dependency>
     42             <groupId>org.springframework</groupId>
     43             <artifactId>spring-web</artifactId>
     44             <version>${org.springframework.version}</version>
     45         </dependency>
     46         <dependency>
     47             <groupId>org.springframework</groupId>
     48             <artifactId>spring-webmvc</artifactId>
     49             <version>${org.springframework.version}</version>
     50         </dependency>
     51 
     52         <dependency>
     53             <groupId>org.codehaus.jackson</groupId>
     54             <artifactId>jackson-core-asl</artifactId>
     55             <version>1.9.2</version>
     56         </dependency>
     57         <dependency>
     58             <groupId>org.codehaus.jackson</groupId>
     59             <artifactId>jackson-mapper-asl</artifactId>
     60             <version>1.9.13</version>
     61         </dependency>
     62         <dependency>
     63             <groupId>net.sf.json-lib</groupId>
     64             <artifactId>json-lib</artifactId>
     65             <version>2.4</version>
     66             <classifier>jdk15</classifier>
     67         </dependency>
     68         <dependency>
     69             <groupId>com.alibaba</groupId>
     70             <artifactId>dubbo</artifactId>
     71             <version>2.5.3</version>
     72             <exclusions>
     73                 <exclusion>
     74                     <artifactId>spring</artifactId>
     75                     <groupId>org.springframework</groupId>
     76                 </exclusion>
     77             </exclusions>
     78         </dependency>
     79         <dependency>
     80             <groupId>org.apache.zookeeper</groupId>
     81             <artifactId>zookeeper</artifactId>
     82             <version>3.3.6</version>
     83             <exclusions>
     84                 <exclusion>
     85                     <groupId>log4j</groupId>
     86                     <artifactId>log4j</artifactId>
     87                 </exclusion>
     88             </exclusions>
     89         </dependency>
     90         <dependency>
     91             <groupId>log4j</groupId>
     92             <artifactId>log4j</artifactId>
     93             <version>1.2.16</version>
     94         </dependency>
     95         <dependency>
     96             <groupId>com.github.sgroschupf</groupId>
     97             <artifactId>zkclient</artifactId>
     98             <version>0.1</version>
     99         </dependency>
    100     </dependencies>
    101     <build>
    102         <plugins>
    103             <plugin>
    104                 <groupId>org.apache.maven.plugins</groupId>
    105                 <artifactId>maven-compiler-plugin</artifactId>
    106                 <configuration>
    107                     <source>1.8</source>
    108                     <target>1.8</target>
    109                 </configuration>
    110             </plugin>
    111         </plugins>
    112     </build>
    113 </project>

    dubbo-provider目录结构

    web.xm;

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
     3   <display-name>dubbo-provider</display-name>
     4   <listener>
     5     <listener-class>
     6             org.springframework.web.context.ContextLoaderListener
     7         </listener-class>
     8   </listener>
     9   <context-param>
    10     <param-name>contextConfigLocation</param-name>
    11     <param-value>classpath:applicationContext.xml</param-value>
    12   </context-param>
    13   <servlet>
    14     <servlet-name>provider</servlet-name>
    15     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    16     <init-param>
    17       <param-name>contextConfigLocation</param-name>
    18       <param-value>
    19                 classpath:applicationContext.xml,classpath:applicationContext-servlet.xml
    20             </param-value>
    21     </init-param>
    22     <load-on-startup>1</load-on-startup>
    23   </servlet>
    24   <servlet-mapping>
    25     <servlet-name>provider</servlet-name>
    26     <url-pattern>*.do</url-pattern>
    27   </servlet-mapping>
    28   <filter>
    29     <filter-name>CharacterEncoding</filter-name>
    30     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    31     <init-param>
    32       <param-name>encoding</param-name>
    33       <param-value>UTF-8</param-value>
    34     </init-param>
    35   </filter>
    36   <filter-mapping>
    37     <filter-name>CharacterEncoding</filter-name>
    38     <url-pattern>/*</url-pattern>
    39   </filter-mapping>
    40   <welcome-file-list>
    41     <welcome-file>index.jsp</welcome-file>
    42   </welcome-file-list>
    43 </web-app>

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
     5        xmlns:tx="http://www.springframework.org/schema/tx"
     6        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     7        xmlns:context="http://www.springframework.org/schema/context"
     8        xsi:schemaLocation="
     9       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    10       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    11       http://www.springframework.org/schema/tx
    12       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    13       http://www.springframework.org/schema/aop
    14       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" >
    15     <context:component-scan base-package="io.guangsoft.dubbo.**"></context:component-scan>
    16     <!-- 引入服务提供者配置文件 -->
    17     <import resource="dubbo-provider.xml"/>
    18 </beans>

    applicationContext-servlet.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:aop="http://www.springframework.org/schema/aop"
     5        xmlns:tx="http://www.springframework.org/schema/tx"
     6        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     7        xmlns:mvc="http://www.springframework.org/schema/mvc"
     8        xmlns:context="http://www.springframework.org/schema/context"
     9        xsi:schemaLocation="
    10       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    11       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    12       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    13       http://www.springframewor.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    14       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    15        default-autowire="byName">
    16     <!-- 默认的注解映射的支持 -->
    17     <mvc:annotation-driven>
    18         <mvc:message-converters register-defaults="true">
    19             <bean class ="org.springframework.http.converter.StringHttpMessageConverter">
    20                 <property name ="supportedMediaTypes">
    21                     <list>
    22                         <value>text/plain;charset=UTF-8</value>
    23                     </list>
    24                 </property>
    25             </bean>
    26         </mvc:message-converters>
    27     </mvc:annotation-driven>
    28     <!-- 视图解释类 -->
    29     <bean id="viewResolver"
    30           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    31         <property name="prefix" value="/WEB-INF/jsp/" />
    32         <property name="suffix" value=".jsp" />
    33         <property name="viewClass"
    34                   value="org.springframework.web.servlet.view.JstlView" />
    35     </bean>
    36     <!-- 加载静态资源 -->
    37     <mvc:resources mapping="/css/**" location="/css/" />
    38     <mvc:resources mapping="/js/**" location="/js/" />
    39     <mvc:resources mapping="/images/**" location="/images/" />
    40 </beans>

    dubbo-provider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans
     6     http://www.springframework.org/schema/beans/spring-beans.xsd
     7     http://code.alibabatech.com/schema/dubbo
     8     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     9     <!-- 提供方应用信息,用于计算依赖关系 -->
    10     <dubbo:application name="provider" />
    11     <!-- 使用multicast广播注册中心暴露服务地址 -->
    12     <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    13     <!-- 使用zookeeper注册中心暴露服务地址 -->
    14     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    15     <!-- 用dubbo协议在20880端口暴露服务 -->
    16     <dubbo:protocol name="dubbo" port="20880" />
    17     <!-- 声明需要暴露的服务接口 -->
    18     <dubbo:service interface="io.guangsoft.dubbo.provider.ProviderService" ref="providerService" />
    19 </beans>

    ProviderService.java

    1 package io.guangsoft.dubbo.provider;
    2 
    3 public interface ProviderService {
    4     String transmit(String str);
    5 }

    ProviderServiceImpl.java

     1 package io.guangsoft.dubbo.provider;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 @Service(value = "providerService")
     6 public class ProviderServiceImpl implements ProviderService {
     7     @Override
     8     public String transmit(String str) {
     9         return "GuangSoft dubbo-provider : " + str;
    10     }
    11 }

    dubbo-consumer目录结构

    dubbo-consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans
     6     http://www.springframework.org/schema/beans/spring-beans.xsd
     7     http://code.alibabatech.com/schema/dubbo
     8     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     9     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    10     <dubbo:application name="customer" />
    11     <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    12     <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    13     <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" />
    14     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    15     <dubbo:reference id="providerService" interface="io.guangsoft.dubbo.provider.ProviderService" check="false" />
    16 </beans>

    ConsumerController.java

     1 package io.guangsoft.dubbo.consumer;
     2 
     3 import javax.annotation.Resource;
     4 import javax.servlet.http.HttpServletRequest;
     5 import javax.servlet.http.HttpServletResponse;
     6 
     7 import io.guangsoft.dubbo.provider.ProviderService;
     8 import org.springframework.stereotype.Controller;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 import org.springframework.web.servlet.ModelAndView;
    11 
    12 @Controller
    13 @RequestMapping(value = "/consumer")
    14 public class ConsumerController {
    15 
    16     @Resource(name = "providerService")
    17     private ProviderService providerService;
    18 
    19     @RequestMapping(value = "/consume.do")
    20     public ModelAndView consume(HttpServletRequest request, HttpServletResponse response) {
    21         String result = providerService.transmit("dubbo-consumer");
    22         System.out.println(result);
    23         ModelAndView mv = new ModelAndView();
    24         mv.setViewName("result");
    25         mv.addObject("result", result);
    26         return mv;
    27     }
    28 }

     result.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>dubbo</title>
     8 </head>
     9 <body>    
    10     <h1>${result}</h1>
    11 </body>
    12 </html>

     测试结果:

     

  • 相关阅读:
    springboot的build.gradle增加阿里仓库地址以及eclipse增加lombok
    mqttfx无法选择证书
    EMQX源码编译过程
    新环境chart包helmlint校验
    安装rebar3
    Ubuntu16.04下,erlang安装和rabbitmq安装步骤
    UTF-8,GBK,ANSI之间的关系和区别
    write命令
    bunzip2命令
    bzip2命令
  • 原文地址:https://www.cnblogs.com/guanghe/p/9283693.html
Copyright © 2011-2022 走看看