简介
本篇主要是基于dubbo2.5.3版本的远程服务调用的简单demo,包括服务端接口和实现以及客户端服务调用,简单的配置了注册中心。
服务端
创建过程
- 创建maven项目dubbo-server
- 创建module
a. server-api(服务接口)
b. server-provider(服务实现)
依赖
server-provider
<dependency>
<groupId>com.study</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
代码实现
server-api
package com.study;
/**
* @author future
* @date 2019-09-04 07:26
*/
public interface IHello {
String sayHello(String msg);
}
server-provider
package com.study;
/**
* @author future
* @date 2019-09-04 07:28
*/
public class HelloImpl implements IHello {
@Override
public String sayHello(String msg) {
return "Hello " + msg;
}
}
配置文件
dubbo-server.xml(整合spring)
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--应用名-->
<dubbo:application name="dubbo-server" owner="future"/>
<!--协议-->
<dubbo:protocol name="dubbo"/>
<!--服务接口-->
<dubbo:service interface="com.study.IHello" ref="helloService"/>
<!--注册中心-->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!--服务实现-->
<bean id="helloService" name="hello" class="com.study.HelloImpl"/>
</beans>
配置文件位置
加载配置文件
package com.study;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @author future
* @date 2019-09-04 22:41
*/
public class BootStrap {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server.xml");
context.start();
System.in.read();
}
}
客户端
创建
- 创建dubbo-client工程
maven依赖
<dependency>
<groupId>com.study</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
服务调用
package com.study;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-client.xml");
// context.start();
IHello hello = (IHello) context.getBean("helloService");
System.out.println( hello.sayHello("World") );
}
}
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--应用名-->
<dubbo:application name="dubbo-client" owner="future"/>
<!--协议-->
<dubbo:protocol name="dubbo"/>
<!--注册中心-->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!---->
<!--<dubbo:reference interface="com.study.IHello" id="helloService" url="dubbo://192.168.3.32:20880/com.study.IHello"/>-->
<dubbo:reference interface="com.study.IHello" id="helloService"/>
</beans>
客户端启动报错
警告: [DUBBO] Ignore empty notify urls for subscribe url consumer://192.168.3.32/com.study.IHello?application=dubbo-client&category=providers,configurators,routers&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer×tamp=1567783039716, dubbo version: 2.5.3, current host: 192.168.3.32
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.study.IHello. No provider available for the service com.study.IHello from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=dubbo-client&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer×tamp=1567783039716 to the consumer 192.168.3.32 use dubbo version 2.5.3
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:127)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:116)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:91)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1288)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:217)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
at com.study.App.main(App.java:15)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.study.IHello. No provider available for the service com.study.IHello from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=dubbo-client&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer×tamp=1567783039716 to the consumer 192.168.3.32 use dubbo version 2.5.3
at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)
at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)
at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)
at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:121)
... 9 more
<dubbo:reference interface="com.study.IHello" id="helloService" url="dubbo://192.168.3.32:20880/com.study.IHello"/>
- 改为使用Zookeeper作为注册中心
<dubbo:registry address="zookeeper://192.168.3.111:2181"/>
Zookeeper依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>