Dobbo --- HelloWorld项目搭建

Dobbo-HelloWorld

  • 1. demo -- spring方式集成
    • 1.1 实现步骤
  • 2. demo -- springboot方式集成
    • 2.1 实现provider
    • 2.2 实现consumer
    • 2.3 项目测试

1. demo – spring方式集成

dubbo官方文档

提供一个可被调用的接口
提供方:实现接口的方法逻辑,启动应用程序,接收消费方的调用
消费方:确认要调用的接口,找到注册中心,调用提供方,获取接口的返回结果

示例文档参考:https://github.com/apache/dubbo

1.1 实现步骤

Dobbo --- HelloWorld项目搭建_第1张图片

图1-2 提前展示项目文件

  1. 启动zookeeper

Dobbo --- HelloWorld项目搭建_第2张图片

图1-1启动zoopeeper

  1. 引入相关依赖
    
    <properties>
        <dubbo.version>3.2.5dubbo.version>
    properties>
	<dependencies>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
            <version>${dubbo.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-dependencies-zookeeperartifactId>
            <version>${dubbo.version}version>
            <type>pomtype>
        dependency>
    dependencies>
  1. 声明接口及其实现类

声明GreetingsService 接口

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

GreetingsService 具体实现类

import com.jyl.api.GreetingsService;

public class GreetingsServiceImpl implements GreetingsService {

    public String sayHi(String name) {
        return "hi, " + name;
    }
}
  1. 创建提供方和消费方的应用程序

服务端Application


import com.jyl.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import java.util.concurrent.CountDownLatch;

public class Application {
    // 先获取zookeeper部署的ip地址
    private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");

    public static void main(String[] args) throws Exception {
        // 创建服务配置 服务对应的接口类GreetingsService 设置泛型
        ServiceConfig<GreetingsService> service = new ServiceConfig<GreetingsService>();
        // 设置应用程序的名称
        service.setApplication(new ApplicationConfig("first-dubbo-provider"));
        // 设置注册中心 指定zookeeper的连接地址
        service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
        // 设置提供的接口
        service.setInterface(GreetingsService.class);
        // 设置提供具体的实现类
        service.setRef(new GreetingsServiceImpl());
        // 设置完成 执行导出命令
        service.export();

        System.out.println("dubbo service started");
        // 保证线程一直挂起
        new CountDownLatch(1).await();
    }
}

客户端Application


import com.jyl.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;


public class Application {
    // 先获取zookeeper部署的ip地址
    private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");


    public static void main(String[] args) {
        // 创建客户配置 服务对应的接口类GreetingsService 设置泛型
        ReferenceConfig<GreetingsService> reference = new ReferenceConfig<GreetingsService>();
        // 设置应用程序的名称
        reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        // 设置注册中心 指定zookeeper的连接地址
        reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
        // 设置提供的接口
        reference.setInterface(GreetingsService.class);
        // 获取到远程的 接口调用
        GreetingsService service = reference.get();
        String message = service.sayHi("dubbo");
        System.out.println(message);
    }
}
  1. 启动服务端Application 和 客户端 Application

Dobbo --- HelloWorld项目搭建_第3张图片

图1-3 运行Server/Client

2. demo – springboot方式集成

示例文档参考地址:https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples

2.1 实现provider

Dobbo --- HelloWorld项目搭建_第4张图片

图2-1 提前展示provider项目创建的文件

  1. 启动zookeeper
  2. 创建dubbo-demo-provider SpringBoot项目
  3. 引入依赖(dubbo-springboot、 dubbo-zookeeper、要使用的api的依赖)
   <properties>
        <dubbo.version>2.7.7dubbo.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>


        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>${dubbo.version}version>
        dependency>

        <dependency>
            <groupId>com.jylgroupId>
            <artifactId>dubbo-demo-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-dependencies-zookeeperartifactId>
            <version>${dubbo.version}version>
            <type>pomtype>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4jgroupId>
                    <artifactId>slf4j-log4j12artifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>
  1. 实现provider, 实现接口逻辑,增加配置参数
// 说明这个类是作为dubbo的服务被调用的
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello," + name;
    }
}
  1. application.properties配置文件

server.port=8090
# 配置dubbo涉及的相关参数

spring.application.name=dubbo-demo-provider

# 配置dubbo服务的扫描路径
dubbo.scan.base-packages=com.jyl.provider.service

# 协议名称
dubbo.protocol.name=dubbo
dubbo.protocol.port=23456

# dubbo注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181


2.2 实现consumer

Dobbo --- HelloWorld项目搭建_第5张图片

图2-2 提前展示consumer项目创建的文件
  1. 创建dubbo-demo-consumer SpringBoot项目

  2. 引入依赖 和 dubbo-demo-provider项目依赖一致

  3. 实现consumer,注意配置类和主程序入口


@SpringBootApplication
public class DubboDemoConsumerApplication {

    //对于调用端
    @DubboReference(version = "1.0.0")
    DemoService demoService;

    public static void main(String[] args) {
        SpringApplication.run(DubboDemoConsumerApplication.class,args);
    }

    @Bean
    public ApplicationRunner runner(){

        return args -> System.out.println(demoService.sayHello("dubbo-spring-boot xxx"));
    }
}
  1. application.properties
# 设置端口 避免端口号占用
server.port=8899

spring.application.name=dubbo-demo-consumer

# dubbo注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181

2.3 项目测试

先启动demo-provider ,后启动 demo-consumer

Dobbo --- HelloWorld项目搭建_第6张图片

图2-4 项目测试结果

寄语【自勉】:想要成功,必须甩掉所有懒惰的借口,起不来、跑不动、戒不掉……成功的路上只需要我可以,一定行!拼搏的滋味最甜美!新的一天,为梦想全力以赴!

希望可以帮助到@你

你可能感兴趣的:(dubbo,dubbo,java,zookeeper)