分布式zookeeper+dubbo的简单配置使用

本文介绍如何使用Dubbo实现分布式服务的提供与消费过程,包括创建服务提供者、配置服务注册中心、定义服务接口及其实现,以及消费者的配置与调用。

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

分布式的流程图:
这里写图片描述

先创建一个提供者类

这里写图片描述

写一个接口和接口实现类

这里写图片描述
接口如下图:

public interface TestService {

    public String getTestMethod(String param);

    public String getTestMethod1(String param);


}

接口实现类如下图:

import com.xdl.service.TestService;

public class TestServiceImpl implements TestService {

    public String getTestMethod(String param) {
        String result = null;
        switch (param) {
        case "1":
            result = "1";
            break;
        case "2":
            result = "2";
            break;
        case "3":
            result = "3";
            break;

        default:
            result = "0";
            break;
        }
        return result;
    }

    @Override
    public String getTestMethod1(String param) {
        String result = null;
        switch (param) {
        case "1":
            result = "1";
            break;
        case "2":
            result = "2";
            break;
        case "3":
            result = "3";
            break;

        default:
            result = "0";
            break;
        }
        return result;
    }



}

配置文件如下:

<?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 ">
    <!-- 具体的实现bean -->
    <bean id="testService" class="com.xdl.service.impl.TestServiceImpl" />
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="provider" />
    <!-- 使用zookeeper注册中心暴露服务地址 ip根据自己的设置-->
    <dubbo:registry address="zookeeper://192.168.100.116:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="29014" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xdl.service.TestService" ref="testService" />
</beans>

如果需要提供更多服务就可以复制下面代码改成相应的接口路径和接口名(首字母小写)
提供别的接口(功能)供接收者调用

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xdl.service.TestService" ref="testService" />
    <dubbo:service interface="com.xdl.service.TestService2" ref="testService2" />
注册服务

这里用main方法来代替调用服务

package com.test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestServiceTest {
    public static void main(String[] args) {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
                new String[]{"application.xml"});    //读取xml配置文件  获得提供者对象
        context.start();   
        System.out.println("提供者服务已注册成功");    
        try {  
            System.in.read();//让此程序一直跑,表示一直提供服务  
        } catch (IOException e) {         
            e.printStackTrace();  
        }    
    }  
}
创建消费者

这里写图片描述

配置文件如下:
<?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" />
    <!-- 使用xxx注册中心暴露发现服务地址 ip地址和上一个提供者配置文件中的保持一致-->
    <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.100.116:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="testService" interface="com.xdl.service.TestService" />
</beans> 

这里测试用main方法测试
在mvc中类似于contrller层 响应请求


import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xdl.service.TestService;

public class ConsumerServiceTest {

    public static void main(String[] args) {
    //读取xml配置文件 获得bean组件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application.xml" });
        context.start();
        TestService testService = (TestService) context.getBean("testService");
//调用接口的方法   System.out.println(testService.getTestMethod("1"));
        System.out.println(testService.getTestMethod1("2"));
        try {
            System.in.read();
        } catch (IOException e) {       
            e.printStackTrace();
        }  

    }

先启动提供者(接口)和注册中心(zookeeper)
再调用服务

dubbo监控中心访问

http://192.168.100.116(ip地址是配置文件设置的)/dubbo-admin-2.5.4-SNAPSHOT(部署在tmocat上 应用的war包或者jar包名称)/

如图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值