搭建一个简单的dubbo项目

1.创建maven项目并导入相关依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.7</version>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>

2.创建一个服务项目

   1.创建类和接口

       接口:

    public interface GreetingsService {
      String sayHi(String name);
    }

        实现类:

             

    public class GreetingsServiceImpl implements GreetingsService {

      public String sayHi(String name) {
        return "hi,"+name;
      }
    }
 

    2.在xml文件中配置注册服务信息

      <?xml version="1.0" encoding="UTF-8"?>
    <!-- xsi:xml schema instance xml模式实例 -->
    <!-- xmlns *.xml文件中用到的命名空间 -->
    <!-- xmlns:xsi *.xml遵守xml规范 -->
    <!-- xmlns:dubbo 引入dubbo前缀 -->
    <!-- xsi:schemaLocation 具体用到的schema资源,必须加上dubbo的资源路径 -->
    <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="demo-provider" version="1.0"/>
    <!-- 服务将会注册到zookeeper上 ,端口必须与zookeeper配置的端口一致-->
    <dubbo:registry address="zookeeper://127.0.0.1:2183"/>
    <!-- 指明程序名和发布的端口 -->
    <dubbo:protocol  name="dubbo" port="20880"/>
    <!-- 把实现类放入到spring的容器中 -->
    <bean id="greetingsService" class="com.ns.interfact.impl.GreetingsServiceImpl"/>
    <!-- 注册到服务的注册表上 -->
    <dubbo:service interface="com.ns.interfact.GreetingsService" ref="greetingsService"/>

</beans>

3.启动服务

      public static void main(String[] args){
//读取xml配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationProvider.xml");
context.start();
System.out.println("服务启动");
try{
//使服务一直处于运行状态
System.in.read();
}catch(Exception e){
e.printStackTrace();
}

        }

3.创建一个消费端

   1.创建要调用的接口

     public interface DemoService {
String sayHello(String name);

    }

  2.配置服务端的配置文件

   <?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="demo-customer" version="1.0"/>
    <!-- 请求的服务注册的zookeeper的地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2183"/>
    <!-- 注册到服务的注册表上 -->
    <dubbo:reference interface="com.ns.interfact.GreetingsService" id="greetingsService" check="false"/>

</beans>

  3.调用服务

     public static void main(String[] args){
//读取xml配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationProvider.xml");
context.start();
GreetingsService greetingsService=context.getBean(GreetingsService.class);
System.out.println(greetingsService.sayHi("王霞"));

    }


4.安装配置并启动zookeeper

  1.从网站上下载zookeeper的压缩包https://jingyan.baidu.com/article/335530dafd4b3619cb41c3d8.html

  2.解压下载的zookeeper的压缩包


 3.修改配置文件


4.通过zkServer.cmd启动Zookeeper的服务器

5.通过zkCli.cmd启动Zookeeper的客户端,查看已经注册的服务

  

   5.出现的错误

    1.dubbo的标签找不到

       错误原因: 找不到dubbo.xsd文件,可能是因为网络原因,判断方法:点击xml文件的资源链接,看是否能够打开.

       解决办法:  将本地的xsd文件导入

      

2.zookeeper服务启动失败

  错误原因:zookeeper的配置文件从出错

  判断方式:用cmd运行zkServer.cmd的文件查看出错原因

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值