Dubbo配置
Dubbo配置可以使用XML、Properties、API、Annotation。

XML配置
Provider
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<bean id="helloService" class="top.guoj.provider.HelloServiceImpl" />
<dubbo:service interface="top.guoj.api.HelloService" timeout="5000"
ref="helloService" />
</beans>
Consumer
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<dubbo:reference id="helloService" check="false" timeout="1500" retries="1"
interface="top.guoj.api.HelloService" />
</beans>
Properties配置
API配置
Provider
HelloService helloService = new HelloServiceImpl();
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-provider");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("multicast://224.5.6.7:1234");
ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
service.setApplication(application);
service.setRegistry(registry);
service.setInterface(HelloService.class);
service.setRef(helloService);
//service.setVersion("1.0.0");
service.export();
System.out.println("Provider started.");
System.in.read();
Consumer
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-consumer");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("multicast://224.5.6.7:1234");
ReferenceConfig<HelloService> reference = new ReferenceConfig<>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(HelloService.class);
//reference.setVersion("1.0.0");
HelloService demoService = reference.get();
System.out.println(demoService.sayHello("world"));
provider与consumer的设置要保持一致。比如 provider设置了setVersion,consumer也需要同时设置,否则抛出异常:
Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method sayHello in the service top.guoj.api.HelloService. Tried 3 times of the providers [192.168.100.61:20880] (1/1) from the registry 224.5.6.7:1234 on the consumer 192.168.100.61 using the dubbo version 2.6.2. Last error is: Failed to invoke remote method: sayHello, provider: dubbo://192.168.100.61:20880/top.guoj.api.HelloService?anyhost=true&application=dubbo-consumer&check=false&dubbo=2.6.2&generic=false&interface=top.guoj.api.HelloService&methods=sayHello&pid=9072®ister.ip=192.168.100.61&remote.timestamp=1533266073861&side=consumer×tamp=1533266624708, cause: com.alibaba.dubbo.remoting.RemotingException: Not found exported service: top.guoj.api.HelloService:20880 in [top.guoj.api.HelloService:1.0.0:20880], may be version or group mismatch , channel: consumer: /192.168.100.61:53628 --> provider: /192.168.100.61:20880, message:RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[world], attachments={dubbo=2.6.2, input=171, path=top.guoj.api.HelloService, interface=top.guoj.api.HelloService, version=0.0.0}]
com.alibaba.dubbo.remoting.RemotingException: Not found exported service: top.guoj.api.HelloService:20880 in [top.guoj.api.HelloService:1.0.0:20880], may be version or group mismatch , channel: consumer: /192.168.100.61:53628 --> provider: /192.168.100.61:20880, message:RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[world], attachments={dubbo=2.6.2, input=171, path=top.guoj.api.HelloService, interface=top.guoj.api.HelloService, version=0.0.0}]
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.getInvoker(DubboProtocol.java:212)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:78)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:172)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:80)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:102)
at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:238)
at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:75)
at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
at com.alibaba.dubbo.common.bytecode.proxy0.sayHello(proxy0.java)
at top.guoj.consumer.Consumer.main(Consumer.java:37)
Caused by: com.alibaba.dubbo.remoting.RemotingException: com.alibaba.dubbo.remoting.RemotingException: Not found exported service: top.guoj.api.HelloService:20880 in [top.guoj.api.HelloService:1.0.0:20880], may be version or group mismatch , channel: consumer: /192.168.100.61:53628 --> provider: /192.168.100.61:20880, message:RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[world], attachments={dubbo=2.6.2, input=171, path=top.guoj.api.HelloService, interface=top.guoj.api.HelloService, version=0.0.0}]
com.alibaba.dubbo.remoting.RemotingException: Not found exported service: top.guoj.api.HelloService:20880 in [top.guoj.api.HelloService:1.0.0:20880], may be version or group mismatch , channel: consumer: /192.168.100.61:53628 --> provider: /192.168.100.61:20880, message:RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[world], attachments={dubbo=2.6.2, input=171, path=top.guoj.api.HelloService, interface=top.guoj.api.HelloService, version=0.0.0}]
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.getInvoker(DubboProtocol.java:212)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:78)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:172)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:80)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:222)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:139)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:112)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:95)
at com.alibaba.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:148)
at com.alibaba.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:77)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:54)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:48)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:78)
... 5 more