首页 > 学院 > 开发设计 > 正文

搭建spring+dubbo环境步骤

2019-11-06 06:39:55
字体:
来源:转载
供稿:网友

dubbo是什么?

dubbo是一个分布式服务架构,提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。

dubbo服务部署的步骤:

1、启动本地注册中心 2、启动dubbo服务控制台 3、新建maven工程,编写提供者接口,接口实现,xml文件配置 4、 junit测试消费者

1、启动本地注册中心:解压zookeeper.zip 运行命令 zkServer.cmd,端口2181

这里写图片描述

2、在tomcat(版本6),将dubbo-admin放到webapps项目底下,启动浏览器127.0.0.1:9000/dubbo-admin,浏览器控制台用户名和密码在dubbo-admin/WEB-INF/dubbo.PRoperties中进行配置

dubbo.admin.root.passWord=guest dubbo.admin.guest.password=guest

并且还要配置注册中心的地址:

dubbo-registry.address=zookeeper://127.0.0.1:2181

这里写图片描述

此时jdk的版本使用1.7,使用1.8会报错,服务器最好使用tomcat,在jetty上显示不出控制台

3、新建maven工程,pom文件如下

<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.haier</groupId> <artifactId>dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.6.RELEASE</spring.version> <!-- <spring.version>3.1.2.RELEASE</spring.version> --> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- 连接zookeeper的客户端 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies></project>

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这里只加载生产者和spring配置,消费者使用junit进行测试--> <param-value>classpath:/spring/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>

项目目录如下:

这里写图片描述

编写提供者的接口

package com.haier.dubbo;public interface Test { public void haveName(String name);}

编写提供者实现

package com.haier.dubbo;public class TestImpl implements Test{ public void haveName(String name) { System.out.println(name); }}

在xml中配置生产者(spring-dubbo.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="dubboWp" owner="wp" organization="iself"/> <dubbo:registry id="wpRegistry690" protocol="zookeeper" address="127.0.0.1:2181" timeout="100000"/> <bean name="testService" class="com.haier.dubbo.TestImpl"></bean> <dubbo:service ref="testService" interface="com.haier.dubbo.Test" version="1.0" protocol="dubbo"/></beans>

至此:生产者已经配置完毕,启动服务器,我此时使用的是exlipse插件的jetty,在dubbo-admin控制台上可查询到我们的接口信息

这里写图片描述

如果你在控制台查询到了你写的提供者的接口,证明发布成功了。

配置消费者,在这里消费者我就使用spring junit进行测试: 消费者extend.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="dubboWp" owner="wp" organization="iself"/> <dubbo:registry id="wpRegistry690" protocol="zookeeper" address="127.0.0.1:2181" timeout="100000"/> <!-- 样机预警接口--> <dubbo:reference id="testService" registry="wpRegistry690" owner="wp" interface="com.haier.dubbo.Test" version="1.0" protocol="dubbo" timeout="100000"/></beans>

消费者junit测试类:

package com.haier.dubbo;import javax.annotation.Resource;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:/spring/extend.xml", "classpath*:/spring/spring-core.xml"})public class TestDubbo{ @Resource public Test test; @org.junit.Test public void testDubbo(){ test.haveName("test"); }}

代码和安装包下载地址: http://download.csdn.net/detail/shiguang_zhiren/9769602


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表