关于Dubbo~不介绍~不介绍~不介绍~(重要的事情要说三遍)
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓官网↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
准备:
zookeeper(下载地址:https://zookeeper.apache.org/releases.html#download);
四个项目(只要服务提供端和服务消费端也行后面说~):
(父项目)dubboparent
(接口)dubboapi
(服务提供端)dubboserver
(服务消费端)dubboclient
ヽ(  ̄д ̄;) 顺便一说,我用的是maven工具构建的项目
启动zookeeper :
下载 zookeeper 解压,把zoo_sample.cfg改名为zoo.cfg,因为Zookeeper在启动时会找这个文件作为默认配置文件;可以设置文件里的dataDir,设置为自己喜欢的输出路径~其他设置请找度娘~
设完后启动,为了好看直接用命令了
(linux是zkServer.sh,有点多余谁不知道?)
成功启动后,占用2181端口↓
构建项目:
~写项目~写项目~写项目~写项目 ヽ(  ̄д ̄;)
dubboparent /pom.xml (包括spring、dubbo、zookeeper的jar包)其他子项目需要依赖这货√
<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.night</groupId> <artifactId>dubboparent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>dubboparent</name> <url>http://maven.apache.org</url> <modules> <module>../dubboapi</module> <module>../dubboserver</module> <module>../dubboclient</module> </modules> <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> <dependencyManagement> <dependencies> <dependency> <groupId>com.night</groupId> <artifactId>dubboapi</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> <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.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> <distributionManagement> <repository> <id>releases</id> <name>Internal Releases</name> <url>http://192.168.1.233:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url>http://192.168.1.233:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> </project>
dubboapi:(dubboserver和dubboclient的依赖这货√的接口)
DemoService.java
package com.night.dubboapi.service; /** * * @author ReverieNight@Foxmail.com * */ public interface DemoService { String sayHi(String name); }
dubboserver :
DemoServiceImpl.java
package com.night.dubboserver.service; import java.text.SimpleDateFormat; import com.night.dubboapi.service.DemoService; /** * * @author ReverieNight@Foxmail.com * */ public class DemoServiceImpl implements DemoService { @Override public String sayHi(String name) { String s = "Server: Hi~ " + name + " time: " + new SimpleDateFormat("EEEE HH:mm:ss").format(System.currentTimeMillis()); System.out.println(s); return s; } }
Main.java
package com.night.dubboserver; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author ReverieNight@Foxmail.com * */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext("applicationProvider.xml"); content.start(); System.out.println("按任意键退出"); try { System.in.read(); //输入流阻塞,保持服务 } catch (IOException e) { e.printStackTrace(); } } }
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="dubboserver-hi" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.1.233:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.night.dubboapi.service.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.night.dubboserver.service.DemoServiceImpl" /> </beans>
dubboclient:
Main.java
package com.night.dubboclient; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.night.dubboapi.service.DemoService; /** * * @author ReverieNight@Foxmail.com * */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext("applicationConsumer.xml"); content.start(); DemoService demo = (DemoService) content.getBean("demoService"); String s = demo.sayHi("reverie"); System.out.println(s); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
applicationConsumer.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="dubboclient-hi" /> <dubbo:registry address="zookeeper://192.168.1.233:2181" /> <!-- 声明需要引用的服务接口 --> <dubbo:reference id="demoService" interface="com.night.dubboapi.service.DemoService" /> </beans>
运行:
启动顺序
zookeeper
服务端 dubboserver
消费端 dubboclient
运行结果
服务端:
消费端:
附:ヽ(  ̄д ̄;) 如果只想一个服务端一个消费端(客户端)的话,由于暴露和引用的接口必须一样,所以在消费端需要有一个于消费端暴露的接口一样的接口;可以写一个一模一样的也可以把服务端打包引入消费端。
-------------------------------------------------------华丽的分割线-----------------------------------------------------------
注解方式:
服务端spring配置文件加入:
<dubbo:annotation package="com.night.dubboserver.service" />
(扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 ← 官方原话)
消费端spring配置文件加入:
<dubbo:annotation package="com.night.dubboclient" />
相关推荐
dubbo+sping+zookeeper整合例子完整版(包括步骤文档和所有jar),有时间在把dubbo+spring boot+redis+zookeeper+seata+rabbitmq补上!
springcloudalibaba 微服务 sentinel案例 dubbo案例 nacos案例 zookeeper案例
ZooKeeper+dubbo+springMvc+Mybatis+Mysql实例,项目是由maven搭建的 整合Dubbo\spring\springMvc\Mybatis,整个压缩包中有两个项目分别是提供者和消费,启动方式是打成WAR形式放到tomcat中启动。
项目有maven搭建,整合dubbo+spring+springMvc+mybatis+Mysql,压缩包有两个项目分别是提供者和消费者两个项目,启动方式是打成WAR,在tomcat启动
这里是一个更加完整的实例,声明了三个生产者,一个api网关消费者,两个模块 api 模块和实体模块 https://gitee.com/under_the_sky/keepgoonDubbo/releases
Dubbo介绍(V2.6.0以上) Zookeeper介绍(V3.4.14) Spring-boot集成配置 实例演示
springmvc+spring+mybatis+dubbo+zookeeper+maven 服务端和消费端实例
这是Dubbo的例子,附件里面包括dubboDemoProvide和dubboDemoConsumer工程,此工程师maven工程,自动下载依赖的包,用eclipse导入即可运行,当然,前提是启动了zookeeper-3.4.5,至于如何启动zookeeper网上查资料。
springboot-dubbo Springboot和dubbo整合的一个项目Demo
使用springMVC + mysql +dubbo搭建的一个分布式简单例子,数据库大家可以自己创建一个,该用例可以直接运行,使用tomcat服务器运行即可
项目是由maven搭建的 整合Dubbo\spring\springMvc\Mybatis,整个压缩包中有两个项目分别是提供者和消费,启动方式是打成WAR形式放到tomcat中启动。
dubbo+spring+zookeeper集成入门实例,重点是熟悉dubbo的使用。
资源包括了三份资源: 1、Dubbo培训与实战.pptx 2、Dubbo实例代码(Sping+Dubbo+Maven).zip ...最后,该实例使用了zookeeper(此实例用zookeeper-3.4.5),至于如何启动zookeeper请看《Dubbo实战代码运行步骤.txt》。
Dubbo+Maven+ZooKeeper+Spring 的简单入门实例,有需要的朋友可以下载下。 myeclipse版本2017 CI 7 maven版本3.5.4 zookeeper版本3.4.13 dubbo2.5.5
基于Maven的dubbo集成spring简单实例.网上有不少例子均无法运行,只好自已整理一个可运行的例子.这是最简单可运行的例子,方便大家交流学习.在zookeeper-3.4.8和zookeeper-3.4.9下均可正常运行.
这是一个参考了网上许多人的代码后,自己拼拼凑凑整合起来的一套dubbo应用实例,包含了生产者和消费者。用到了zookeeper、spring、springMVC、mybatis、dubbo等技术。详细的说明请查看同名文章。
1,基于Spring XML配置管理dubbo的提供者与消费者 2,清晰的maven项目结构 3,使用sl4j+logback日志输出 4,google guava使用 5,dubbo消费者实例 6,多线程压测dubbo服务 7,dubbo的性能参数验证,比如timeout,...
Zookeeper 用于分布式 id 的生成;Redis 用于记录用户在线状态以及记录 Netty 节点的元数据;MySQL 对数据进行持久化。 ## 运行截图 #### 一对一聊天 ![](https://cdn.tojintao.cn/聊天测试1.PNG) #### 群聊 !...
配合spring以及用docker启动中间件做Java生态研究,测试(如:redis,ignite,任务,zookeeper,dubbo,kafka,rabbitmq等) 对常见技术的整合,以及前沿新技术的整合(如:kotlin,webflux) 启动说明 如果是不依赖...
Java客户端实现Kafka生产者与消费者实例 kafka的副本机制及选举原理剖析 基于kafka实现应用日志实时上报统计分析 RabbitMQ 初步认识RabbitMQ及高可用集群部署 详解RabbitMQ消息分发机制及主题消息分发 ...