Spring、Spring-boot 整合使用Dubbo
废话说在前面
在工作过程中,老板安排了工作,将原来项目的用户系统与另外一个项目打通(以后称作A项目、B项目)。刚开始的时候因为A项目需要调用B项目的接口做操作。然后就一顿URL常量定义,远程调用接口来实现,后面发现定义的常量越来越多太麻烦了,所以想到用Dubbo这种分布式的架构解决这个问题。
开发工具
- zookeeper
- Eclipse
有用的链接(必要的哦)
- Zookeeper 下载地址:https://zookeeper.apache.org/releases.html
- Dubbo文档链接 :http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
POM引入
必要的引入有两个(Spring 和Spring-boot的基础依赖就不再赘述)
#dubbo的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
#spring-boot 还需要引入下面依赖 1.x :0.1.2.RELEASE 2.x :0.2.1.RELEASE
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.2.RELEASE</version>
</dependency>
#dubbo的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
#spring-boot 还需要引入下面依赖 1.x :0.1.2.RELEASE 2.x :0.2.1.RELEASE
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.2.RELEASE</version>
</dependency>`
//自定义的接口包,消费者和提供者都需要引用的包,
//自己新建一个simple maven项目,然后编写Interface。
// 拿到这个项目的groupId、artifactId和version三要素 在消费者和提供者都引入。
//这个十分重要哦。以下是我自定义的
<dependency>
<groupId>com.dubbo.interface</groupId>
<artifactId>dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>`
实战来了
配置文件
- spring 消费者和提供者 可以在 Dubbo文档中找到现有的配置。下面贴出来
//provider.xml 示例
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20890"/>
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
</beans>
//consumer.xml示例
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry group="aaa" address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
</beans>
- spring-boot
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer //dubbo 应用名称
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 //zookeeper地址
spring.dubbo.scan=com.jaycekon.dubbo.service` //扫描的包
写代码
package com.dubbo.service;
public interface DemoService {
public String getString(String subaccountid);
}
package com.demo.service.dubbo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//依赖子dubbo
import com.alibaba.dubbo.config.annotation.Service;
//自定义包中的接口(我们已经引入了对于自定义包的依赖)
import com.dubbo.service.DemoService;
@Service
@Component
public class DemoServiceImpl implements DemoService{
@Override
public String getCashableAmount(String name) {
// TODO Auto-generated method stub
return "hello"+ name ;
}
}
package org.demo.service.service;
import org.demo.service.init.DubboInit;
import com.dubbo.service.DemoService ;
public class DemotServiceImpl {
public void name() {
DemoService demoService = (DemoService ) DubboInit.CONTEXT
.getBean("DemoService ");
System.err.println(accountService.getString("liu"));
}
}
package org.demo.service.init;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboInit {
private static Logger log = LoggerFactory.getLogger(DubboInit.class);
public static ClassPathXmlApplicationContext CONTEXT;
private static boolean inited = false;
public static void init(String[] args) {
if (!inited) {
synchronized (log) {
CONTEXT = new ClassPathXmlApplicationContext("classpath:consumer.xml");
inited = true;
}
}
}
}