dubbo入门

dubbo-server

在这里插入图片描述

  1. 创建父工程dubbo-server
    在这里插入图片描述
  2. 以同样的方法创建两个子模块, server-api和server-provider. 其中server-api存放dubbo服务暴露的接口, server-provider为服务的实现模块.
  3. server-api模块实现
public interface IGpHello {

    String sayHello(String msg);
}
  1. server-provider模块实现
// 引入依赖
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>server-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>

// 服务的实现类
public class IGpHelloImpl implements IGpHello {
    @Override
    public String sayHello(String msg) {
        return "hello, " + msg;
    }
}

// 服务端的配置文件
<?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="dubbo-server" owner="jzx"/>
    <!--    <dubbo:monitor protocol="registry"/>-->

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--    <dubbo:registry address="zookeeper://localhost:2181" />-->
    <dubbo:registry address="N/A"/>

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service
            interface="org.example.IGpHello"
            ref="iGpHello"/>

    <!--Bean bean定义-->
    <bean id="iGpHello" class="org.example.IGpHelloImpl"/>

</beans>

// 启动类
public class Bootstrap {

    public static void main(String[] args) throws IOException {
        //加载xml配置文件启动
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

dubbo-client

在这里插入图片描述

  1. 代码实现
// 引入依赖
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
    </dependency>

    <dependency>
      <groupId>org.example</groupId>
      <artifactId>server-api</artifactId>
      <version>1.0-SNAPSHOT</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="dubbo-client" owner="jzx"/>
    <!--    <dubbo:monitor protocol="registry"/>-->

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--    <dubbo:registry address="zookeeper://localhost:2181" />-->
    <dubbo:registry address="N/A"/>

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口, 有注册中心无需配置url属性, 若无则必须配置-->
    <dubbo:reference id="gpHelloService"
            interface="org.example.IGpHello"
            url="dubbo://127.0.0.1:20880/org.example.IGpHello"/>

</beans>

// 启动类
import java.io.IOException;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        IGpHello iGpHello = (IGpHello) context.getBean("gpHelloService");
        String str = iGpHello.sayHello("lm");
        System.out.println(str);
        System.in.read();
    }
}

测试

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值