简介
Apache Dubbo |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。RPC全称为remote procedure call,即远程过程调用。(这里结合下面的功能实现进行理解)
本案例实现效果描述:
dubbo_quick_provider和dubbo_quick_consumer两个项目分别部署在两个服务器上,不在一个内存空间上。dubbo_quick_provider可以调用dubbo_quick_consumer上的方法。
实现讲解:
这是Dubbo官网提供的架构
consumer想要调用provider中的方法就需要一个服务注册中心来支持。provider需要告诉注册中心可以被调用的方法是哪个,consumer需要告诉服务注册中心想要调用哪个方法。本案例使用zookeeper作为注册中心,zookeeper的安装在上一篇有介绍点击进入。
管理控制台(Monitor)起到一个监控管理的作用,不是必须,我们在下一篇进行介绍。
代码实现:
dubbo_quick_provider:
pom.xml导入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</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.6.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
日志:log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug, stdout
配置文件:applicationContext-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="dubbo_quick_provider"/>
<!--指定zookeeper注册中心的address和port-->
<dubbo:registry address="zookeeper://192.168.182.129:2181" ></dubbo:registry>
<!--配置协议和端口-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--开启注解扫描-->
<dubbo:annotation package="com.zyw.service.impl"/>
</beans>
web.xml加载spring配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-provider.xml</param-value>
</context-param>
HelloService接口
public interface HelloService {
public String sayHello(String name);
}
HelloServiceImpl实现类,注意@Service注解是dubbo的不是spring的
@Service //注意这里使用com.alibaba.dubbo.config.annotation.Service; 不是spring的注解
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello"+name;
}
}
目录结构如图所示:
dubbo_quick_consumer:
pom.xml导入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</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.6.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
日志:log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug, stdout
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--开启包扫描,用来扫描所有的Spring注解-->
<context:component-scan base-package="com.zyw.controller"/>
<!--开启MVC注解,使所有的MVC注解生效-->
<mvc:annotation-driven/>
<!--为服务消费者在向注册中心订阅服务时起个名字-->
<dubbo:application name="dubbo_quick_consumer"/>
<!--连接到zookeeper注册中心-->
<dubbo:registry address="zookeeper://192.168.182.129" port="2181"/>
<!--开启包扫描,使dubbo的注解生效,让当前的被注解的类交给dubbo管理-->
<dubbo:annotation package="com.zyw.controller"/>
</beans>
web.xml
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
HelloService 接口
public interface HelloService {
public String sayHello(String name);
}
HelloController
@Controller
@RequestMapping("/hello")
public class HelloController {
@Reference
private HelloService helloService;
@ResponseBody
@RequestMapping("/sayHello")
public String sayHello(String name){
return helloService.sayHello(name);
}
}
目录结构
测试:
1.启动zookeeper服务,注意开启防火墙端口2181,没有开启的请参考下面代码:
firewall-cmd --zone=public --add-port=2181/tcp --permanent
#重新加载并查看开放的端口号
firewall-cmd --reload
firewall-cmd --permanent --zone=public --list-ports
2.tomcat以8081端口启动dubbo_quick_provider
3.tomcat以8082端口启动dubbo_quick_consumer
调用成功!!
帮助到您请点赞关注收藏谢谢!!