【dubbo】3、利用maven搭建dubbo环境(demo)

本文详细介绍了Dubbo Hello Demo的搭建过程。利用Idea搭建,先创建提供者,包括创建Maven项目、用户实体、接口及实现类;接着配置提供者,添加Maven依赖和配置文件;然后创建并配置消费者,复制相关接口,引入依赖、创建配置文件和启动程序,最终成功搭建Demo。

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

1 创建提供者

利用idea搭建。

1-1 创建maven项目

在这里插入图片描述
在这里插入图片描述

需求分析:
hello的demo,消费者传输用户名,提供者分解用户名返回hello + name。

2 创建提供者

创建用户实体 (provider\src\main\java\entity\userentity.java)

package entity;

public class userentity {
    private String name;
    private  String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

创建接口(provider\src\main\java\service\userService.java):

package service;

import entity.userentity;

public interface userService {

    public String SayHello(userentity user) ;
}

创建接口实现类(provider\src\main\java\service\impl\userserviceimpl.java):

package service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import entity.userentity;
import service.userService;


public class userserviceimpl implements userService {
    public String SayHello(userentity user) {
        return "您好" + user.getName() + "性别:" + user.getSex();
    }
}


/*
* 1、将服务器提供者注册到注册中心(暴露服务)
*       1)引入dubbo依赖和zookeeper客户端(curator)
* 2、让服务消费者去注册中心订阅服务提供者的服务地址。
* */



//报错处理:
//        1、java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup
//            这是因为dubbo和zookeeper,zookeeper客户端版本冲突问题,需要一同更新版本
/*
2、java.lang.IllegalStateException: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider&timestamp=1558431972456 to registry 192.168.25.128:2181, cause: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider&timestamp=1558431972456 to zookeeper zookeeper://192.168.25.128:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-provider&dubbo=2.0.2&interface=com.alibaba.dubbo.registry.RegistryService&pid=14916&timestamp=1558431972444, cause: KeeperErrorCode = Unimplemented for /dubbo/service.userService/providers/dubbo%3A%2F%2F192.168.25.10%3A20880%2Fservice
这是因为dubbo的版本太低了,而zookeeper和其客户端版本高,提升dubbo版本,现在apache.dubbo的版本高。
3、Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener
* */

到此,provider的服务已经写好了。
下面我来配置消费者。

3 配置提供者

pom 文件中,添加maven依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dubbo</groupId>
    <artifactId>provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 引入dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
    dubbo 2.6以前的版本引入zkclient操作zookeeper
    dubbo 2.6及以后的版本引入curator操作zookeeper
    下面两个zk客户端根据dubbo版本2选1即可
    -->
        <!--<dependency>-->
            <!--<groupId>com.101tec</groupId>-->
            <!--<artifactId>zkclient</artifactId>-->
            <!--<version>0.10</version>-->
        <!--</dependency>-->
        <!-- curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>


      
    </dependencies>


</project>

在resources文件夹中添加配置文件。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:cache="http://www.springframework.org/schema/cache"
       xmlns:tx="http://code.alibabatech.com/schema/dubbo" 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://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--1、制定当前服务的名称,(同样的服务名字项目,不要和其他的服务同名)-->
<dubbo:application name="provider"></dubbo:application>
    <!--2/ 制定注册中心的位置-->
    <dubbo:registry address="zookeeper://192.168.25.128:2181"></dubbo:registry>
    <!--<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />-->
    <!--3/指定通信规则-->
    <dubbo:protocol name="dubbo" port="20880" />

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

    <!--5/和本地bean一样实现服务 -->
    <bean id="userserviceimpl" class="service.impl.userserviceimpl" />

 <!--注意:第四步和第五步中的ref和id相对应。 -->

</beans>

配置provider的启动项:
创建\provider\src\main\java\Provider.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}

到此配置结束,启动项目:
在这里插入图片描述
我们可以在dubbo监控中心看到:
在这里插入图片描述
provider项目结构如下:
在这里插入图片描述

4 配置消费者

同样创建一个maven项目:
将provider项目中entity和serve接口复制到本项目中,此时大部分工作已经结束了。
下面就是配置和调用了。

5 配置消费者

引入依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dubbo</groupId>
    <artifactId>consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
  
</dependencies>
</project>

创建配置文件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.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


    <!-- 应用名 -->
    <dubbo:application name="consumer12"></dubbo:application>
    <!-- 指定注册中心地址 -->
    <dubbo:registry address="zookeeper://192.168.25.128:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference interface="service.userService" id="userservice"></dubbo:reference>



<!--注意:dubbo:reference中的interface ,使用provider中暴露的端口 interface -->
</beans>

创建启动程序:

package testmain;


import entity.userentity;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import service.userService;

import java.io.IOException;

@Service
public class main {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        userentity userentity=new userentity();
        userentity.setName("2");
        userentity.setSex("22");
        userService demoService =  context.getBean(userService.class); // 获取远程服务代理
        String hello = demoService.SayHello(userentity); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行consumer程序:
在这里插入图片描述

到此,demo已经搭建成功。
源码下载:
https://download.csdn.net/download/aiming66/11192954

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aiming66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值