Dubbo基本是一个版本一个配置方式,2.6.x的时候是一种,2.7.x的时候是另一种,我这里使用的是Spring5.3.9+Dubbo2.7.13+zookeeper3.6.3来配置的,官网写的也是不清不楚,看不太懂
Dubbo官网
这里采用的是配置文件的方式配置,注解配置打算留着springboot再慢慢来尝试
服务提供者
(即service的实现类配置方式),同时要在实现类上添加@DubboService,@Service,@Transactional
<?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://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 指定应用名称 -->
<dubbo:application name="service"/>
<!--指定暴露服务的端口,如果不指定默认为20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--指定服务注册中心地址-->
<dubbo:registry address="zookeeper://zookeeper主机Ip地址:2181"/>
<!--批量扫描,发布服务-->
<dubbo:annotation package="com.ipisces42"/>
<context:component-scan base-package="com.ipisces42.service.impl"/>
<import resource="classpath:applicationContext-tx.xml"/>
</beans>
消费者
需要在Controller层添加@DubboReference
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<!--不需要视图解析器,项目中的所有的请求都返回json数据结构-->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
<property name="features">
<list>
<!--Map类型格式化,接收参数允许空值
User{name, age} => new User('zhangsan') => user{username: 'zhangsan'}
WriteMapNullValue
User{name, age} => new User('zhangsan') => user{username: 'zhangsan',age:null}
-->
<value>WriteMapNullValue</value>
<!--日期类型格式化 数值16..... 毫秒级的时间戳
WriteDateUseDateFormat yyyy-MM-dd hh:mm:ss
-->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 指定应用名称 -->
<dubbo:application name="web"/>
<!--指定服务注册中心地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--批量扫描 dubbo2.6.0下,mvc不需要再扫controller
2.6.2 则mvc要扫一次controller
<context:component-scan/>
-->
<dubbo:annotation package="com.ipisces42" />
<!--
超时全局设置 10分钟
check=false 不检查服务提供方,开发阶段建议设置为false
check=true 启动时检查服务提供方,如果服务提供方没有启动则报错
-->
<context:component-scan base-package="com.ipisces42.controller" />
<!--文件上传组件-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</beans>
Q.E.D.