具体参考我的gitee: springbootdubbo
spring-dubbo-provider.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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的,保持唯一性 -->
<!-- java.net.BindException: Address already in use: bind-->
<dubbo:application name="Dubbo-springboot">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="22222"/>
</dubbo:application>
<!--开启注解扫描,使dubbo的注解生效-->
<dubbo:annotation package="com.jk.*"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" register="" check="false" subscribe="false" />
<!-- 多协议dubbo协议和rmi协议在20880端口暴露服务 -->
<!-- 如果你想记录每一次请求信息,可开启访问日志,类似于apache的访问日志。注意:此日志量比较大,请注意磁盘容量-->
<dubbo:protocol name="dubbo" port="20881" accesslog="E:\oilrichfit\dubboAsk.log"/>
<!-- rmi consumer还得带着token 访问-->
<dubbo:protocol name="rmi" port="1099" accesslog="E:\oilrichfit\dubboAsk.log"/>
<!-- 和本地bean一样实现服务 -->
<bean id="testService" class="com.jk.service.TestServiceImpl"></bean>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.jk.service.TestService" ref="testService" timeout="30000"/>
<!-- 同一个接口多个实现类用group区分-->
<bean id="mysqlServiceA" class="com.jk.service.MysqlServiceImpl"></bean>
<!-- 令牌验证防止消费者绕过注册中心访问提供者,消费者不需要做任何操作,如果直连直接被拒绝了 token="123456"-->
<!-- 用不同的协议-->
<dubbo:service interface="com.jk.service.MysqlService" protocol="dubbo" token="123456" group="ced" ref="mysqlServiceA" timeout="30000"/>
<bean id="mysqlServiceB" class="com.jk.service.MysqlServiceImpl2"></bean>
<!-- 多个实现类时有一个写token就行,不然访问其他接口时报错-->
<dubbo:service interface="com.jk.service.MysqlService" protocol="rmi" group="fed" ref="mysqlServiceB" timeout="30000"/>
</beans>
spring-dubbo-consumer.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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-autowire="byName">
<!-- 消费方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用调用的,保持唯一性 -->
<dubbo:application name="consumer-of-helloworld-app">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<!-- 使用zookeeper注册中心发现暴露的服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 关闭所有服务的启动时检查 (没有提供者时报错)-->
<dubbo:consumer check="false"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService,该interface需要与服务端暴露的服务interface保持一致,该id已注入Spring -->
<!-- 关闭某个服务的启动时检查 (没有提供者时报错)<dubbo:reference interface="com.foo.BarService" check="false" /> 提供者不错操作-->
<!-- 增加对缓存的支持 cache="lru" 直接去缓存中取减少服务调用次数 提供者不做操作-->
<dubbo:reference id="testService" cache="lru" interface="com.jk.service.TestService" timeout="30000"></dubbo:reference>
<!-- 同一个接口多个实现类用group区分 其实就是当时两个serviceImpl 两个Controller 一个接口-->
<dubbo:reference id="mysqlServiceA" group="ced" cache="lru" interface="com.jk.service.MysqlService" timeout="30000"></dubbo:reference>
<dubbo:reference id="mysqlServiceB" group="fed" cache="lru" interface="com.jk.service.MysqlService" timeout="30000"></dubbo:reference>
</beans>