本dubbo使用教程基于zookeeper-3.4.9搭建,包含服务提供者DubboProvider、和消费者DubboConsumer,可直接在Tomcat上运行。DubboProvider对外开放2个接口,DubboConsumer调用DubboProvider,并对外暴露HTTP服务。DubboProvider还提供了对数据库的操作,基于SpringJDBC并加入了RowMapper通用类,这点在下文《SpringJDBC之RowMapper通用类》详讲。
项目完整源码见文末链接,下载即可用。
网上现有教程大多是直接照搬官网,用main函数启动,未使用Tomcat启动,直接应用于企业开发较为困难。当然我也是参照官网,学习了前辈的经验,哈哈。
开发环境:Windows10、Eclipse、JDK8、Spring4.3.7、zookeeper-3.4.9。
package com.zxiaofan.dubboProvidder.service;
import com.zxiaofan.dubboProvidder.model.HelloBo;
/**
*
* @author xiaofan
*/
public interface IProviderService {
/**
* 测试字符串传输.
*
* @param name
* @return
*/
String helloBoy(String name);
/**
* 测试bean传输.
*
* @param bo
* @return
*/
HelloBo helloGirl(HelloBo bo);
}
接口IUserService略过,详见源码。
package com.zxiaofan.dubboProvidder.service.impl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.zxiaofan.dubboProvidder.model.HelloBo;
import com.zxiaofan.dubboProvidder.service.IProviderService;
/**
*
* @author xiaofan
*/
@Component("providerService")
public class ProviderServiceImpl implements IProviderService {
@Value("${param.url}")
private String url;
/**
* {@inheritDoc}.
*/
@Override
public String helloBoy(String name) {
String result = "hello " + name + ", This is dubboProvider[" + url + "]";
System.out.println(result);
return result;
}
/**
* {@inheritDoc}.
*/
@Override
public HelloBo helloGirl(HelloBo helloBo) {
if (null == helloBo.getName()) {
helloBo.setName("DefaultName");
}
helloBo.setUrl(helloBo.getName() + ":This is dubboProvider[csdn.zxiaofan.com]");
return helloBo;
}
}
结合配置文件dubbo.properties来看
# dubbo param
dubbo.applicationName=dubboProvider
dubbo.registryProtocol=zookeeper
dubbo.registryAddress=127.0.0.1:2181
dubbo.port=20880
#服务端超过serverTimeout未返回结果则抛500异常
dubbo.serverTimeout=120000
#客户端超过clientTimeout未获取到数据则抛500异常
dubbo.clientTimeout=120000
#dubbo调用失败,默认重试2次
dubbo.retries=0
/WEB-INF/config/param.properties
/WEB-INF/config/dubbo.properties
/WEB-INF/config/jdbc.properties
web.xml一定要引入app-context.xml以及设置监听ContextLoaderListener。
contextConfigLocation
classpath:/com/zxiaofan/config/spring/app-context.xml
org.springframework.web.context.ContextLoaderListener
package com.zxiaofan.dubboConsumer.service;
/**
*
* @author zxiaofan
*/
public interface IConsumerService {
String hi(String name);
}
package com.zxiaofan.dubboConsumer.service.impl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.zxiaofan.dubboConsumer.service.IConsumerService;
import com.zxiaofan.dubboProvidder.model.HelloBo;
import com.zxiaofan.dubboProvidder.service.IProviderService;
import com.zxiaofan.dubboProvidder.service.IUserService;
/**
*
* @author zxiaofan
*/
@Component("consumerService")
public class ConsumerServiceImpl implements IConsumerService {
@Resource(name = "providerService")
private IProviderService providerService;
@Resource(name = "userService")
private IUserService userService;
@Value("${param.url}")
private String url;
/**
* {@inheritDoc}.
*/
@Override
public String hi(String name) {
String result = null;
if (null != name && name.startsWith("boy")) {
System.out.println("Hi Boy!");
result = providerService.helloBoy(name.replace("boy", "~~~"));
} else if (null != name && name.startsWith("select")) {
System.out.println("hi select!");
result = userService.selectByID(name.replace("select", ""));
return result;
} else {
HelloBo helloBo = new HelloBo();
helloBo.setName(name);
HelloBo helloBoResult = null;
helloBoResult = providerService.helloGirl(helloBo);
if (null != helloBoResult) {
result = helloBoResult.getUrl();
}
}
result += "; This is dubboConsumer[" + url + "]";
return result;
}
}
总共有3个分支:
该配置文件配置了2个服务的调用关系:providerService(入参为单字符串接口、入参为model接口)、userService(基于SpringJDBC的数据库服务)。
contextConfigLocation
classpath:/com/zxiaofan/config/spring/app-context.xml
org.springframework.web.context.ContextLoaderListener
HttpServer
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:/com/zxiaofan/config/spring/httpServer-servlet.xml
1
HttpServer
/*
引入app-context.xml,监听ContextLoaderListener,注意加上servlet,方便暴露HTTP服务。
欢迎个人转载,但须在文章页面明显位置给出原文连接;
未经作者同意必须保留此段声明、不得随意修改原文、不得用于商业用途,否则保留追究法律责任的权利。
【 CSDN 】:csdn.zxiaofan.com
【GitHub】:github.zxiaofan.com
如有任何问题,欢迎留言。祝君好运!
Life is all about choices!
将来的你一定会感激现在拼命的自己!