做一个简单的功能,单个商品查询服务。
一、前提
必须是已经安装了zookeeper这个注册中心才能发布服务。
二、功能实现
mannger-interface中加入这个方法
public interface ItemService {
public TbItem getItemById(Long itemId);
}
mannger-service中实现
public class ItemServiceImpl implements ItemService {
@Autowired
private TbItemMapper itemMapper;
@Override
public TbItem getItemById(Long itemId) {
TbItem item = itemMapper.selectByPrimaryKey(itemId);
return item;
}
}
项目中引入dubbo
在mannger-service的applicationConfig.xml文件中加入:
<!-- 发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-mannger"/>
<!-- 这里使用127.0.0.1就不行,后来又好了,因为没有复现所以具体可能是我地址写错了 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!-- 可以是首字母小写 -->
<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" timeout="3000"></dubbo:service>
在mannger-web的springmvc.xml加入:
<!-- 引用dubbo服务 -->
<dubbo:application name="taotao-mannger-web"/>
<dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
<dubbo:reference interface="com.taotao.service.ItemService" id="itemServiceImpl"></dubbo:reference>
解决dubbo传递依赖导致Spring 版本不一致问题:
右键execulde
mannger的maven进行插件启动配置
启动顺序:zookeeper、mannger聚合工程、mannger-web工程
三、项目启动遇见的错误
问题一、Child module /home/lxj/eclipse-workspace/taotao-mannger/taotao-mannger-service2 of /home/lxj/eclipse-workspace/taotao-mannger/pom.xml does not exist
这个错误是说mannger聚合工程下有一个叫service2的mudle没有pom.xml文件,最开始的时候,我很是懵比,我压根没有这个mudle啊,然后去mannger的pom.xml查看发现:
<modules>
<module>taotao-mannger-dao</module>
<module>taotao-mannger-pojo</module>
<module>taotao-mannger-interface</module>
<module>taotao-mannger-service</module>
<module>taotao-mannger-service2</module>
</modules>
确实是多了一个,这个是测试创建的时候多写蓝一个,真尴尬!
问题二、org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-8083]]
这个问题是说端口占用了,因为插件启动了两次,所以只要关闭就行了。
问题三、在mannger没哟umaven install的情况下
Failed to execute goal on project taotao-mannger-web: Could not resolve dependencies for project com.taotao:taotao-mannger-web:war:0.0.1-SNAPSHOT: Could not find artifact com.taotao:taotao-mannger-interface:jar:0.0.1-SNAPSHOT -> [Help 1]
这个是mannger-web找不到依赖包的关系, 右键mannger maven install即可。
问题四:描述如下
Property 'name' threw exception; nested exception is java.lang.IllegalStateException: Invalid name="/taotao-mannger-web" contain illegal charactor, only digit, letter, '-', '_' and '.' is legal.
这个是在mannger-web引入服务的时候
<dubbo:application name="taotao-mannger-web"/>
这个属性name写错蓝。超尴尬。
问题五、mapper.xml文件不发表的问题
Invalid bound statement (not found):com.taotao.mapper.TbItemMapper.selectByPrimaryKeyorg.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByPrimaryKey
这个问题是说找不到mapper代理对象,因为用的接口,那只能是说mapper.xml文件没有移动过来。解决办法,在mannger-dao中的pom.xml加入:
<!-- 解决mapper.xml文件不发布的问题 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
问题六、访问http://localhost:8081/item/830972
键入地址返回的是个空的字符串,原本因该是各json串的。推测,没有报错,说明请求接受到了,但是返回值是各空,数据库有这个值,那说明itemId这个字段没有接收到值,查看代码后发现果然少了一个注解:@PathVariable 问题解决。
问题七:序列化
com.alibaba.dubbo.remoting.RemotingException: Failed to send response: Response [id=2, version=2.0.0, status=20, event=false, error=null, result=RpcResult [result=com.taotao.pojo.TbItem@2321786e, exception=null]], cause: java.lang.IllegalStateException: Serialized class com.taotao.pojo.TbItem must implement java.io.Serializable
将pojo进行序列化。