dubbo是一个分布式服务框架,是阿里巴巴SOA服务化治理方案的核心框架。zookeeper是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。zookeeper可以作为dubbo服务的注册中心,两者结合起来可以实现微服务中的 服务注册、发现、负载均衡和健康检查,容错,动态配置管理的功能。我们先用springMVC将dubbo服务暴露成浏览器可访问的http接口。
本例代码已经上传到GitHub,传送门见下:
https://github.com/ArchitectRoad/BoliERP.git
重要的事情说前面。先总结Dubbo+Spring Boot使用过程中的要点、注意点吧。
- Spring Boot的目的之一就是使开发人员可以摆脱繁杂的xml配置,所以Spring Boot整合Dubbo时,使用Annotation是推荐方式。使用Annotation集成Spring Boot + Dubbo时可以参照 https://github.com/JeffLi1993/springboot-learning-example。
- 为便于更好理解Dubbo的原理,在本例中仍然使用xml进行Dobbo provider和consumer的配置。Spring Boot + Dubbo联合使用时,需要显示在入口类中通过以下方式引用dubbo配置。
@ImportResource(classpath: dubbo-consumer.xml)
public class ProdWebApplication {
...
}
- Service Provider打包为Jar,使用com.alibaba.dubbo.container.Main作为入口启动Spring容器时,默认读取classpath:META-INF/spring/目录下的applicationContext.xml。关于如何生成Service Provider Jar,详见prodService模块的pom.xml中的build节点定义。
- 解决Dubbo与Spring Boot的依赖包冲突,引入dubbo时exclude spring。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-version}</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
- 解决log4j与slf4j的版本冲突。Exclude zookeeper中的slf4j与log4j,exclude zkclient中的log4j。
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper-version}</version>
<!-- Solve conflict with Spring boot -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient-version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
- classpath 和 classpath* 区别:
classpath: 只会到你的class路径中查找找文件。只加载找到的第一个文件。
classpath*: 不仅包含class路径,还包括jar文件中(class路径)进行查找。从多个jar文件中加载相同的文件。
用classpath*:需要遍历所有的classpath,所以加载速度较慢。因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。
**/ 表示的是任意目录。
项目结构如下:
- pordParentPom module
定义所有模块的parent pom,统一管理依赖版本。继承spring-boot-starter-parent,并定义dubbo, zookeeper, zkclient版本。通过exclusion解决jar包冲突。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.boli</groupId>
<artifactId>prodParentPom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<modules>
<module>../prodApi</module>
<module>../prodService</module>
<module>../prodWebApp</module>
</modules>
<properties>
<dubbo-version>2.5.3</dubbo-version>
<zookeeper-version>3.4.9</zookeeper-version>
<zkclient-version>0.1</zkclient-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- dubbo dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-version}</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper-version}</version>
<!-- Solve conflict with Spring boot -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient-version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
-
prodApi module
Api接口定义,以及Dubbo consumer定义。prodService模块具体实现Api接口。prodWebApp模块引用该模块,根据Api接口定义,通过zookeeper发现并调用具体服务。
IProdSvc.java
/*
* Copyright (c) 2017 architectroad@yeah.net. All Rights Reserved.
*/
package com.boli.service.intfc.prod;
import java.util.List;
/**
* Product Service Interface
* Author: Joey Zhu (architectroad@yeah.net)
* Date: 2017/3/11.
* Time: 11:01
*/
public interface IProdSvc {
public List<String> getProdList();
public String sayHello (String name);
}
prodApi.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:application name="prodSvcConsumer" />
<!-- 注册地址,用于消费者寻找服务,, 此处可以配置多个zookeeper -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 超时设置 -->
<dubbo:consumer timeout="5000" />
<!-- 引用的服务 -->
<dubbo:reference id="prodService" interface="com.boli.service.intfc.prod.IProdSvc" version="1.0.0" />
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>prodParentPom</artifactId>
<groupId>com.boli</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.boli</groupId>
<artifactId>prodApi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
</project>
- prodService module
ProdSvcProvider.java
Service Provider入口类
/*
* Copyright (c) 2017 architectroad@yeah.net. All Rights Reserved.
*/
package com.boli;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Service Provider Entry Class
*
* Author: Joey Zhu (architectroad@yeah.net)
* Date: 2017/3/13.
* Time: 13:58
*/
public class ProdSvcProvider {
public static void main(String[] args) throws Exception {
//本地启动spring容器,适用于开发阶段调试
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"META-INF/spring/applicationContext.xml"});
context.start();
System.out.println("启动成功");
System.in.read(); // 按任意键退出
//内置Tomcat方式启动
// SpringApplication.run(ProdSvcImpl.class, args);
//Dubbo spring容器方式启动
// com.alibaba.dubbo.container.Main.main(args);
}
}
prodService.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:application name="prodSvcProvider" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注册中心暴露服务地址,即zookeeper的所在服务器ip地址和端口号 -->
<!-- check: 注册中心不存在时是否报错, subscribe: 是否向此注册中心订阅服务, register: 是否向此注册中心注册服务 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="true"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 具体的实现bean -->
<bean id="prodService" class="com.boli.service.impl.prod.ProdSvcImpl"/>
<!-- 如果使用扫描包方式引入bean,使用如下方式 -->
<!-- dubbo:annotation package="com.boli.service.impl.*" / -->
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.boli.service.intfc.prod.IProdSvc" ref="prodService" version="1.0.0"/>
<!-- 使用注解方式暴露接口 -->
<!--dubbo:annotation package="com.boli.service.impl" /-->
<!-- 加入spring注解扫描 -->
<!--context:component-scan base-package="com.boli.service.impl"/-->
</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: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">
<!-- import dubbo providers -->
<import resource="classpath:dubbo/prodService.provider.xml"></import>
</beans>
pom.xml
通过mvn package命令打包生成prodService-1.0-SNAPSHOT.jar时,指定com.alibaba.dubbo.container.Main作为入口类,并拷贝所有的依赖包到lib目录中。打包完成后,使用java -jar prodService-1.0-SNAPSHOT.jar
命令即可运行并注册服务到指定的服务注册中心,比如zookeeper。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.boli</groupId>
<artifactId>prodService</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<artifactId>prodParentPom</artifactId>
<groupId>com.boli</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.boli</groupId>
<artifactId>prodApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!--生成的classes文件的路径,此文件夹中包含运行时的所有文件,可以不配置-->
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*provider.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources/META-INF/spring</directory>
<filtering>true</filtering>
<includes>
<include>applicationContext.xml</include>
</includes>
</resource>
</resources>
<!--下面的必须配置-->
<plugins>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- prodWebApp module
Web Application。服务消费方。
ProdController.java
/*
* Copyright (c) 2017 architectroad@yeah.net. All Rights Reserved.
*/
package com.boli.web;
import com.boli.service.intfc.prod.IProdSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* Product Controller
*
* Author: Joey Zhu (architectroad@yeah.net)
* Date: 2017/3/10.
* Time: 18:46
*/
@Controller
public class ProdController {
@Autowired
IProdSvc prodService;
@RequestMapping("/prodList")
@ResponseBody
private List<String> getProdList () {
return prodService.getProdList();
}
}
ProdWebApplication.java
/*
* Copyright (c) 2017 architectroad@yeah.net. All Rights Reserved.
*/
package com.boli;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
/**
* Product Module Web Application
* Author: Joey Zhu (architectroad@yeah.net)
* Date: 2017/3/13.
* Time: 12:16
*/
@EnableAutoConfiguration
@ImportResource({"classpath*:META-INF/spring/applicationContext.xml"})
@ComponentScan("com.boli.web")
public class ProdWebApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProdWebApplication.class, args);
}
}
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: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">
<!-- import dubbo consumers -->
<import resource="classpath*:dubbo/*.consumer.xml"></import>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.boli</groupId>
<artifactId>prodWebApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<artifactId>prodParentPom</artifactId>
<groupId>com.boli</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.boli</groupId>
<artifactId>prodApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
代码已经上传到GitHub,传送门见下:
https://github.com/ArchitectRoad/BoliERP.git
本地测试步骤及测试结果:
- 先启动ZooKeeper
joeys-MacBook-Pro:~ joey$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Library/ZooKeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
joeys-MacBook-Pro:~ joey$
-
启动ProdSvcProvider
从日志中可以看到服务注册成功
INFO com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry - [DUBBO] Register: dubbo://192.168.0.109:20880/com.boli.service.intfc.prod.IProdSvc?anyhost=true&application=prodSvcProvider&dubbo=2.5.3&interface=com.boli.service.intfc.prod.IProdSvc&methods=sayHello,getProdList&pid=5836&revision=1.0.0&side=provider×tamp=1490058278762&version=1.0.0, dubbo version: 2.5.3, current host: 127.0.0.1
3.启动ProdWebApplication
从日志中可以看到服务订阅成功
INFO 5872 --- [ main] c.a.d.r.zookeeper.ZookeeperRegistry : [DUBBO] Subscribe: consumer://192.168.0.109/com.boli.service.intfc.prod.IProdSvc?application=prodSvcConsumer&category=providers,configurators,routers&default.timeout=5000&dubbo=2.5.3&interface=com.boli.service.intfc.prod.IProdSvc&methods=sayHello,getProdList&pid=5872&revision=1.0.0&side=consumer×tamp=1490058693542&version=1.0.0, dubbo version: 2.5.3, current host: 192.168.0.109
4.访问应用
访问本地接口,验证web应用部署成功
访问远程服务接口,验证Dubbo远程服务调用成功