项目开发需要使用到Dubbo框架进行开发,记录一个简单的demo。
1.Dubbo简介
目前我使用Dubbo就三个角色,Provider,Consumer,Registry
Provider是暴露服务的服务提供方
Consumer是调用远程服务的服务消费方
Registry:服务注册与发现的注册中心
流程:Provider在Registry中注册服务,Consumer在Registry中的调用服务。
2.DTO(Data Transfer Object)数据传输对象
根据字面理解,数据的传输基本分两类,输入和输出。这里的DTO指的是一个接口方法的传入参数和传出参数,没有行为的POJO对象,不包含任何物业逻辑,它的目的只是为了领域对象进行数据封装,实现层与层之间的数据传递,表现层与应用层之间通过数据传输对象(DTO)进行交互的。
简单来说,Model面向业务,我们是通过业务来定义Model的。而DTO是面向界面的,UI的需求来定义的。通过DTO我们实现了表现层与Model之间的解耦,表现层不引用Model,如果开发过程中我们的模型改变了,界面没变,我们就只需要改动Model而不需要改表现层中的东西。
另外,DTO可以是认为是向外公开的,而model是内部使用的,安全性也有一定保障。
3.Demo
(1)下载zookeeper(Registry)
百度自己查找最新的zookeeper,看教程自行安装,安装完毕,启动放一边就可以了。
(2)创建三个工程client server bizflow
client:
用来定义DTO,即输入、输出参数,以及Interface(服务,行为)
public interface IProductService {
public ProductOutputDto show(ProductInputDto inputDto);
}
DTO:
public class ProductInputDto implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class ProductOutputDto implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
server:实现client工程中定义的行为(服务)
包括
1)ORM-Mapping
2)DAO和DAOImpl
3)POJO对象转换成DTO对象
4)将实现的行为暴露成dubbo服务,供他人调用
要暴露的Service:
@Service("productService")
public class ProductService implements IProductService {
private ProductDao productDao = new ProductDao();
@Override
public ProductOutputDto show(ProductInputDto inputDto) {
Product product = productDao.load(inputDto.getId());
ProductOutputDto outputDto = new ProductOutputDto();
outputDto.setId(product.getId());
outputDto.setName(product.getName());
return outputDto;
}
}
bean.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"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:annotation-config/>
<!-- 自动扫描 -->
<context:component-scan base-package="org.xk"></context:component-scan>
<dubbo:application name="dubbo_serverr"/>
<!-- 注册位置 --
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!-- 要暴露的服务 -->
<dubbo:service interface="org.xk.lrcs.interfaces.IProductService" ref="productService"></dubbo:service>
</beans>
注册服务:
public class StartProductServiceProvider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
context.start();
System.out.println("product service provider is start...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
此处使用SpringMVC进行测试:详细可以参考上篇
SpringMVC配置
另外添加manager:
package org.xk.lrcs.manager;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.xk.lrcs.dto.ProductInputDto;
import org.xk.lrcs.dto.ProductOutputDto;
import org.xk.lrcs.interfaces.IProductService;
@Service("productManager")
public class ProductManager {
private IProductService productService;
@Resource(name="productService")
public void setProductService(IProductService productService) {
this.productService = productService;
}
public ProductOutputDto find(int id){
ProductInputDto inputDto = new ProductInputDto();
inputDto.setId(id);
return productService.show(inputDto);
}
}
controller:调用manager
@Controller
public class ProductController {
private ProductManager productManager;
@Resource(name="productManager")
public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>bizflow</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
hello-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<import resource="classpath:beans-consumer.xml"/>
<context:component-scan base-package="org.xk"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
beans-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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.xk"/>
<dubbo:application name="dubbo_server"/>
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!-- 调用 -->
<dubbo:reference interface="org.xk.lrcs.interfaces.IProductService"
id="productService"></dubbo:reference>
</beans>
项目之间的依赖关系:
server依赖于client,client.jar文件导入bizflow中,bizflow记得导入json有关jar
运行server中的
StartProductServiceProvider,bizflow放入Tomcat中,在浏览器中访问,如图: