dubbo入门
2019-03-13 14:53 雄风狂飙 阅读(150) 评论(0) 收藏 举报任何一个技术都是从hello world开始,据说。
1.1. 下载代码
git clone https://github.com/apache/incubator-dubbo.git --depth 1
1.2. 下载zookeeper并启动
过程略
假设启动端口为:2181
1.3. 启动provider
1. provoider代码结构
2. 代码
dubbo-demo\dubbo-demo-interface\src\main\java\org\apache\dubbo\demo\DemoService.java
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
dubbo-demo\dubbo-demo-xml\dubbo-demo-xml-provider\src\main\java\org\apache\dubbo\demo\provider\DemoServiceImpl.java
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
@Override
public String sayHello(String name) {
logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
dubbo-demo\dubbo-demo-xml\dubbo-demo-xml-provider\src\main\java\org\apache\dubbo\demo\provider\Application.java
package org.apache.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
/**
* In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
* launch the application
*/
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
System.in.read();
}
}
配置
dubbo-demo\dubbo-demo-xml\dubbo-demo-xml-provider\src\main\resources\spring\dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>
地址要换成自己的zookeeper的地址。
1.4. 启动consumer
代码
dubbo-demo\dubbo-demo-xml\dubbo-demo-xml-consumer\src\main\java\org\apache\dubbo\demo\consumer\Application.java
package org.apache.dubbo.demo.consumer;
import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static java.lang.Thread.sleep;
public class Application {
/**
* In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
* launch the application
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
while(true) {
String hello = demoService.sayHello("world");
System.out.println("result: " + hello);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
配置
dubbo-demo\dubbo-demo-xml\dubbo-demo-xml-consumer\src\main\resources\spring\dubbo-consumer.xml
<?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="demo-consumer"/>
<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>
地址必要要修改为自己的zookeeper地址。
启动provider后,再启动consumer,就可以看到如下打印信息了:
result: Hello world, response from provider: 192.168.123.119:20880
result: Hello world, response from provider: 192.168.123.119:20880
1.5. 踩过的坑
错误:
下载示例代码报错:
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
解决:
在git clone 后面增加 --depth 1
>git clone https://github.com/apache/incubator-dubbo.git --depth 1
浙公网安备 33010602011771号