dubbo+zookeeper配置(ssm)

本文详细介绍了如何使用Dubbo框架结合Zookeeper实现服务注册与发现的过程。包括Dubbo与Zookeeper的依赖配置,服务提供方与消费方的XML配置示例,以及接口定义和服务实现的代码片段。

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

1.下载安装zookeeper

2.服务提供方配置

    <!-- dubbo  -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.5</version>
		</dependency>
		
		<!-- zookeeper -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.10</version>
		</dependency>
	  
	   <dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.8.0</version>
		</dependency>

provider.xml配置:

<?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:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-provider"/>

    <!-- 服务端需要把dubbo服务注册到zookeeper上进行广播 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
    <dubbo:provider timeout="10000" threadpool="fixed" threads="100" accepts="1000" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="xxxx.ThirdMemberService" ref="thirdMemberService" />

</beans>
ThirdMemberService接口
import java.util.List;

/**
 */
public interface ThirdMemberService {
    /**
     * 查询所有用户
     * @return
     */
    public List<Member> findUserList();
}


ThirdMemberServiceImpl类
import xxxx.ThirdMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 */
@Service("thirdMemberService")
public class ThirdMemberServiceImpl implements ThirdMemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<Member> findUserList() {
        return memberMapper.findMemberByMobileAndName("15215710450","小明");
    }
}

3.消费者

  <!-- dubbo  -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.5</version>
		</dependency>
		
		<!-- zookeeper -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.10</version>
		</dependency>
	  
	   <dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.8.0</version>
		</dependency>

consumer.xml

<?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:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="33333" />
    </dubbo:application>

    <!-- 服务端需要把dubbo服务注册到zookeeper上进行广播 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="thirdMemberService" interface="xxx.ThirdMemberService" check="false"/>
</beans>

把ThirdMemberService拷贝到消费方,路径需一致,直接调用

import xxx.ThirdMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 */
@Controller
@RequestMapping("/third")
public class ThirdMemberController {

    @Autowired
    private ThirdMemberService thirdMemberService;

    @RequestMapping("/findList")
    @ResponseBody
    public List<Member> findUserList() {
        return thirdMemberService.findUserList();
    }

}

4.启动zookeeper,先启动服务方,在启动消费方

二、其他方式,只展示配置文件

引入的jar:

 <dependency>
          <groupId>org.apache.dubbo</groupId>
          <artifactId>dubbo-dependencies-bom</artifactId>
          <version>2.7.7</version>
          <type>pom</type>
          <scope>import</scope>
   </dependency>
<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.7</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>

生产者:

<?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: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://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="provider" />
	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<dubbo:provider timeout="10000" />
	<!-- 声明需要暴露的服务接口 -->


	<dubbo:service interface="xxxx.UserService" ref="userService" loadbalance="roundrobin" />
	<bean id="userService" class="xxxx.UserServiceImpl"></bean>

	<import resource="classpath*:xxx.xml" />
</beans>

消费者:

<?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: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://code.alibabatech.com/schema/dubbo
	                           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<dubbo:application name="consumer" />
	<dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper" />
	<dubbo:consumer timeout="10000" />

	<dubbo:reference id="userService" retries="0" interface="xxx.UserService" check="false"/>



</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值