Dubbo+zookeeper入门实例

该博客介绍了如何使用Spring、Dubbo和Zookeeper搭建服务提供者和消费者。服务提供者配置了Dubbo应用名、Zookeeper注册中心地址,并通过@Service注解标记服务接口实现。服务消费者同样配置了应用名、注册中心,并通过@Reference注解远程调用服务。整个流程涉及web.xml、springmvc.xml的配置以及Tomcat的内置配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录使用,帮助理解
源代码点击这里

服务方

服务方的目录结构
在这里插入图片描述

  • 依赖
    spring的一些依赖,内置tomcat+dubbo+zookeeper的依赖
<!-- dubbo组件 -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.5.10</version>
	</dependency>
	
	<!-- zookeeper -->
	<dependency>
	    <groupId>org.apache.zookeeper</groupId>
	    <artifactId>zookeeper</artifactId>
	    <version>3.4.12</version>
	</dependency>
	
	<!-- zookeeper客户端 -->
	<dependency>
	    <groupId>com.github.sgroschupf</groupId>
	    <artifactId>zkclient</artifactId>
	    <version>0.1</version>
	</dependency>
	<build>
	
  <plugins>
	    <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-compiler-plugin</artifactId>
	        <version>3.7.0</version>
	        <configuration>
	            <source>1.8</source>
	            <target>1.8</target>
	            <encoding>UTF8</encoding>
	        </configuration>
	    </plugin>
	 	
	 	<!-- 给maven项目 内置一个tomcat,之后 可以直接运行 -->
	    <plugin>
	    <groupId>org.apache.tomcat.maven</groupId>
	    <artifactId>tomcat7-maven-plugin</artifactId>
	    <configuration>
	        <port>8802</port>
	        <path>/</path>          
	     </configuration>
	</plugin> 
	</plugins>
	</build>
  
  • 配置
  1. web.xml
    因为是war包所以需要再webapp下建立WEB-INF/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">
 <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>
  1. applicationContext.xml
	<!-- 配置dubbo的应用名称 -->
	
	<dubbo:application name="students-server" />
	<!-- 配置注册中心地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.2.129:2181"  />
	
	<!-- 配置dubbo扫描包  :将@Service所在包 放入 dubbo扫描中,供后续 dubbo在rpc时使用-->
	<dubbo:annotation package="org.students.server.impl" />

	<!--@Service所在包 放入springIOC容器中,供后续 依赖注入时使用 -->
	<context:component-scan base-package="org.students.server.impl"></context:component-scan>

  • 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;
	}

}

消费方

在这里插入图片描述

  • 依赖:跟上面服务方一样
  • 配置
  1. 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">
 
  <!-- 解决post乱码   -->
  <filter>
  	<filter-name>CharacterEncodingfilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  		<init-param>
  		<param-name>foreEncoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>CharacterEncodingfilter</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>  		
  	</init-param>
  </servlet>
  <!-- 只拦截 .action结尾的请求 -->
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.action</url-pattern> 
  </servlet-mapping>
</web-app>
  1. springmvc.xml
    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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<mvc:annotation-driven>
		<!--Controller中的内容 直接打印到 浏览器中 -->
		<mvc:message-converters register-defaults="false">
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<constructor-arg value="UTF-8"/>
				</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	<!-- 配置dubbo的应用名称 -->
	<dubbo:application name="students-consumer"/>
	<!-- 配置注册中心地址     <dubbo:consumer timeout="20000" check="false"/>-->
	<dubbo:registry address="zookeeper://192.168.2.129:2181" />
	
	
	<!-- 配置dubbo扫描包 -->
	<dubbo:annotation  package="org.controller"/>
	<!-- 将控制器@Controller所在包 加入IOC容器 -->
	<context:component-scan base-package="org.controller"></context:component-scan>
		
</beans>

  • 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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值