dubbox的使用案例,完成两个项目之间的交互

xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


开发步骤
完成服务提供方的编写,完成服务消费方的编写

  • 第一步:创建服务提供方的工程
    dubbox的使用案例,完成两个项目之间的交互_第1张图片

  • 第二步:配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.echo.dubboxservicegroupId>
  <artifactId>dubbox-serviceartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  <name>dubbox-servicename>

   <properties>     
        <spring.version>4.2.4.RELEASEspring.version>
   properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>   

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.8.4version>            
        dependency>
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
            <version>0.1version>
        dependency>

        <dependency>
            <groupId>javassistgroupId>
            <artifactId>javassistartifactId>
            <version>3.11.0.GAversion>
        dependency>

    dependencies>
   <build>  
      <plugins>
          <plugin>  
              <groupId>org.apache.maven.pluginsgroupId>  
              <artifactId>maven-compiler-pluginartifactId>  
              <version>2.3.2version>  
              <configuration>  
                  <source>1.8source>  
                  <target>1.8target>  
              configuration>  
          plugin>

          <plugin>
            <groupId>org.apache.tomcat.mavengroupId>
            <artifactId>tomcat7-maven-pluginartifactId>
            <version>2.2version>
            <configuration>
                <port>10080port>
            configuration>
          plugin>
      plugins>  
    build>
project>
  • 第三步:配置web.xml

<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" id="WebApp_ID" version="2.5">
  <display-name>springmvcdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>


  
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext*.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>


web-app>
  • 第四步:配置applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <dubbo:application name="dubbox-service"/>
    <dubbo:registry address="zookeeper://192.168.25.129:2181"/>
    <dubbo:annotation package="com.echo.service.impl" />

beans>
  • 第五步:编写接口
package com.echo.service;
public interface UserService {
    public String getName();
}
  • 第六步:编写实现
package com.echo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.echo.service.UserService;
@Service
public class UserServiceImpl implements UserService{

    @Override
    public String getName() {
        return "echo";
    }

}

注意:要在开发工具上配置一个离线约束http://code.alibabatech.com/schema/dubbo/dubbo.xsd

  • 第七步:编写服务消费方,创建工程
    dubbox的使用案例,完成两个项目之间的交互_第2张图片

  • 第八步:配置web.xml,注意:两个工程的pom文件都一样,只是服务端口不能相同


<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">  
   
    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>  
            <param-name>forceEncodingparam-name>  
            <param-value>trueparam-value>  
        init-param>  
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>   

  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
web-app>
  • 第九步:编写控制类
package com.echo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.echo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference//代表远程引用
    private UserService userServie;

    @RequestMapping("/showName")
    @ResponseBody
    public String showName(){
        return userServie.getName();
    }
}
  • 第十步:将服务提供方复制到服务消费方
    dubbox的使用案例,完成两个项目之间的交互_第3张图片

  • 第十一步:保证zookeeper是启动的

  • 第十二步:在maven仓库配置dubbox的支持jar包,注意:要首先保证系统环境变量已经配置了maven
    首先将dubbo-2.8.4放在D盘根目录下,然后执行这条命令:mvn install:install-file -Dfile=d:\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar

  • 第三步:先启动服务提供方,再启动服务消费方,访问服务消费方,看是否是服务提供方返回的信息,如果是demo运行成功(访问路径:http://localhost:10081/user/showName.do)

源码地址:链接:https://pan.baidu.com/s/1t0kGDoFLCll5m8C51YSyvQ 密码:s7bn


做一个有底线的博客主

你可能感兴趣的:(Dubbox)