为什么用Dubbo:
Dubbo是阿里开源的开源的一个高性能优秀的服务框架,详见https://www.oschina.net/question/tag/dubbo,Dubbo撑起阿里的很多业务,可以理解为业务的桥梁,一个消费者需要知道哪里有这么一个服务,dubbo告诉他,然后他自己去调用。它的原理和优点详见https://www.oschina.net/p/dubbo。
环境:
dubbo-admin-2.5.3.war
CentOS 6.5
zookeeper-3.4.6.tar.gz
jdk-7u25-linux-x64.gz
Dubbo与Zookeeper、SpringMVC整合使用
1、zookeeper集群搭建
关于zookeeper集群搭建,可以参考我之前写的博客。
链接地址:https://my.oschina.net/u/2489417/blog/778600
2、配置dubbo-admin的管理页面
(1):复制local文件夹dubbo-admin-2.5.3.war包,放在tomcat的webapps/ROOT下,然后进行解压:
拷贝:
[root@wbx1 local]# cp /usr/local/dubbo-admin-2.5.3.war /usr/local/apache-tomcat-7.0.68/webapps/ROOT/
解压:
jar -xvf dubbo-admin-2.5.3.war
(2):然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:
(3) 然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:
3:SpringMVC与Dubbo的整合
(1)Maven构建的多模块项目
这里不介绍了,大家可以参考我之前博客 https://my.oschina.net/u/2489417/blog/777443
(2)我们先开发服务注册的,就是提供服务,项目结构如图所示:
a:SDK项目里加入一个服务接口,代码如下:
b:Service项目里在pom.xml加入Dubbo和Zookeeper的jar包代码如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wangbx</groupId>
<artifactId>project-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-service</artifactId>
<packaging>jar</packaging>
<name>project-service</name>
<dependencies>
<dependency>
<groupId>com.wangbx</groupId>
<artifactId>project-SDK</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.wangbx</groupId>
<artifactId>project-mybatis</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
c:service项目实现具体的服务,代码如下:
package com.wbx.project.service.information;
import org.springframework.stereotype.Service;
import com.wbx.project.dto.RealNameRegDto;
import com.wbx.project.interfaces.information.TestRegistryService;
@Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService{
public String userBind(RealNameRegDto realNameRegDto) {
// TODO Auto-generated method stub
return "hello"+realNameRegDto.getFullName();
}
}
d:我们服务以及实现好了,这时要暴露服务,代码如下:
dubbo-tfs-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: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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<dubbo:application name="${dubbo.application.name}" owner="${dubbo.application.owner}" />
<dubbo:registry protocol="${dubbo.monitor.protocol}" address="${dubbo.registry.address}" />
<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />
<dubbo:service interface="com.wbx.project.interfaces.information.TestRegistryService" ref="testRegistryService" timeout="100000" owner="${dubbo.application.owner}" group="${dubbo.service.group}"/>
</beans>
applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-lazy-init="true">
<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true"
location="classpath*:/application.properties" />
<!-- 扫包 包含Service 注解 ,排除其他注解 -->
<context:component-scan base-package="com.wbx.project">
</context:component-scan>
</beans>
e:编写服务启动类DemoProvider
f:启动项目,然后我们在Dubbo管理页面上显示,已经暴露的服务,但显示还没有消费者,因为我们还没实现消费者服务,如图所示:
第二:我们在开发服务消费者,就是调用服务,我们看一下子项目project-SpringMVC,消费者项目结构如图所示:
a:同样在project-SpringMVC添加下依赖的jar,有Dubbo和Zookeeper的jar包,同时依赖子项目project-SDK,代码如下:
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wangbx</groupId>
<artifactId>project-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.wangbx</groupId>
<artifactId>project-SpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>project-SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.wangbx</groupId>
<artifactId>project-SDK</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>servletapi</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>project-SpringMVC</finalName>
</build>
</project>
b:我们创建消费的dubbo.properties文件来配置dubbo,代码如下:
dubbo.properties
# dubbo config
dubbo.application.name=project-Consumer
dubbo.application.owner=wangbx
dubbo.registry.address=zookeeper://192.168.102.136:2181?backup=192.168.102.137:2181,192.168.102.132:2181
dubbo.monitor.protocol=registry
dubbo.protocol.name=dubbo
dubbo.protocol.serialization=dubbo
dubbo.protocol.port=20881
dubbo.protocol.accesslog=true
dubbo.service.group=wangbx
# logback
dubbo.container=spring,logback
dubbo.application.logger=slf4j
dubbo.logback.file=logs/biz-qa-manage.logback.log
dubbo.logback.level=INFO
dubbo.logback.maxhistory=10
c:我们再创建配置文件applicationContext-dubbo-service.xml,用来读取dubbo配置属性,同时 生成远程服务代理,可供消费方调用引用服务,代码如下:
applicationContext-dubbo-service.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-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dubbo/dubbo.properties</value>
</list>
</property>
</bean>
<!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="${dubbo.application.name}" owner="${dubbo.application.owner}" />
<!--zookeeper注册中心 -->
<dubbo:registry protocol="${dubbo.monitor.protocol}" address="${dubbo.registry.address}" />
<!-- 协议 -->
<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService-->
<dubbo:reference id="testRegistryService" interface="com.wbx.project.interfaces.information.TestRegistryService" check="false" timeout="100000" owner="${dubbo.application.owner}" group="${dubbo.service.group}"/>
</beans>
d:controller层实现注入服务TestRegistryService,调用服务的具体实现代码如下:
HelloController
package com.springmvc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wbx.project.dto.RealNameRegDto;
import com.wbx.project.interfaces.information.TestRegistryService;
@Controller
public class HelloController {
@Autowired
private TestRegistryService testRegistryService;
@RequestMapping(value="/Hello")
public String HelloWorld(Model model){
RealNameRegDto realNameRegDto = new RealNameRegDto();
realNameRegDto.setFullName("*****dubbo+SpringMVC");
String str = testRegistryService.userBind(realNameRegDto);
model.addAttribute("message","Hello World!!!"+str);
return "Helloworld";
}
}
e:web.xml我们完善下,代码如下
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- spring mvc 请求后缀 -->
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext-dubbo-service.xml
</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>production</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
f:前台显示的界面Hello.jsp 和 配置文件spring-mvc-servlet.xml我们没有变动,代码可查看之前博客
使用Maven、Eclipse搭建SpringMVC链接地址https://my.oschina.net/u/2489417/blog/775944
g:下面我们tomcat启动下项目,浏览器上访问项目地址:http://localhost:8080/project-SpringMVC/Hello
f:Dubbo管理页面上 查看地址 http://192.168.102.136:8080/ 点击查看消费者情况