Dubbo不用注册中心实现远程调用的简单用法demo
服务端:
先在服务端写接口以及接口实现类:
package com.gupao.dubbo;
public interface GPDubboService {
public String sayHello(String msg);
}
package com.gupao.dubbo;
public class GPDubboServiceImpl implements GPDubboService{
@Override
public String sayHello(String msg) {
return "Hello ,liaoyang make you see " + msg;
}
}
2 xml文件中注册服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-server" owner="liaoyy"/>
--表示不用注册中心
<dubbo:registry address="N/A"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.gupao.dubbo.GPDubboService" ref="gphelloservice"/>
<bean id="gphelloservice" class="com.gupao.dubbo.GPDubboServiceImpl"/>
</beans>
运行代码发布服务
public class BootStrap {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new
ClassPathXmlApplicationContext("dubbo-server.xml");
classPathXmlApplicationContext.start();
System.in.read();
}
}
消费端:
先把服务方依赖过来
<dependency>
<groupId>com.gupao.dubbo</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
由于没用注册中心,肯定要指定请求地址的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-client" owner="liaoyy"/>
<dubbo:registry address="N/A"/>
<dubbo:protocol name="dubbo" port="20880"/>
-- id为服务方bean的id interfacce为服务方发布的接口路径
<dubbo:reference id="gphelloservice" interface="com.gupao.dubbo.GPDubboService" url="dubbo://169.254.105.124:20880/com.gupao.dubbo.GPDubboService"/>
</beans>
然后调用就完事儿
public class BootStrap {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new
ClassPathXmlApplicationContext("dubbo-client.xml");
classPathXmlApplicationContext.start();
GPDubboService gphelloservice
= (GPDubboService) classPathXmlApplicationContext.getBean("gphelloservice");
System.out.println(gphelloservice.sayHello("liaoyyyyy"));
}
}
运行结果
七月 05, 2020 4:15:40 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6267c3bb: startup date [Sun Jul 05 16:15:40 CST 2020]; root of context hierarchy
七月 05, 2020 4:15:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [dubbo-client.xml]
七月 05, 2020 4:15:40 下午 com.alibaba.dubbo.common.logger.LoggerFactory info
信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
七月 05, 2020 4:15:41 下午 com.alibaba.dubbo.remoting.transport.AbstractClient info
信息: [DUBBO] Successed connect to server /169.254.105.124:20880 from NettyClient 169.254.105.124 using dubbo version 2.5.6, channel is NettyChannel [channel=[id: 0x0c33b74f, /169.254.105.124:59010 => /169.254.105.124:20880]], dubbo version: 2.5.6, current host: 169.254.105.124
七月 05, 2020 4:15:41 下午 com.alibaba.dubbo.remoting.transport.AbstractClient info
信息: [DUBBO] Start NettyClient DESKTOP-TLTOEDB/169.254.105.124 connect to the server /169.254.105.124:20880, dubbo version: 2.5.6, current host: 169.254.105.124
七月 05, 2020 4:15:42 下午 com.alibaba.dubbo.config.AbstractConfig info
信息: [DUBBO] Refer dubbo service com.gupao.dubbo.GPDubboService from url dubbo://169.254.105.124:20880/com.gupao.dubbo.GPDubboService?application=dubbo-client&dubbo=2.5.6&interface=com.gupao.dubbo.GPDubboService&methods=sayHello&owner=liaoyy&pid=29396&revision=1.0-SNAPSHOT&side=consumer×tamp=1593936941363, dubbo version: 2.5.6, current host: 169.254.105.124
Hello ,liaaoyang make you see liaoyyyyy
七月 05, 2020 4:15:42 下午 com.alibaba.dubbo.config.AbstractConfig info
信息: [DUBBO] Run shutdown hook now., dubbo version: 2.5.6, current host: 169.254.105.124
Process finished with exit code 0

浙公网安备 33010602011771号