介绍:以后开发的时候需要将业务中心单独剥离出去,实际上可以得到以下优势:
• 一旦进行业务更新,只需要修改业务中心即可,不要修改WEB端
• 业务中心也是一个又一个的独立系统,那么可以继续利用负载均衡的概念,实现多个业务功能相同的服务器集群
• 由于同一个业务可以不同的服务主机实现,这样当某台主机的业务中心无法使用时,可以自动切换到可用的主机,继续提供业务的处理,所以拥有高可用(HA)的处理机制
• 在进行某些重大业务处理的时候,可以采用降级的模式让出某些业务中心的资源。
1.创建dubbo项目:Dubbo项目都是基于Spring框架开发的,按照官方给定的标准此时应该创建有三个项目:dubbo-api,dubbo-echo-provider,
dubbo-echo-consumer。
2.创建一个"dubbo-api"的项目模块,这个模块主要定义公共的VO类以及公共的业务接口。
3.在"dubbo-api"的项目中,定义一个传输的Message的对象即VO类
package com.yootk.dubbo.vo;
import java.io.Serializable;
public class Message implements Serializable {
private String title ;
private String content ;
private String sender ;
}
4.【dubbo-api】只要是进行RPC项目开发,一定需要提供有一个公共的标准业务接口,该接口的实现由provider负责。
package com.yootk.dubbo.service;
import com.yootk.dubbo.vo.Message;
public interface IMessageService {
public Message echo(Message msg) ;
}
5.由于在整个项目中,api模块要被provider模块和consumer模块引用,所以最好早父项目的pom.xml配置文件中进行模块的引用定义、
<dependency>
<groupId>com.yootk</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0</version>
</dependency>
6.创建"dubbo-echo-provider"模块,同时要引入api模块,定义IMessageService的子类
package com.yootk.dubbo.service.impl;
import com.yootk.dubbo.service.IMessageService;
import com.yootk.dubbo.vo.Message;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements IMessageService {
@Override
public Message echo(Message msg) {
Message echoMessage = new Message() ;
echoMessage.setTitle("【ECHO】" + msg.getTitle());
echoMessage.setContent("【ECHO】" + msg.getContent());
echoMessage.setSender("【ECHO】" + msg.getSender());
return echoMessage;
}
}
7.【dubbo-echo-provider】创建"src/main/profiles/dev/config/dubbo.properties配置文件及目录,dev为源文件夹
# 定义应用程序的名称,这个名称随便填写,只要不重名就好
dubbo.application.name=dubbo-echo-provider
# 由于Dubbo属于远程的调用,就需要考虑到连接的超时时间(毫秒)
dubbo.provider.timeout=10000
# 定义Dubbo服务注册地址的ZooKeeper连接
dubbo.registry.address=zookeeper://zookeeper-cluster-a:2181;zookeeper-cluster-b:2181;zookeeper-cluster-c:2181
# 定义Dubbo协议的监听端口
dubbo.protocol.port=9327
# 设置Dubbo服务版本编号,版本编号和profile一致,版本号不同无法使用Dubbo
dubbo.interface.version=dev
# 缓存在本地的服务注册信息项
dubbo.registry.file=/usr/data/dubbo/dubbo-echo-registry.properties
8.【dubbo-echo-provider】在"src/main/resources/"创建一个META-INF的子目录(Dubbo要求的)
9.在META-INF目录下创建spring/spring-base.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: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://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yootk.dubbo.service.impl"/>
<context:property-placeholder location="classpath:config/*.properties"/>
</beans>
10.在此目录下在创建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: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服务的提供者的应用程序名称 -->
<dubbo:application name="${dubbo.application.name}" />
<!-- 在进行Dubbo服务注册的时候一定要将服务注册到ZooKeeper之中 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"
timeout="${dubbo.provider.timeout}" file="${dubbo.registry.file}"/>
<!-- 定义Dubbo服务的提供者的相关处理配置,配置要使用的名称以及发布的端口 -->
<dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" />
<!-- 将远程接口的实现子类配置到服务之中,这一个是关键的步骤 -->
<dubbo:service
interface="com.yootk.dubbo.service.IMessageService"
ref="messageServiceImpl" version="${dubbo.interface.version}" />
</beans>
11.在此模块下写一个启动程序
package com.yootk.dubbo.main;
import com.alibaba.dubbo.container.Main;
public class StartMessageServiceMain {
public static void main(String[] args) {
Main.main(args); // 启动Dubbo服务
}
}
执行结果为Dubbo service server started!则表示启动成功
12.随意选择集群之中的任意一台Zookeeper服务器进行客户端的处理操作:
执行:/usr/local/zookeeper/bin/zkCli.sh -server zookeeper-cluster-b
节点列表:ls /dubbo
如果Dubbo服务正常注册成功了,那么一定会在Zookeeper里面针对于此接口提供有一个专属的节点。
13.创建"dubbo-echo-consumer"模块,引用api模块,创建src/main/profiles/dev/config/dubbo.properties目录及文件
# 客户端应用的名称
dubbo.application.name=dubbo-echo-consumer
# 由于Dubbo属于远程的调用,就需要考虑到连接的超时时间(毫秒)
dubbo.provider.timeout=10000
# 定义Dubbo服务注册地址的ZooKeeper连接
dubbo.registry.address=zookeeper://zookeeper-cluster-a:2181;zookeeper-cluster-b:2181;zookeeper-cluster-c:2181
# 设置Dubbo服务版本编号,版本编号和profile一致,版本号不同无法使用Dubbo
dubbo.interface.version=dev
14.创建src/main/resources/spring/spring-base.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: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://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yootk.dubbo.consumer"/>
<context:property-placeholder location="classpath:config/*.properties"/>
<import resource="spring-dubbo-consumer.xml"/>
</beans>
15.创建src/main/resources/spring/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: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服务的提供者的应用程序名称 -->
<dubbo:application name="${dubbo.application.name}" />
<!-- 在进行Dubbo服务注册的时候一定要将服务注册到ZooKeeper之中 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"
timeout="${dubbo.provider.timeout}"/>
<!-- 通过注册中心去引入远程接口实现类,即实例化远程接口对象 -->
<dubbo:reference id="messageInstance"
interface="com.yootk.dubbo.service.IMessageService"
version="${dubbo.interface.version}" />
</beans>
16.编写测试类调用业务层检测接口功能是否能正常使用
package com.yootk.dubbo.test;
import com.yootk.dubbo.service.IMessageService;
import com.yootk.dubbo.vo.Message;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = {"classpath:spring/spring-base.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMessageService {
@Autowired
private IMessageService messageInstance ;
@Test
public void testEcho() {
Message message = new Message() ;
message.setTitle("Hello同学们");
message.setContent("即将迎来伟大的假期!");
message.setSender("超级可爱的小李");
System.out.println(this.messageInstance.echo(message));
}
}
17.思考:一个项目的业务接口是非常多的,如果按照以上的方式(上面的背景色为 的部分就是引用远程接口的地方)进行所有业务接口的定义,那么就极为繁琐,下面采用注解的方式引入接口便方便很多。
• 修改spring-dubbo-provider.xml配置文件,在这个配置文件里面启用扫描配置:将上面的金色部分字体删除
<dubbo:annotation package="com.yootk.dubbo.service.impl"><!--扫描路径,理解为扫描包-->
• 由于此时要使用Dubbo的方式进行扫描处理,所以修改MessageServiceImpl子类,更换新的@Service注解,采用import com.alibaba.dubbo.config.annotation.Service;
• 修改spring-dubbo-consumer.xml配置文件,将上面的金色字体部分删除,添加以下代码
<!--此时一定要写上需要调用Dubbo远程业务接口的类所在的包的名称,下面的com.yootk.dubbo.test就是包名称-->
<dubbo:annotation package="com.yootk.dubbo.test">
• 在【dubbo-consumer】模块,在注入远程业务层实例的时候采用新的注解@Reference