栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

通过Dubbo实现消费方远程调用服务方提供的方法

通过Dubbo实现消费方远程调用服务方提供的方法

目录

 一、对于注册中心Zookeeper的设置

 二、服务提供方

2.1pom.xml文件

2.2web.xml文件

2.3服务接口

2.4服务接口实现类

2.5类资源路径下的applicationContext-service.xml文件

三、服务消费方

3.1pom.xml文件

3.2web.xml文件

3.3cotroller

3.4在类资源路径下创建applicationContext-web.xml文件

四、对于tomcat的设置

五、运行程序

 5.1启动zookeeper服务端

 5.2启动tomcat

 5.3在提供端和消费端分别启动Tomcat

 5.4运行测试


一、对于注册中心Zookeeper的设置

进入zookeeper-3.4.6conf这个目录,把zoo_sample.cfg 改名为zoo.cfg

 在zookeeper-3.4.6这个目录下添加data和log文件夹

 打开zookeeper-3.4.6confzoo.cfg这个文件,添加如下内容:dataDir=../data dataLogDir=../log

 二、服务提供方

用骨架创建maven工程。

2.1pom.xml文件



  4.0.0

  org.example
  b0351_dubbo
  1.0-SNAPSHOT
  war

  b0351_dubbo Maven Webapp
  
  http://www.example.com



  
    UTF-8
    1.8
    1.8
    5.0.5.RELEASE
  
  
  
    org.springframework
    spring-context
    ${spring.version}
  
  
    org.springframework
    spring-beans
    ${spring.version}
  
  
    org.springframework
    spring-webmvc
    ${spring.version}
  
  
    org.springframework
    spring-jdbc
    ${spring.version}
  
  
    org.springframework
    spring-aspects
    ${spring.version}
  

  
    org.springframework
    spring-jms
    ${spring.version}
  

  
    org.springframework
    spring-context-support
    ${spring.version}
  

    
    
      com.alibaba
      dubbo
      2.6.0
    

    
      org.apache.zookeeper
      zookeeper
      3.4.7
    
    
      com.github.sgroschupf
      zkclient
      0.1
    
    
      javassist
      javassist
      3.12.1.GA
    
    
      com.alibaba
      fastjson
      1.2.47
    

    
      junit
      junit
      4.11
      test
    
  


    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        2.3.2
        
          1.8
          1.8
        
      
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
        
          
          8081
          
          /
        
      
    


  b0351_dubbo
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

2.2web.xml文件
  Archetype Created Web Application

  
    contextConfigLocation
    classpath:applicationContext*.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

2.3服务接口
public interface Service {
    public String sayHello(String name);
}

2.4服务接口实现类

这里要注意Service注解是dubbo下边的

@com.alibaba.dubbo.config.annotation.Service
public class HelloService implements Service {
    @Override
    public String sayHello(String name) {
        return "hello"+name;
    }
}

2.5类资源路径下的applicationContext-service.xml文件
    
    
    
    
    
    
    

三、服务消费方

用骨架创建maven项目。

3.1pom.xml文件

将提供方的pom.xml文件的端口号改为8082即可。

3.2web.xml文件
 Archetype Created Web Application
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:applicationContext-web.xml
    
    1
  
  
    springmvc
    /test
  

将服务提供者工程中的HelloService接口复制到当前工程 ,注意路径要保持一致:

 

 3.3cotroller
import com.alibaba.dubbo.config.annotation.Reference;
import com.buka.edu.com.Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @Reference
    private Service service;

    @RequestMapping("/test")
    @ResponseBody
    public String getName(String name){
        String result=service.sayHello(name);
        System.out.println(result);
        return result;
    }
}

3.4在类资源路径下创建applicationContext-web.xml文件
    
    
    

四、对于tomcat的设置

进入tomcatapache-tomcat-8.0.28webappsdubbo-admin-2.6.0WEB-INF这个目录,打开dubbo.properties文件:

 进行编辑:

 五、运行程序

5.1启动zookeeper服务端

进入到zookeeper-3.4.6bin目录,双击zkServer.cmd:

 5.2启动tomcat

进入到apache-tomcat-8.0.28bin目录,双击startup.bat:

 5.3在提供端和消费端分别启动Tomcat

 5.4运行测试

在浏览器地址栏输入:

http://localhost:8082/test?name=tom

即可运行出结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/746660.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号