2020-03-21Dubbo本地环境搭建-实现服务注册和消费

Posted 张曾经

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020-03-21Dubbo本地环境搭建-实现服务注册和消费相关的知识,希望对你有一定的参考价值。

前言

    本周主题:加班工作。本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度。今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理。之所以说是重拾,是因为去年自学过一次,但那次主要是针对源码的流程,在实战上欠缺,且对其理解未深入到架构层次,只能说是基本理解。现在的我跟去年比起来,对技术的理解上有了一些提升,经验也更丰富,故本次目标是做深入研究,且看能从中吸收多少要义。

    今天先记录一下dubbo本地服务的简易搭建流程。

一、环境准备

    本次搭建用zookeeper作为注册中心,故需要准备好zookeeper环境。博主是在自己购置的阿里云服务器上搭建的,操作比较简单,可参见【https://yq.aliyun.com/articles/83804?spm=5176.10695662.1996646101.searchclickresult.5d89d8499feXWX】一文,其中要注意的是需提前在云服务器上开通2181端口(即zk的端口),否则dubbo服务会连不上,博主今天就是在这里排查了好久,才想到原来是自己犯蠢了。

二、项目构建

    本地项目用的maven项目,一个消费模块一个服务模块,结构如下所示:

 

 1、服务端代码

pom文件依赖:

 1 <dependencies>
 2         <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
 3         <dependency>
 4             <groupId>org.apache.dubbo</groupId>
 5             <artifactId>dubbo</artifactId>
 6             <version>2.7.5</version>
 7         </dependency>
 8         <dependency>
 9             <groupId>org.slf4j</groupId>
10             <artifactId>slf4j-api</artifactId>
11             <version>1.7.30</version>
12         </dependency>
13         <dependency>
14             <groupId>org.slf4j</groupId>
15             <artifactId>slf4j-log4j12</artifactId>
16             <version>1.7.25</version>
17         </dependency>
18         <dependency>
19             <groupId>log4j</groupId>
20             <artifactId>log4j</artifactId>
21             <version>1.2.17</version>
22         </dependency>
23         <dependency>
24             <groupId>org.apache.curator</groupId>
25             <artifactId>curator-recipes</artifactId>
26 <version>4.0.1</version> 27 </dependency> 28 </dependencies>

xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 5        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">
 6 
 7     <!-- 应用名 -->
 8     <dubbo:application name="my-dubbo-provider"/>
 9 
10     <!-- 使用zookeeper注册中心暴露服务地址 -->
11     <dubbo:registry address="zookeeper://xxxx:2181" use-as-config-center="true" timeout="10000"/>
12 
13     <!-- 用dubbo协议在20880端口暴露服务 -->
14     <dubbo:protocol name="dubbo" port="20880" />
15 
16     <!-- 要暴露的服务接口 -->
17     <dubbo:service interface="com.dubbo.provider.DemoService" ref="demoServiceImpl" />
18 
19 </beans>

服务类:

 1 package com.dubbo.provider.impl;
 2 
 3 import com.dubbo.provider.DemoService;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 public class DemoServiceImpl implements DemoService {
 8 
 9     public String doSomething(String name) {
10         System.out.println("消费端传过来了:" + name);
11         return name + "做了XXOO!";
12     }
13 }

config类导入配置文件:

 1 package com.dubbo.client;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.context.annotation.ImportResource;
 6 
 7 @Configuration
 8 @ImportResource("classpath:application-provider.xml")
 9 @ComponentScan("com.dubbo")
10 public class ProviderConfig {
11 }

服务端启动类:

 1 package com.dubbo.client;
 2 
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 
 5 import java.io.IOException;
 6 
 7 public class ProviderClient {
 8     public static void main(String[] args) {
 9         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ProviderConfig.class);
10         applicationContext.start();
11         try {
12             System.in.read(); // 任意输入即可退出
13         } catch (IOException e) {
14             e.printStackTrace();
15         }
16     }
17 }

启动后会一直提供服务,输入任意信息退出。

2、消费端代码

pom文件依赖:

 1 <dependencies>
 2         <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
 3         <dependency>
 4             <groupId>org.apache.dubbo</groupId>
 5             <artifactId>dubbo</artifactId>
 6             <version>2.7.5</version>
 7         </dependency>
 8         <dependency>
 9             <groupId>my-dubbo-demo</groupId>
10             <artifactId>my-dubbo-demo-provider</artifactId>
11             <version>1.0-SNAPSHOT</version>
12         </dependency>
13     </dependencies>

 

xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 5        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">
 6 
 7     <!-- 消费方应用名 -->
 8     <dubbo:application name="my-dubbo-consumer"  />
 9 
10     <!-- 使用zookeeper注册中心暴露发现服务地址 -->
11     <dubbo:registry address="zookeeper://xxxx:2181" />
12 
13     <!-- 引入服务 -->
14     <dubbo:reference id="demoService" interface="com.dubbo.provider.DemoService" />
15 </beans>

消费类:

 1 package com.dubbo.consumer;
 2 
 3 import com.dubbo.provider.DemoService;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Service;
 6 
 7 @Service
 8 public class ConsumerDemo {
 9     @Autowired
10     private DemoService demoService;
11 
12     public String consumeDemoService (String name) {
13         String result = demoService.doSomething(name);
14         System.out.println(result);
15         return result;
16     }
17 }

消费端配置类:

 1 package com.dubbo.client;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.context.annotation.ImportResource;
 6 
 7 @Configuration
 8 @ComponentScan("com.dubbo.consumer")
 9 @ImportResource("classpath:application-consumer.xml")
10 public class ConsumerConfig {
11 }

消费端启动类:

 1 package com.dubbo.client;
 2 
 3 import com.dubbo.consumer.ConsumerDemo;
 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 5 
 6 public class ConsumerClient {
 7     public static void main(String[] args) {
 8         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConsumerConfig.class);
 9         ConsumerDemo consumerDemo = applicationContext.getBean(ConsumerDemo.class);
10         consumerDemo.consumeDemoService("马甲哥");
11     }
12 }

3、启动调用

    先启动服务端,此时登入zookeeper的客户端,通过ls /dubbo命令即可看到注册到zk上的服务端节点。

    然后启动消费端main方法,发现服务调用成功。

消费端日志:

 

 服务端日志:

 

 

至此,一个简易版的本地dubbo框架便搭建好了,后面要做的就是debug调试整个的调用流程,我们未完待续!

 

以上是关于2020-03-21Dubbo本地环境搭建-实现服务注册和消费的主要内容,如果未能解决你的问题,请参考以下文章

DUBBO+Zookeeper在Centos7中本地搭建及小案例

Dubbo简介-环境搭建

dubbo分布式系统---环境搭建

dubbo+zookeeper+springmvc搭建实例教程

dubbo+zookeeper+springmvc搭建实例教程

DUBBO本地搭建及小案例 (转)