上一篇文章介绍了zookeeper,作为服务的注册中心,如何配置集群等,接下来使用dubbo开发接口作为服务的提供者,通过zookeeper把服务暴露给外界使用。
首先,dubbo与zookeeper之间采用netty的NIO框架来进行通信的,并且对外提供服务,NIO的并发处理能力比较强,这个也是dubbo服务的性能好的主要原因。
首先我们可以编译dubbo-admin 程序,用来监控我们要发布的dubbo服务。编译完成后,把war报部署在tomcat里面,这里有个配置文件,WEB-INF 下面的dubbo.properties
写道
dubbo.registry.address=zookeeper://127.0.0.1:4180
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
注意修改自己的zookeeper集群地址
接下来,开发服务注册的agent
package com.mor.server.dubbo.service; public interface DemoServer { String sayHello(String str); }
/** * */ package com.mor.server.dubbo.service; import java.util.Date; public class DemoServerImpl implements DemoServer { /** * 返回添加后的语句 */ public String sayHello(String str) { str = "Hello " + str + " 2:" + new Date(); System.err.println("server:" + str); return str; } }
/** * */ package com.mor.main; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" }); context.start(); System.out.println("按任意键退出"); System.in.read(); } }
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.mor.maven</groupId>
<artifactId>dubboserver</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>dubboserver</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.4.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</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>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo</finalName>
<plugins>
<!-- 非多个资源配置 start-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
<failOnError>false</failOnError>
</configuration>
</plugin>
<!-- 非多个资源配置 end-->
</plugins>
</build>
</project>
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.mor.maven</groupId>
<artifactId>dubboserver</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>dubboserver</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.4.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</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>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo</finalName>
<plugins>
<!-- 非多个资源配置 start-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
<failOnError>false</failOnError>
</configuration>
</plugin>
<!-- 非多个资源配置 end-->
</plugins>
</build>
</project>
applicationProvider.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="hello-world-app" />
<!-- 本机 伪集群 测试 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182" />
<dubbo:protocol name="dubbo" port="20882" />
<dubbo:service interface="com.mor.server.dubbo.service.DemoServer"
ref="demoService" /> <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
</beans>
<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="hello-world-app" />
<!-- 本机 伪集群 测试 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182" />
<dubbo:protocol name="dubbo" port="20882" />
<dubbo:service interface="com.mor.server.dubbo.service.DemoServer"
ref="demoService" /> <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
</beans>
完成后运行,打开dubbo 的admin管理界面,可以看到服务的注册信息等
接着开发消费者
package com.mor.client.dubbo.action; import java.util.Date; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mor.server.dubbo.service.DemoServer; public class ChatAction { public void SayHello(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" }); context.start(); DemoServer demoServer = (DemoServer) context.getBean("demoService"); System.out.println("client:"+demoServer.sayHello("Wanggq"+"1:"+new Date())+"3:"+new Date()); } }
package com.mor.client.dubbo.main; import com.mor.client.dubbo.action.ChatAction; public class Main { public static void main(String[] args) throws InterruptedException { int i=0; while(i++<100){ ChatAction act = new ChatAction(); act.SayHello(); Thread.sleep(3000); } } }
写道
<?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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
</beans>
<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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
</beans>
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>demo-dubbo</groupId>
<artifactId>dubboclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubboclient</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.4.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</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>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo</finalName>
<plugins>
<!-- 非多个资源配置 start-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
<failOnError>false</failOnError>
</configuration>
</plugin>
<!-- 非多个资源配置 end-->
</plugins>
</build>
</project>
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>demo-dubbo</groupId>
<artifactId>dubboclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubboclient</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.4.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</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>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo</finalName>
<plugins>
<!-- 非多个资源配置 start-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
<failOnError>false</failOnError>
</configuration>
</plugin>
<!-- 非多个资源配置 end-->
</plugins>
</build>
</project>
运行测试就可以看到效果了。
相关推荐
Dubbo服务框架是一个由阿里巴巴开源的高性能Java RPC框架,它在阿里巴巴集团内部广泛使用,并且在开源社区中也获得了极大的关注。本资料包包含了Dubbo服务框架相关的面试题及答案,旨在帮助求职者或者开发者更好地...
【标题】"淘宝DUBBO服务框架1"与【描述】"taobao DUBBO服务框架 还有2"一起揭示了本次讨论的核心——淘宝的DUBBO服务框架。DUBBO是阿里巴巴开源的一个高性能、轻量级的服务治理框架,主要用于实现分布式服务的调用与...
在压缩包中的`dubbo-dubbo-3.2.11`文件夹内,包含了Dubbo框架的源代码,这对于学习和研究Dubbo的工作原理非常有帮助。源码阅读可以帮助我们理解Dubbo如何实现服务的注册、发现、调用等核心功能,以及其内部的设计...
《Dubbo服务框架 v2.6.12.zip——深入解析与应用》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的服务治理框架,自2008年诞生以来,便在分布式系统领域赢得了广泛的关注和应用。本次我们关注的是其v2.6.12版本,...
Dubbo服务框架是阿里巴巴开源的一...总的来说,"Dubbo服务框架 v2.7.22.zip"是一个全面的资源,涵盖了Dubbo框架的所有核心组件,无论你是想要学习、研究还是在实际项目中使用Dubbo,这个压缩包都将是你不可或缺的工具。
Dubbo服务框架是阿里巴巴开源的一款高性能、轻量级的Java RPC框架,主要用于构建微服务架构。版本v3.1.10带来了许多增强特性和优化,使得它在现代分布式系统中的应用更加广泛和稳定。这个压缩包包含了Dubbo 3.1.10的...
dubbo 服务框架高清视频 内含zookeeper&Linux;&redis;... dubbo 服务框架高清视频 内含zookeeper&Linux;&redis;...
《Dubbo服务框架面试专题及答案》文档是一个深入解析Dubbo服务框架的资源集合,它旨在帮助求职者或开发者充分准备与Dubbo相关的面试问题。Dubbo作为阿里巴巴开源的高性能、轻量级的服务治理框架,广泛应用于分布式...
【Dubbo服务框架面试专题及答案整理文档】 在IT行业中,Java编程语言一直是企业级应用开发的首选,而Dubbo作为阿里巴巴开源的一款高性能、轻量级的服务框架,它在Java分布式系统中的应用广泛。这份“Dubbo服务框架...
《Dubbo服务框架面试题及答案》 在Java面试中,Dubbo作为一款高可用、高性能的分布式服务框架,常常成为考察的重点。本资料集合了关于Dubbo的常见面试问题和对应答案,帮助求职者更好地理解和掌握这一核心技术。 1...
《Dubbo服务框架v2.7.9源码解析》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的服务框架,被广泛应用于分布式系统中,为开发者提供了强大的服务治理功能。这次我们关注的是其v2.7.9版本的源码,通过深入研究,...
taobao DUBBO服务框架 有1
【Dubbo服务框架面试题及答案】 Dubbo是一款由阿里巴巴开源的高性能、轻量级的服务框架,它主要致力于解决分布式系统的服务治理问题。在面试中,对于熟悉和掌握Dubbo的开发者来说,以下是一些常见且重要的面试知识...
dubbo框架有什么用 把一个大的项目拆分成多个项目,由原来的一个项目处理工作,变为多个项目一同处理工作,提高工作效率 dubbo框架和传统框架有什么区别 传统框架,无论部署多少份代码,每单个请求,都必须经过...
Dubbo是一个由阿里巴巴开源的高性能服务框架,它主要是为了解决分布式服务的调用问题。通过使用RPC(Remote Procedure Call,远程过程调用)技术,Dubbo使得应用可以方便地进行服务的输出和输入。Dubbo最大的特点是...
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring 框架无缝集成。 Dubbo主要核心部件: Remoting:网络通信框架,实现了sync-over-async和...
Dubbo是一个高性能、轻量级的Java开源RPC框架,它主要设计用于构建微服务架构,提供了服务自动注册、服务发现、服务治理等高级服务管理功能。Dubbo支持多种通信协议,每种协议都有其特定的应用场景和优缺点。 1. **...
《Dubbo框架源码解析深度探索》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的服务治理框架,已经成为Java世界中分布式服务的重要选择。本文将深入探讨Dubbo的核心概念、设计模式以及源码实现,旨在帮助读者理解...