如何在本地搭建一个简单 dubbo 项目

本贴最后更新于 787 天前,其中的信息可能已经时移世易

如何在本地搭建一个简单dubbo项目

  1. zookooper下载
官网:

Apache ZooKeeper

下载镜像

Index of /apache/zookeeper

下载之后解压到无中文无特殊字符路径中:

image.png

  1. 修改conf/zoo_sample.cfg 为 zoo.cfg

image.png

zoo.cfg内容如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\09tmp\\zookeeper
# the port at which the clients will connect
clientPort=2181
  1. 启动zookeeper

双击bin/zkServer.cmd

image.png

  1. dubbo服务提供者代码编写,启动类、接口类、接口实现类。
//启动类
public class Application {
    public static void main(String[] args) throws Exception {
        ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
        service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
        service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.export();
        System.in.read();
    }
}
//服务接口类
public interface DemoService {

    String sayHello(String name);
  
    String show();

    String getName(String name);

}
//接口实现类
public class DemoServiceImpl implements DemoService {

    @Override  
    public String sayHello(String name) {
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }
  
    @Override  
    public String show() {
        return "show , response from provider: " + RpcContext.getContext().getLocalAddress();
    }
  
    @Override  
    public String getName(String name) {
        return  name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

}
  1. 安装dubbo-admin.war,查看dubbo接口信息。

准备好dubbo-admin.war之后放到tomcat/webapp中

image.png

  1. 启动tomcat,输入tomcat对应端(本案例是7070,需要自己设定)口加上dubbo-admin-2.6.0项目名

image.png

输入服务名称点击搜索按钮

image.png

点击IP地址查看详细信息

image.png

OK,这样咱们一个简单的dubbo服务提供者的环境就搭建完毕了。

1 操作
luojie 在 2020-08-06 17:48:04 更新了该帖
回帖
请输入回帖内容 ...