work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Spring中集成Dubbo实践

Posted on 2019-04-24 15:28  work hard work smart  阅读(192)  评论(0编辑  收藏  举报

开发工具:Eclipse,项目结构如下

模块介绍:

DubboAPI    ----API接口

DubboConsumer ----消费者

DubboProvider ----生产者

DubboAPI  Service 提供的接口,
DubboProvider实现了 DubboAPI 接口,并对外开放http端口,DubboConsumer 依赖DubboAPI工程,但是不会直接依赖
DubboProvider工程。 这种可以让消费者仅仅依赖DubboAPI工程,而不需要对
DubboProvider工程的依赖,也是松耦合的经典

 

一、创建Root工程

 

下一步

 

 下一步

 

点击完成后的工程目录

然后删除src文件夹

pom.xml 配置如下

<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.example</groupId>
  <artifactId>DubboDemons</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Dubbo</name>
  
   <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    
    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>
    
    <!-- log4j日志包版本号 -->
    <slf4j.version>1.7.18</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    
   </properties>
   
   
   
   <dependencyManagement>
      <dependencies>
	    
	    <!-- 添加junit4依赖 -->
	    <dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>4.11</version>
	    </dependency>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
   
	    <!-- 添加日志相关jar包 -->
	    <dependency>
	      <groupId>log4j</groupId>
	      <artifactId>log4j</artifactId>
	      <version>${log4j.version}</version>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-api</artifactId>
	      <version>${slf4j.version}</version>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-log4j12</artifactId>
	      <version>${slf4j.version}</version>
	    </dependency>
    
	     <dependency>
	         <groupId>commons-logging</groupId>
	         <artifactId>commons-logging</artifactId>
	         <version>1.2</version>
	     </dependency>
    
	    <!-- dubbo -->
	    <dependency>
	      <groupId>com.alibaba</groupId>
	      <artifactId>dubbo</artifactId>
	      <version>2.5.3</version>
	      <exclusions>
	        <exclusion>
	          <groupId>org.springframework</groupId>
	          <artifactId>spring</artifactId>
	        </exclusion>
	      </exclusions>
	    </dependency>
    
    	<!-- Zookeeper zkclient-->
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		    <version>0.1</version>
		</dependency>
    
   </dependencies>
  </dependencyManagement>


  
</project>

  

二、创建API项目

新建一个Maven Module

 

下一步

 

下一步

 

下一步

点击完成

 

DubboDemons的工程pom.xml会自动增加Module: DubboAPI

 

而DubboAPI会增加parent

然后在DubboAPI项目向,右键->Run AS->Maven install 

 

三、创建provider(生产者)

 

下一步

 

下一步

点击完成

 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.example</groupId>
    <artifactId>DubboDemons</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>DubboProvider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>DubboProvider</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  
  	<dependency>
	    	<groupId>com.example</groupId>
	    	<artifactId>DubboAPI</artifactId>
	    	<version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		
		
		<!-- 添加日志相关jar包 -->
	    <dependency>
	      <groupId>log4j</groupId>
	      <artifactId>log4j</artifactId>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-api</artifactId>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-log4j12</artifactId>
	    </dependency>
    
	     <!-- dubbo -->
	    <dependency>
	      <groupId>com.alibaba</groupId>
	      <artifactId>dubbo</artifactId>
	      <exclusions>
	        <exclusion>
	          <groupId>org.springframework</groupId>
	          <artifactId>spring</artifactId>
	        </exclusion>
	      </exclusions>
	    </dependency>
    
    	<!-- Zookeeper zkclient-->
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		</dependency>



    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

  

 

四、创建consumer(消费者)

 

下一步

 

下一步

 

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.example</groupId>
    <artifactId>DubboDemons</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>DubboConsumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>DubboConsumer</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version><!--$NO-MVN-MAN-VER$-->
      <scope>test</scope>
    </dependency>
    
    
      <dependency>
    	<groupId>com.example</groupId>
    	<artifactId>DubboAPI</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
     </dependency>
    
    <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
    </dependency>
 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
	</dependency>
 
    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
   
   	<!-- Zookeeper zkclient-->
	<dependency>
	    <groupId>com.github.sgroschupf</groupId>
	    <artifactId>zkclient</artifactId>
	</dependency>
    
  </dependencies>


</project>

  

创建完成后,

 

修改设置

 

 显示如下

 

 

五、定义接口API,bo对象,公共类及方法

这些是提供方和调用方都需要的

在DubboAPI模块中创建一个接口对外开放。 获得用户信息接口getUserInfo

 

 

 六、定义Dubbo服务service,实现DubbpAPI中的api接口,书写业务逻辑及DB操作。通过spring暴露服务。

在DubbboProvider模块中创建UserService的实现类UserServiceImpl

public class UserServiceImpl implements UserService{


	@Override
	public String getUserInfo(String arg0) {
		
		return "I'm " + arg0 + " Age: 30";
	}

	
}

  

 

 

DubboProvider模块的启动类

public class App 
{
    public static void main( String[] args ) throws IOException
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        System.out.println(context.getDisplayName());
        context.start();
        System.out.println("服务已经启动");
        //press any key to exist
        System.in.read();
    }
}

  

增加Provider.xml  /src/main/resources文件夹下

<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="org.dubbo.api.UserService" ref="userService" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="userService" class="org.dubbo.provider.impl.UserServiceImpl"/>
</beans>

   

 

发现eclipse出错

 

解决方法:

下载一个dubbo.xsd 文件

下载路径:https://pan.baidu.com/s/16N0PDU0zcyFWlMa2PML1OA

 windows->preferrence->xml->xmlcatalog 

add->catalog entry  ->file system 选择刚刚下载的文件路径

 

七、定义调用端也叫消费端,实现对应的控制层。通过spring获取服务

 启动类

public class App 
{
	 public static void main( String[] args ) throws IOException
	    {
	        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml");
	        System.out.println(context.getDisplayName());
	        context.start();
	        System.out.println("消费服务已经启动");
	        UserService userService = (UserService)context.getBean(UserService.class);
	        System.out.println(userService.getUserInfo("Nick"));
	        
	    }
}

  

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="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="permissionService" interface="org.dubbo.api.UserService"/>
    
</beans>

  

 

八、启动Zookeeper

九、启动Dubbo的service端,即Provider

启动Zookeeper后,启动DubboProvider

 

 十、启动Consumer端

启动DubboConsumer中的App类

 

参考:https://blog.csdn.net/u011781521/article/details/76337893

 

 

十一、Zookeeper客户端使用

 参考:ZooKeeper 可视化客户端工具的使用