本章节内容是《Spring集成Dubbo 3.x脚手架》和《Dubbo 3与Spring集成 - producer篇》的续集,需要先看完这两篇才能开始这一篇哈。
前置准备:【以下三项内容没有完成,请看《Spring集成Dubbo 3.x脚手架》和《Dubbo 3与Spring集成 - producer篇》】
1、完成Spring+Dubbo的Producer端的开发,并且启动成功,成功标准见《Dubbo 3与Spring集成 - producer篇》
2、完成Spring+Dubbo的Consumer子模块的创建,如何创建子模块参见《Spring集成Dubbo 3.x脚手架》
章节导读:
1、展示已完成的Spring+Dubbo的Consumer子模块
2、配置调用Producer服务
3、开发Consumer客户端代码
4、演示整合Producer和Consumer的内容
1、展示已完成的Spring+Dubbo的Consumer子模块
项目结构图

POM文件内容
<?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>
<!-- 增加parent节点, 根据你自己设置的父工程Maven坐标设置 -->
<parent>
<groupId>com.jiangzh.course</groupId>
<artifactId>spring_dubbo_study</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.jiangzh.course</groupId>
<artifactId>dubbo_consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dubbo_consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 修改成对应JDK版本 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 增加dubbo依赖包管理 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<!-- 增加Spring相关依赖 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<!-- spring相关jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Spring配置文件内容
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系,name可以随便起名,但是不能重复 -->
<dubbo:application name="hello-world-consumer"/>
<!-- 使用zookeeper为注册中心,客户端使用curator -->
<dubbo:registry address="zookeeper://localhost:2181" client="curator"/>
</beans>
启动类内容
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
System.in.read(); // 按任意键退出
}
}
2、配置调用Producer服务
在consumer端的Spring配置文件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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系,name可以随便起名,但是不能重复 -->
<dubbo:application name="hello-world-consumer">
<!-- qos默认开启,为了不与producer端口冲突,需要修改此内容 -->
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<!-- 使用zookeeper为注册中心,客户端使用curator -->
<dubbo:registry address="zookeeper://localhost:2181" client="curator"/>
<!-- 调用远程Producer的服务 -->
<dubbo:reference id="producerService" interface="com.jiangzh.course.service.IProducerService" />
</beans>
3、开发Consumer客户端代码
3.1 开发相关接口,这里一定要注意,接口的package与Producer的package一定要一模一样

3.2 调用远程服务返回结果
IProducerService iProducerService = (IProducerService)context.getBean("producerService");
String response = iProducerService.sayHello("Hello Producer!!!");
System.out.println("response : "+response);

4、演示整合Producer和Consumer的内容

网友评论