1、新建maven项目:

image.png

2、修改pom.xml文件,添加依赖:

    
4.0.0
    
com.ym
    
testdubbodemo
    
1.0-SNAPSHOT
    
        
service
        
serviceimpl
        
testweb
    
    
pom
    
        
UTF-8
    
    
        
            
junit
            
junit
            
4.12
        
        
            
org.springframework
            
spring-webmvc
            
5.0.8.RELEASE
        
        
        
            
com.alibaba
            
dubbo
            
2.5.9
            
                
                    
org.springframework
                    
spring
                
            
        
        
        
            
com.101tec
            
zkclient
            
0.10
        
        
            
org.slf4j
            
slf4j-api
            
1.7.25
        
    
    
        
            
                
                    
maven-clean-plugin
                    
3.0.0
                
                
                
                    
maven-resources-plugin
                    
3.0.2
                
                
                    
maven-compiler-plugin
                    
3.7.0
                
                
                    
maven-surefire-plugin
                    
2.20.1
                
                
                    
maven-jar-plugin
                    
3.0.2
                
                
                    
maven-install-plugin
                    
2.5.2
                
                
                    
maven-deploy-plugin
                    
2.8.2
                
            
        
    

3、创建远程接口模块,这个模块用于客户端和接口实现调用,如controller,serviceImpl;

image.png

创建接口:

com.ym;TestService {    String getData(String name);}

4、创建接口实现模块:

image.png

实现远程接口:

com.ym.service.impl;com.ym.TestService;org.springframework.stereotype.;()TestServiceImpl TestService {    String getData(String name) {        + name;    }}

在spring配置文件中,配置dubbo相关配置项:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-5.0.8.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   <!--扫描注解-->
   <context:component-scan base-package="com.ym"/>
   <!--使用dubbo的另外一个原因,可以和spring无缝整合-->
   <!--
       目的:找到注册中心,告诉他我是谁,端口是多少
   -->
   <!--1、配置别名,目的是在后台看到这个服务的别名,好区分到底是谁-->
   <dubbo:application name="test1"/>
   <!--2、配置注册中心
       address表示注册中心的地址,protocol表示注册中心的协议格式
   -->
   <dubbo:registry address="192.168.1.224:2181" protocol="zookeeper"/>
   <!--配置要注册的服务-->
   <dubbo:service interface="com.ym.TestService" ref="testServiceImpl" timeout="6000"/>
   <!--配置端口,因为消费者要想连接服务提供者,必须知道ip和端口,ip在服务注册到Zookeeper的时候就已经知道了-->
   <dubbo:protocol name="dubbo" port="12345"/>

</beans>

配置web.xml文件

  
    
Archetype Created Web Application
    
        
contextConfigLocation
        
classpath:applicationContext.xml
    
    
        
org.springframework.web.context.ContextLoaderListener
    

5、启动接口模块:clean tomcat7:run

image.png

6、打开dubbo控制台查看服务信息

image.png

7、创建客户端模块

image.png

修改pom.xml文件:

    
        
testdubbodemo
        
com.ym
        
1.0-SNAPSHOT
    
    
4.0.0
    
testweb
    
war
    
testweb Maven Webapp
    
    
http://www.example.com
    
        
UTF-8
        
1.8
        
1.8
    
    
        
            
junit
            
junit
            
4.11
            
test
        
        
            
javax.servlet
            
javax.servlet-api
            
3.1.0
            
provided
        
        
            
com.ym
            
service
            
1.0-SNAPSHOT
        
    
    
        
testweb
        
            
                
org.apache.tomcat.maven
                
tomcat7-maven-plugin
                
2.2
                
                    
9001
                    
/
                
            
        
    

配置springmvc文件:

   
   
   
   
修改web.xml:
   
Archetype Created Web Application
   
       
springmvc
       
org.springframework.web.servlet.DispatcherServlet
       
           
contextConfigLocation
           
classpath:spring-mvc.xml
       
       
2
   
   
       
springmvc
       
/*
   
8、启动testweb,并观察dubbo控制台;