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

Dubbo+zookeeper入门实例

Dubbo+zookeeper入门实例

服务方

服务方的目录结构

依赖
spring的一些依赖,内置tomcat+dubbo+zookeeper的依赖


	
	    com.alibaba
	    dubbo
	    2.5.10
	
	
	
	
	    org.apache.zookeeper
	    zookeeper
	    3.4.12
	
	
	
	
	    com.github.sgroschupf
	    zkclient
	    0.1
	
	
	
  
	    
	        org.apache.maven.plugins
	        maven-compiler-plugin
	        3.7.0
	        
	            1.8
	            1.8
	            UTF8
	        
	    
	 	
	 	
	    
	    org.apache.tomcat.maven
	    tomcat7-maven-plugin
	    
	        8802
	        /          
	     
	 
	
	
  

配置

    web.xml
    因为是war包所以需要再webapp下建立WEB-INF/web.xml

 
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  	org.springframework.web.context.ContextLoaderListener
  

    applicationContext.xml
	
	
	
	
	
	
	
	

	
	

java文件
service接口和实现类(注解使用阿里提供的@service)

package org.students.server.impl;

import org.students.server.StudentServer;

import com.alibaba.dubbo.config.annotation.Service;

@Service//阿里提供的
public class StudentServerImpl  implements StudentServer{

	public String server(String name) {
		return "server:" +name;
	}

}

消费方

依赖:跟上面服务方一样配置

    web.xml

 
  
  
  	CharacterEncodingfilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  		
  		foreEncoding
  		UTF-8
  	
  
  
  
  	CharacterEncodingfilter
	/*
  
  
  
  	dispatcherServlet
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
		classpath:springmvc.xml  		
  	
  
  
  
  	dispatcherServlet
  	*.action 
  

    springmvc.xml
    dubbo服务方要配,消费方也要配


	
		
		
			
					
				
		
	
	
	
	
	
	
	
	
	
	
	
	
		


java类
service的接口 和controller
service的接口:跟服务方的service接口一样,要在controller中远程调用服务端中StudentServer 然后自动装配。

package org.students.server;

public interface StudentServer {
	public String server(String name);//zs
}

package org.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.students.server.StudentServer;

import com.alibaba.dubbo.config.annotation.Reference;
@RestController
@RequestMapping("controller")
public class StudentController {
	
	@Reference//调用远程服务中的StudentServer
	 StudentServer stuServer ;
	 
	@RequestMapping("rpcSerer")
	public String rpcSerer() {
		String result = stuServer.server("zs") ;
		return result ;//将结果显示在控制台	
	}
}
测试

消费方tomcat端口号为8803
访问http://localhost:8803/controller/rpcSerer.action

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

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

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