**在上一篇我们介绍了如何使用Dubbo,但是出现了以下问题:
1.服务层的对象需要我们自己new,代码的耦合度很大。
2.开发效率低,要写很多代码。
3.不适合做web应用。
为了解决以上问题,我们来整合spring框架。**
Spring整合Dubbo框架
首先我们需要创建四个项目。
下面是项目的结构:
—–Spring-Dubbo-Parent:父项目(打成jar包) |
—–Spring-Dubbo-api:提供接口服务(打成jar包) |
—-Spring-Dubbo-Server:项目的服务端(打成war包) |
—-Spring-Dubbo-Mgrsite:网站后台,项目的客户端(打成war包) |
开发步骤:
1.创建好以上几个项目之后,导入相关依赖(还要在客户端和服务端分别导入接口项目的依赖)。
服务端依赖:
dependencies>
<!--接口依赖 -->
<dependency>
<groupId>cn.just.dubbo</groupId>
<artifactId>Spring-Dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
客户端依赖:
<dependencies>
<!--接口依赖 -->
<dependency>
<groupId>cn.just.dubbo</groupId>
<artifactId>Spring-Dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
2.编写接口服务:
ProductService.java
package cn.just.dubbo.api;
import cn.just.dubbo.domain.Product;
public interface ProductService {
public Product getProduct(Product product);
}
Product.java
package cn.just.dubbo.domain;
import java.io.Serializable;
public class Product implements Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.编写服务端实现类:
ProduceServiceImpl.java:
package cn.just.dubbo.server.service;
import cn.just.dubbo.api.ProductService;
import cn.just.dubbo.domain.Product;
public class ProduceServiceImpl implements ProductService{
@Override
public Product getProduct(Product product) {
product.setName("不清不慎");
return product;
}
}
实现类编写好之后,接着就是添加spring的配置文件:
application-server.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 ">
<bean id="ProductService" class="cn.just.dubbo.server.service.ProduceServiceImpl" />
<!-- 配置服务名 -->
<dubbo:application name="Spring-Dubbo-Server" />
<!-- 注册地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 配置rpc协议 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="cn.just.dubbo.api.ProductService" ref="ProductService" />
</beans>
为了方便我们观看启动日志信息,我们添加log4j文件:
log4j.properties:
log4j.rootLogger=error,Console,OneFile,errFile
#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.encoding=utf-8
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
#\u8F93\u51FA\u5230\u6587\u4EF6\u7EA7\u522B\u4E3Ainfo
log4j.appender.OneFile=org.apache.log4j.RollingFileAppender
log4j.appender.OneFile.encoding=utf-8
log4j.appender.OneFile.File=../logs/test-map/info.log
log4j.appender.OneFile.MaxFileSize=10MB
log4j.appender.OneFile.MaxBackupIndex=50
log4j.appender.OneFile.Threshold=INFO ## \u53EA\u8F93\u51FAINFO\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.OneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.OneFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
#\u8F93\u51FA\u5230\u6587\u4EF6\u7EA7\u522B\u4E3Aerror
log4j.appender.errFile=org.apache.log4j.RollingFileAppender
log4j.appender.errFile.encoding=utf-8
log4j.appender.errFile.File=../logs/test-map/error.log
log4j.appender.errFile.MaxFileSize=10MB
log4j.appender.errFile.MaxBackupIndex=50
log4j.appender.errFile.Threshold=ERROR ## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.errFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
最后我们编写服务端主类启动程序:
App.java:
package cn.just.dubbo.server.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("application-server.xml");
context.start();
System.out.println("启动成功");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
至此,我们已经编写好服务端程序,注意在启动服务端的时候我们必须启动注册中心zookeeper,否则会报错,启动失败。
4.编写客户端:
服务端配置文件,application-client.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:application name="Spring-Dubbo-Client" />
<!-- 注册地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 引用服务 -->
<dubbo:reference interface="cn.just.dubbo.api.ProductService" id="ProductService" />
</beans>
启动客户端的主类,App.java:
package cn.just.Client.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.just.dubbo.api.ProductService;
import cn.just.dubbo.domain.Product;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("application-client.xml");
context.start();
ProductService productService=(ProductService) context.getBean("ProductService");
Product product=new Product();
product=productService.getProduct(product);
System.out.println(product.getName());
}
}
至此,我们已经整合好Spring,启动程序就会发现客户端调用服务端程序。
通过上面我们明显的感受到了不用 我们 编写繁琐重复的程序,全部交给spring容器去管理,这样会使我们的服务端和客户端程序方便简洁很多,下面,我贴出各个项目的目录结构图:
注意上面的报错不要管,这是IDE的问题,我用的是myeclipse,如果使用IDEA应该就不会出现这种错误,即使出现也不会影响程序的运行。这里提醒一下,我们在创建客户端和服务端项目时会报错,这是因为我们是打成war包,所以只要在webapp目录下添加WEB-INF/web.xml文件即可。