查看dubbo端口(如何修改并获得dubbo服务的端口号)
本文目录
- 如何修改并获得dubbo服务的端口号
- zk客户端命令查看dubbo服务的生产者和消费者
- dubbo是如何启动的
- dubbo服务器线程使用情况查看
- dubbo注册zk后是内网地址问题
- linux查看dubbo注册哪些服务
如何修改并获得dubbo服务的端口号
package com.github.doctor.dubbo.config;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostC***truct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.config.ProtocolConfig;
/**
* 这是一个进程启动多个dubbo服务的DubboPortHandler另一种写法。以前的方案见DubboNamespaceHandlerEx
* 利用spring修改受spring管理bean的属性信息
*
* @author doctor
*
* @time 2014年12月31日 上午9:43:52
*/
@Component
public class DubboPortHandler2 {
@Autowired
private ApplicationContext applicationContext;
private int port = 20080;
@PostC***truct
public void init() {
Map《String, ProtocolConfig》 beansOfType = applicationContext.getBeansOfType(ProtocolConfig.class);
for (Entry《String, ProtocolConfig》 item : beansOfType.entrySet()) {
port = NetUtils.getAvailablePort();
item.getValue().setPort(port);
}
}
public int getPort() {
return port;
}
}
zk客户端命令查看dubbo服务的生产者和消费者
假设zookeeper安装在172.16.22.2这台服务器上,现在我们通过命令行查看dubbo在zookeeper注册服务的生产者和消费者信息
首先通过命令切换到/zookeeper-3.4.11/bin目录,然后输入
(2182为zookeeper在服务器上提供服务的端口)会看到如下截图:
然后在命令行再输入:
查看目录信息,就能看到注册的dubbo服务,截图如下:
在命令行依次输入:
会看到dubbo服务提供的对外接口,截图如下:
查看消费者命令:
会看到消费者的信息,截图如下:
查看生产者命令:
会看到生产者的信息,截图如下:
dubbo是如何启动的
已知,在项目启动过程中,我们会将dubbo的配置文件写到spring的配置文件里,如下xml文件:
《dubbo:application name="anyname_provider" /》
《!-- 使用zookeeper注册中心暴露服务地址 --》
《dubbo:registry address="zookeeper://127.0.0.1:2181" /》
《!-- 用dubbo协议在20880端口暴露服务 --》
《dubbo:protocol name="dubbo" port="20880" /》
《!-- 声明需要暴露的服务接口 --》
《dubbo:service inte***ce="com.shxz130.provider.Provider"
ref="demoService" /》
从官方文档中,我们能看到如下:
启动过程.png
也就是说spring启动过程中,随着Spring在初始化过程中,碰到dubbo命名的标签,如(《dubbo:service》,《dubbo:registry》)等标签,会由DubboNamespaceHandler类处理,具体原理见链接Spring自定义标签
DubboBeanDefinitionParser代码如下:
public class DubboNamespaceHandler extends NamespaceHandlerSupport { static {
Version.checkDuplicate(DubboNamespaceHandler.class);
} public void init() {
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
registerBeanDefinitionParser("c***umer", new DubboBeanDefinitionParser(C***umerConfig.class, true));
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
}
}
遇到不同的标签,会由不同的Parser处理,这里重点看服务发布,这行代码:
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
也就是说,当Spring容器处理完《dubbo:service》标签后,会在Spring容器中生成一个ServiceBean ,服务的发布也会在ServiceBean中完成。不妨看一下ServiceBean的定义:
public class ServiceBean《T》 extends ServiceConfig《T》 implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener《ContextRefreshedEvent》, BeanNameAware {
}
该Bean实现了很多接口,关于InitializingBean,DisposableBean,ApplicationContextAware,BeanNameAware,这些接口的使用介绍如下链接:
InitializingBean&DisposableBean
BeanNameAware& ApplicationContextAware
- public class ServiceBean《T》 extends ServiceConfig《T》 implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener《ContextRefreshedEvent》, BeanNameAware { //Spring容器初始化完成,调用
- public void onApplicationEvent(ContextRefreshedEvent event) { if (isDelay() && !isExported() && !isUnexported()) { if (logger.isInfoEnabled()) {
- logger.info("The service ready on spring started. service: " + getInte***ce());
- } //暴露服务
- export();
- }
- } //判断是否延迟发布
- private boolean isDelay() {
- Integer delay = getDelay();
- ProviderConfig provider = getProvider(); if (delay == null && provider != null) {
- delay = provider.getDelay();
- } return supportedApplicationListener && (delay == null || delay == -1);
- } //当bean初始化完成调用
- public void afterPropertiesSet() throws Exception { //......此处省略10000行代码
- if (!isDelay()) { //暴露服务
- export();
- }
- }
- }
- public synchronized void export() { //忽略若干行代码
- if (delay != null && delay 》 0) { //当delay不为null,且大于0时,延迟delay时间,暴露服务
- delayExportExecutor.schedule(new Runnable() { public void run() { //暴露服务
- doExport();
- }
- }, delay, TimeUnit.MILLISECONDS);
- } else { //直接暴露服务
- doExport();
- }
- }
- protected synchronized void doExport() { //忽略10000行代码
- doExportUrls(); //忽略10000行代码
- } private void doExportUrls() {
- List《URL》 registryURLs = loadRegistries(true); for (ProtocolConfig protocolConfig : protocols) { //按照不同的Protocal暴露服务
- doExportUrlsFor1Protocol(protocolConfig, registryURLs);
- }
- }
而在Spring初始化完成Bean的组装,会调用InitializingBean的afterPropertiesSet方法,在Spring容器加载完成,会接收到事件ContextRefreshedEvent,调用ApplicationListener的onApplicationEvent方法。
在afterPropertiesSet中,和onApplicationEvent中,会调用export(),在export()中,会暴露dubbo服务,具体区别在于是否配置了delay属性,是否延迟暴露,如果delay不为null,或者不为-1时,会在afterPropertiesSet中调用export()暴露dubbo服务,如果为null,或者为-1时,会在Spring容器初始化完成,接收到ContextRefreshedEvent事件,调用onApplicationEvent,暴露dubbo服务。
部分ServiceBean的代码如下:
在export(),暴露服务过程中,如果发现有delay属性,则延迟delay时间,暴露服务,如果没有,则直接暴露服务。
而在doExport()中,验证参数,按照不同的Protocol,比如(dubbo,injvm)暴露服务,在不同的zookeeper集群节点上注册自己的服务。
作者:一滴水的坚持
***隐藏网址***
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
dubbo服务器线程使用情况查看
在dubbo服务器上,执行telnet可进入dubbo命令控制行:
点击回车,进入dubbo控制台
ls
显示服务列表。
ls -l
显示服务详细信息列表。
ls XxxService
显示服务的方法列表。
ls -l XxxService
显示服务的方法详细信息列表。
ps
显示服务端口列表。
ps -l
显示服务地址列表。
ps 20880
显示端口上的连接信息。
ps -l 20880
显示端口上的连接详细信息。
trace XxxService
跟踪1次服务任意方法的调用情况。
trace XxxService 10
跟踪10次服务任意方法的调用情况。
trace XxxService xxxMethod
跟踪1次服务方法的调用情况
trace XxxService xxxMethod 10
跟踪10次服务方法的调用情况。
dubbo注册zk后是内网地址问题
1、查看当前hostname
hostname
2、修改/etc/hosts文件
vim /etc/hosts
将hostname对应的ip地址改为外网地址
3、重启jar包即可
【注】修改/etc/hosts文件 记得将localhost新增为127.0.0.1 本地ip地址,防止tomcat启动不起来(org.apache.catalina.core.StandardServer.await 无法在地址)java.net.BindException: 无法指定被请求的地址 (Bind failed))
linux查看dubbo注册哪些服务
直连加不发布服务
DUBBO的配置属性里面对消费端提供了不从注册中心发现服务的机制,直接配置远程接口的地址,这样可以保证消费端连接到制定的环境接口。这样消费端是解决了问题,但是服务提供端呢?如上图的B1它即是消费端也是服务提供端,它提供A1所依赖的接口,那么如果B1将它的服务发布到注册中心里面(这里需要提醒,STABLE环境机制里面所有子环境公用一个注册中心),那么势必会导致stable环境里面的A会发现B1提供的服务?势必会导致stable环境的不稳定(stable环境的机制是stable环境只能进不能出,就是不能调用外部其他子环境的服务)?所以B1不能发布服务到注册中心,dubbo也提供了相关的配置属性来支持这一点。下面我例举出通过哪些配置可以实现这种方案:
服务消费端:
DUBBO在消费端提供了一个url的属性来指定某个服务端的地址
《!--lang:xml--》
《dubbo:reference inte***ce="com.alibaba.dubbo.demo.HelloWorldService" check="false" id="helloWorldService"/》
默认的方式是从注册中心发现接口为com.alibaba.dubbo.demo.HelloWorldService的服务,但是如果需要直连,可以在dubbo.properties下面配置dubbo.reference.helloWorldService.url=dubbo://ip:port/com.alibaba.dubbo.demo.HelloWorldService可以通过配置dubbo.reference.url=dubbo://ip:port/来让某个消费者系统的服务都指向制定的服务器地址(关于配置信息可以参考《DUBBO配置规则详解》)
更多文章:
php中session的用法(十万火急,关于用PHP写留言板时登录权限的SESSION用法,新手不会啊)
2026年4月9日 09:00
excel median函数(excel中,几个数中取中间数的函数是什么啊)
2026年4月9日 08:40
codebase(谁能详细解释一下所谓codebase方式谢谢)
2026年4月9日 07:00
geant4安装(geant4为什么会生成3个root文件)
2026年4月9日 06:40






