近来对java比较痴迷,研究了下dubbo的创建到发布这个生命周期,在java友人的帮助下搞定,特此记录下(主要看过程,具体的按照自己项目设置)
1、提供dubbo服务,我们需要做什么
一般来讲dubbo对外服务,都会放到api下,我们现在来创建一个api,
这样基本module创建好了,然后在该module下创建包
在根pom.xml下加
<module>bbs-api</module>
在api目录下的pom.xml中加上,来确定你要推的仓库
<distributionManagement>
<repository>
<id>bbs</id>
<name>bbs-group</name>
<url>http://mvn.bbs.inc:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>bbs</id>
<name>bbs-group</name>
<url>http://mvn.bbs.inc:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
这样我们的module就创建完了,如果找不到package,请多次刷新maven.
2、开发代码
在com.bbs.api下创建一个api目录,目录下创建一个SiteMessage.java代码如下(注意,这是一个接口类用来定义和对外暴露)
package com.bbs.api.api;
public interface SiteMessage {
/**
* 检查用户是否存在
*/
String isUserExists(String UserId);
}
一个接口继承类放在service下(一般都是这样放)
package com.bbs.service;
import com.bbs.api.api.SiteMessage;
public interface SiteMessageService extends SiteMessage {
String checkOtherUserId();
}
一个实现类
package com.bbs.service.impl;
import com.bbs.service.SiteMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SiteMessageImpl implements SiteMessageService{
@Override
public String checkOtherUserId(){
return "hello,world";
};
@Override
public String isUserExists(String userId){
return "yes,itis";
}
}
这时dubbo的功能开发完了,假设我们测试通过了。
3、dubbo的基本配置
一般在web目录下加上2个xml配置文件,不要问为什么,老师傅就这样教的哈哈。
producer.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="user_promotion-prize-producer"/>
<dubbo:registry protocol="zookeeper" address="${zk.pro.url.rw}" />
<!-- 用dubbo协议在端口暴露服务 -->
<dubbo:protocol name="dubbo" port="-1"/>
<!-- 下面这句很重要,为提供服务使用,ref为实例的id,ref是提供方接口实例bean的id-->
<dubbo:service interface="com.bbs.api.api.SiteMessage" ref="siteMessageImpl" version="1.0.0" />
</beans>
consumer.xml如果需要消费就加上这个
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="bbs-web"/>
<dubbo:registry protocol="zookeeper" address="${zookeeper.servers}" register="true"/>
<!-- 用dubbo协议在端口暴露服务 -->
<dubbo:protocol name="dubbo" port="-1"/>
<dubbo:consumer timeout="60000"/>
<dubbo:reference id="SendService" interface="com.bbs.api.api.SiteMessage" version="1.0.0" check="false"/>
</beans>
在spring-parent.xml中加上这两个
<import resource="dubbo-producer.xml">
<import resource="dubbo-consumer.xml">
4、上传到你指定的仓库
点右边maven中
如果成功就完成了dubbo的提供。