Dubbo可能实现在每一个Java程序员都绕不过去的一个框架了,目前我面试过的所有公司,不管用不用多少都会被问到该框架的技术问题。
本在2018年加入了Apache之后,工程很多方面都出现了变化;Dubbo引入路径和dubbo-admin工程都放生了变化;
如果不想冒险尝试最新的dubbo ops的话,可以尝试下载 https://github.com/dangdangdotcom/dubbox.git 版本,感觉还不错;
这里我们主要学习Dubbo OPS 安装步骤:
下载代码:
GIT地址:https://github.com/apache/incubator-dubbo-ops.git
分支选择:develop
clone 下来发现有两个工程
这里dubbo将前后台分开:
前端: dubbo-admin-frontend
后端: dubbo-admin-backend
启动前端:
前端项目需要nodejs,如果安装不会的朋友,可以去看另一篇博客:https://blog.csdn.net/keenstyle/article/details/86577297
万事OK后,cmd命令行切换到dubbo-admin-frontend路径下;
运行命令:npm install
该命令将下载所有前端项目以来的包;(这个可能等待时间稍微长一点,如果长时间不懂,建议回车一下;不知道怎么回事,但是亲测有效。感觉贱贱的!)
修改端口:
C:\openSource\code\incudator-dubbo-ops\dubbo-admin-frontend\config\index.js
找到port项,默认的端口为8081,建议还是修改,这个端口默认的地方太多了。
我们在这里修改为:7001
port: 7001
在找到proxyTable属性修改为和端口同样:
然后在dubbo-admin-frontend项目根目录下运行:npm run dev 或者 npm start 启动项目,启动后通过http://localhost:7001/访问前台页面;
启动后端:(注意:启动后端之前一定要启动zookeeper)
后端项目是通过spring boot来实现,我们开始下面的启动步骤:
倘若没有导入eclipse工具,直接进入dubbo-admin-frontend根目录进入cmd命令行;
执行命令:mvn clean package
注:长时间发现不动了就敲一下回车(这玩意儿感觉贱贱)
这里有点小问题需要注意,自己在这里耽误了一天的时间;
1、一定要先启动zookeeper;
2、我们需要把前面的前端项目先关掉,不然会报已经被绑定的错误;
如果打包途中报KeeperErrorCode = ConnectionLoss错误信息,就需要我们处理一下;
我们需要加上一个java文件在我们的后端项目中:Conf
package org.apache.dubbo.admin;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.data.Stat;
public class Conf {
public static void waitUntilConnected(ZooKeeper zooKeeper, CountDownLatch connectedLatch) {
if (States.CONNECTING == zooKeeper.getState()) {
try {
connectedLatch.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
static class ConnectedWatcher implements Watcher {
private CountDownLatch connectedLatch;
ConnectedWatcher(CountDownLatch connectedLatch) {
this.connectedLatch = connectedLatch;
}
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedLatch.countDown();
}
}
}
static public Conf Instance() {
if (static_ == null) {
static_ = new Conf();
}
return static_;
}
public boolean Init(String hostports, int times) {
try {
CountDownLatch connectedLatch = new CountDownLatch(1);
Watcher watcher = new ConnectedWatcher(connectedLatch);
zk_ = new ZooKeeper(hostports, times, watcher);
waitUntilConnected(zk_, connectedLatch);
} catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
public String Get(String keys) {
String re = "";
String ppath = "/zookeeper";
int oldpos = -1;
int pos = 0;
while (true) {
pos = keys.indexOf(".", oldpos + 1);
if (pos < 0) {
ppath += "/";
String str = keys.substring(oldpos + 1);
ppath += str;
break;
}
ppath += "/";
String str = keys.substring(oldpos + 1, pos);
ppath += str;
oldpos = pos;
}
Stat stat = new Stat();
try {
byte[] b = zk_.getData(ppath, false, stat); // 获取节点的信息及存储的数据
re = new String(b);
} catch (Exception e) {
System.out.println(e);
}
return re;
}
private Conf() {
}
private ZooKeeper zk_;
static private Conf static_;
public static void main(String args[]) {
String hostports = "127.0.0.1:2181";
Conf.Instance().Init(hostports, 1000);
String str = Conf.Instance().Get("conf.logicpoint.subscriberserverip");
str = Conf.Instance().Get("conf.logicpoint.subscriberserverport");
System.out.println(str);
while (true) {
try {
// System.out.println("*************************" + str);
Thread.sleep(100);
} catch (Exception e) {
}
}
}
}
自己是在eclipse上面直接启动的,启动结果:
目前就安装好了,前后端都启动,就能访问了!