Dubbo入门案例

1. 注册中心zookeeper

1.1 下载

apache-zookeeper-3.6.3-bin.tar.gz

1.2 解压&启动

windows直接双击bin/zkServer.cmd启动即可,系统一定要有Java环境。

2. 服务提供者Provider

2.1 pom依赖
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <!-- Spring的版本 -->
    <spring.version>4.3.2.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring单元测试 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring基础依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
    </dependency>

    <!-- zookeeper -->
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.6.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
    <dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.10</version>
    </dependency>
  </dependencies>
2.2 spring-dubbo.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://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"/>
    <!--配置注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--配置服务协议-->
    <dubbo:protocol name="dubbo" port="20890"/>
    <!--配置服务接口暴露-->
    <dubbo:service interface="cool.gjh.service.DemoService" ref="demoService"/>
    <!--配置服务实现类暴露-->
    <bean id="demoService" class="cool.gjh.service.impl.DemoServiceImpl"/>

</beans>

2.3 DemoService接口和实现类

接口:

package cool.gjh.service;

/**
 * Dubbo暴露的接口
 * @author ACE_GJH
 * @date 2021/5/17
 */
public interface DemoService {

    /**
     * 说你好
     * @param name 名字
     * @return
     */
    String hello(String name);

}

实现类:

package cool.gjh.service.impl;

import cool.gjh.service.DemoService;

/**
 * Dubbo暴露的接口实现类
 * @author ACE_GJH
 * @date 2021/5/17
 */
public class DemoServiceImpl implements DemoService {
    @Override
    public String hello(String name) {
        return "Hello, " + name;
    }
}
2.4 启动类
package cool.gjh;

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

/**
 * @author ACE_GJH
 * @date 2021/05/17
 */
public class DubboProvider
{
    public static void main( String[] args ) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo.xml");
        System.in.read();
    }
}

3. 服务订阅者Consumer

3.1 pom依赖

同服务提供者Provider一样

3.2 spring-dubbo.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://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-consumer"/>
    <!--配置注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--配置服务协议-->
    <dubbo:protocol name="dubbo" port="20890"/>
    <!--配置消费服务-->
    <dubbo:reference interface="cool.gjh.service.DemoService" id="demoService"/>

</beans>
3.3 DemoService接口

同服务提供者Provider一样

3.4 启动类
package cool.gjh;

import cool.gjh.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author ACE_GJH
 * @date 2021/05/17
 */
public class DubboConsumer
{
    public static void main( String[] args )
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo.xml");
        DemoService demoService = (DemoService)context.getBean("demoService");
        String result = demoService.hello("Dubbo");
        System.out.println(result);

    }
}
3.5 运行结果

结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

郭建華

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

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

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

打赏作者

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

抵扣说明:

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

余额充值