吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 774|回复: 2
收起左侧

[学习记录] dubbo学习记录(一)

[复制链接]
HK仅輝 发表于 2022-9-9 16:57
注意:本地要下载zookeeper
API接口
[Java] 纯文本查看 复制代码
public interface GreetingsService {
    String sayHi(String name);
}


API实现
[Java] 纯文本查看 复制代码
import com.api.GreetingsService;


public class GreetingsServiceImpl implements GreetingsService {
    @Override
    public String sayHi(String name) {
        return "hi, " + name;
    }
}


注册dubbo服务
[Java] 纯文本查看 复制代码
import com.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 {

    private static String zookeeperHost = System.getProperty("zookeeper.address","127.0.0.1");

    public static void main(String[] args) throws InterruptedException {

        ServiceConfig<GreetingsService> service = new ServiceConfig<>();

        service.setApplication(new ApplicationConfig("first-dubbo-provider"));

        service.setRegistry(new RegistryConfig("zookeeper://"+zookeeperHost+":2181"));

        service.setInterface(GreetingsService.class);

        service.setRef(new GreetingsServiceImpl());

        service.export();

        System.out.println("dubbo service start");

        new CountDownLatch(1).await();
    }
}


客户端
[Java] 纯文本查看 复制代码
import com.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;

public class Application {

    private static String zookeeperHost = System.getProperty("zookeeper.address","127.0.0.1");

    public static void main(String[] args) {

        ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();

        reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));

        reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));

        reference.setInterface(GreetingsService.class);

        GreetingsService service = reference.get();

        String message = service.sayHi("dubbo-Hi");
        System.out.println(message);

    }

}


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

小木hwy 发表于 2022-9-9 17:18
支持!!!!!!!!
头像被屏蔽
xiadongming 发表于 2022-9-10 11:50
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表