什么是Dubbo?
dubbo是阿里巴巴公司开源的高性能优秀服务框架,通过高性能的RPC(远程服务调用)实现服务的输入输出功能,可以与spring框架无缝整合;
传统的架构所有的模块都在一台服务器上,单台服务器的性能毕竟有限,无法处理高并发,分布式服务将模块进行分离,通过多台服务器来分担一台服务器承受的压力。而多台服务器之间通信可以使用远程服务的调用。(自己理解的,如有不对还望指正);
Dubbo运行的原理:
1、Provider:服务的提供方;
2、ConSumer:调用远程服务的消费方;
3、Registry:服务注册与发现服务的注册中心(提供服务的一方在注册中心注册,消费者来到注册中心,注册中心提供给消费者已经注册服务的地址列表,消费者选择自己需要的服务去调用);
4、Monitor:统计服务的调用次数和时间;
5、Container:服务运行的容器;
(服务提供者:小张(生产汽车轮胎),注册中心:小册,服务消费者:小李(生产汽车的,需要轮胎);首先小张去找小册,注册一个自己的地址,说我这有最好的汽车轮胎,小张注册完了之后。小李又去找小册,说想要买轮胎来造汽车,小册就告诉小张,我这有卖汽车轮胎的地址,你去找他吧,然后小李就去找小张买轮胎,买来轮胎装在汽车上,在这整个过程中,还有一个小雅,它相当于Monitor,先有小张注册,然后小李来问过之后又去找过小张,它对着整个过程都做了记录。)
Dubbo的使用:
1、注册中心使用zookeeper:搭建zookeeper环境,在这里只是简单的使用dubbo,搭建一个单机版的zookeeper,不涉及集群,我使用的docker来搭建zookeeper,相对来说还是比较简单的。可以根据这个博客搭建 https://blog.csdn.net/qq_26641781/article/details/80886831
2、注册中心有了,然后开始敲代码:
2.1、项目结构:
一个聚合工程下创建三个子工程;其中controller是web项目,interface普通jar项目,service是web项目(创建项目就不详述了)
2.2、interface项目:只有一个接口TestInterface,
package top.interface1; public interface TestInterface { public String getData(String data); }
2.3、service项目(相当于提供者):对interface接口的实现,主要编写的TestService实现类,log4j文件,spring.xml文件,web.xml文件,pom.xml文件;
由于需要使用到另一个项目的interface接口,先引入interface项目的依赖;
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> <parent> <groupId>top.free</groupId> <artifactId>testdubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>service</artifactId> <packaging>war</packaging> <dependencies> <!-- interface项目依赖,需要用到接口 --> <dependency> <groupId>top.free</groupId> <artifactId>interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- springweb模块依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- dubbo相关依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> <!-- zookeeper客户端依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <!-- tomcat插件,端口是8081 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <uriEncoding>UTF-8</uriEncoding> <port>8081</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
TestService实现类:注意加@service注解
package top.service; import org.springframework.stereotype.Service; import top.interface1.TestInterface; @Service public class TestService implements TestInterface{ @Override public String getData(String data) { return "小张生产了轮胎,"+data+"来买走了"; } }
log4j.properties文件:
###set log levels### log4j.rootLogger=info, stdout ###output to the console### 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{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
spring.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://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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 扫描service包下的@Service注解 -->
<context:component-scan base-package="top.service"></context:component-scan> <!-- 发布服务到注册中心,告诉注册中心我这有轮胎 --> <!-- 为服务起一个别名,别名可以随便起,最好能见名知意 --> <dubbo:application name="testprovider"/> <!--address 注册中心zookeeper的地址(替换成自己的地址) protocol 指的是注册中心的协议的格式 --> <dubbo:registry address="192.168.25.130:2181" protocol="zookeeper"/> <!-- interface告诉注册中心是哪个类型 (这是轮胎) ref说明具体是哪个服务(实现类TestService的Bean名称,默认类名首字母小写) timeout连接超时的时间 --> <dubbo:service interface="top.interface1.TestInterface" ref="testService" timeout="60000"></dubbo:service> <!--配置暴露服务端口(相当于这是轮胎厂的西门,只有这个开着,其他的都关着),协议是dubbo,消费者消费的时候必须通过端口来消费,端口可以随便写,但是不能被占用,一个dubbo,独占一个端口 --> <dubbo:protocol name="dubbo" port="12345"></dubbo:protocol> </beans>
Web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>service</display-name>
<!--字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--加载spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param>
<!--spring监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
2.4、controller项目(消费者):TestController类,log4j文件(与service一致),spring.xml文件,pom.xml文件,Web.xml文件;
由于Controller中需要注入Interface接口,所以也需要引入Interface项目的依赖:
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> <parent> <groupId>top.free</groupId> <artifactId>testdubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>controller</artifactId> <packaging>war</packaging> <dependencies> <!-- webmvc模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- interface接口依赖 --> <dependency> <groupId>top.free</groupId> <artifactId>interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> <!-- zookeeper依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <!-- tomcat插件,端口8082,与service项目端口不能一样 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <uriEncoding>UTF-8</uriEncoding> <port>8082</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
TestController类:
package top.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.interface1.TestInterface; @RestController public class TestController { @Autowired private TestInterface test;
//produces防止显示json数据页面乱码 @RequestMapping(value="/test",produces = "text/json;charset=UTF-8" ) public String getData(String user) { String data = test.getData(user); return data; } }
spring.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://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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!--扫描controller包下的注解-->
<context:component-scan base-package="top.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!-- 消费者起一个别名,可以随便起,最好见名知意 --> <dubbo:application name="testconsumer"/> <!--address 注册中心的地址 protocol 指的是注册中心的协议的格式 --> <dubbo:registry address="192.168.25.130:2181" protocol="zookeeper"/> <!-- 告诉注册中心我需要一个轮胎,interface是Interface项目中的接口全路径 id指的是bean的引用 --> <dubbo:reference interface="top.interface1.TestInterface" id="testInterface"></dubbo:reference> </beans>
Web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>controller</display-name> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--springmvc的前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载核心配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<!--拦截以.do结尾的所有请求--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
至此配置完成;
测试:
首先安装整个聚合工程到本地,在聚合工程上右键run as-------》Maven install;安装过程中没报错,配置成功;
启动service项目:在service项目上右键:Run as ------》Maven build...(注意是有三个点的)------》会出现如下界面:
启动controller项目:与service步骤一致,命令不变;
在浏览器中输入localhost:8082/test.do?user=张三(这里访问的是controller项目启动的tomcat端口)
dubbo的简单使用完成;
如果启动时遇到:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project controller: Failed to clean project: Failed to delete E:Spring STS程序 estdubbocontroller arget omcatlogsaccess_log.2019-05-16 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
有相同的启动程序未关闭:点击打开会看到有未关闭的,关闭之后重新启动;(启动顺序是先启动service,再启动controller,否则先启动controller会因为查找不到服务而报错)