写了好多博客,觉得应该以一种通俗的方式去阐述相关案例更贴切一些
dubbo是一款非常优秀的分布式开发框架,尤其是传统项目改造成分布式开发部署项目时,改造成本极低,可用性,一致性,容错性 较好
下面开始入门案例,本次案例以官方给出的dubbo-demo 作为入门案例,较为便捷,且 贴近实际生产。
dubbo官方网站:dubbo.io
学习dubbo最佳的方式就是参考官方网站,笔者极力推荐官网,有中文翻译,一键切换,各种详细配置均有提供,还有很多案例供学习参考 。
一、dubbo-demo 官方案例详解
dubbo-admin 源码下载地址:https://github.com/apache/dubbo/tree/dubbo-2.5.10
本文采用的版本为2.5.10
可以采用 git clone 或者以直接下载得到一个zip的压缩包,如下:
本次案例主要参考dubbo-demo做详细解答
1、项目结构如下:
dubbo-demo-api :存放接口
dubbo-demo-provider:具体实现,提供服务方
dubbo-demo-consumer:消费方,主要用于消费
1、dubbo-demo-api
父工程 pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.alibaba</groupId> <artifactId>dubbo-parent</artifactId> <version>2.5.10</version> </parent> <artifactId>dubbo-demo</artifactId> <packaging>pom</packaging> <name>${project.artifactId}</name> <description>The demo module of dubbo project</description> <properties> <skip_maven_deploy>true</skip_maven_deploy> </properties> <modules> <module>dubbo-demo-api</module> <module>dubbo-demo-provider</module> <module>dubbo-demo-consumer</module> </modules> </project>
dubbo-demo-api 工程的 pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo</artifactId> <version>2.5.10</version> </parent> <artifactId>dubbo-demo-api</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <description>The demo module of dubbo project</description> <properties> <skip_maven_deploy>true</skip_maven_deploy> </properties> </project>
package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); }
package com.alibaba.dubbo.demo;
public interface UserService {
String getUserName(Long id);
}
2、dubbo-demo-provider
dubbo-demo-provider 工程的pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo</artifactId> <version>2.5.10</version> </parent> <artifactId>dubbo-demo-provider</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <description>The demo provider module of dubbo project</description> <properties> <skip_maven_deploy>false</skip_maven_deploy> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> </dependencies> </project>
package com.alibaba.dubbo.demo.provider; import com.alibaba.dubbo.demo.DemoService; import com.alibaba.dubbo.rpc.RpcContext; import java.text.SimpleDateFormat; import java.util.Date; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); } }
package com.alibaba.dubbo.demo.provider; import java.util.Objects; import com.alibaba.dubbo.demo.UserService; public class UserServiceImpl implements UserService { @Override public String getUserName(Long id) { if (Objects.equals(0L, id)) { return "张三"; } if (Objects.equals(1L, id)) { return "李四"; } return "barnes"; } }
package com.alibaba.dubbo.demo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/dubbo-demo-provider.xml" }); context.start(); System.in.read(); // press any key to exit } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-provider"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> <bean id="userService" class="com.alibaba.dubbo.demo.provider.UserServiceImpl"/> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> <dubbo:service interface="com.alibaba.dubbo.demo.UserService" ref="userService"/> </beans>
3、dubbo-demo-consumer
dubbo-demo-consumer 工程的 pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo</artifactId> <version>2.5.10</version> </parent> <artifactId>dubbo-demo-consumer</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <description>The demo consumer module of dubbo project</description> <properties> <skip_maven_deploy>false</skip_maven_deploy> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.35.Final</version> </dependency> </dependencies> </project>
package com.alibaba.dubbo.demo.consumer; import com.alibaba.dubbo.demo.DemoService; import com.alibaba.dubbo.demo.UserService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) { System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/dubbo-demo-consumer.xml" }); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy UserService userService = (UserService) context.getBean("userService"); // get remote service proxy while (true) { try { Thread.sleep(2000); String hello = demoService.sayHello("world"); // call remote method System.out.println(hello); // get result String userName = userService.getUserName(1L); System.out.println("print out for: "+userName); } catch (Throwable throwable) { throwable.printStackTrace(); } } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService" /> <dubbo:reference id="userService" check="false" interface="com.alibaba.dubbo.demo.UserService" /> </beans>
provider 和 consumer一些公用配置如下
dubbo.properties
dubbo.application.qos.port=33333
log4j.properties
###set log levels### log4j.rootLogger=info, stdout ###output to 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
以上为项目基本代码,编写完成后开始启动项目
1、启动 zookeeper 默认配置 端口 2181
2、启动生产者:Provider.java
3、启动消费者:Consumer.java
观看控制台有一下内容打印出,则说明服务正常运行:
二、dubbo-admin 平台搭建及介绍
关于dubbo-admin 平台,笔者也是踩了好多坑,这里可以简单分享一下
dubbo-admin官方文档:http://dubbo.apache.org/zh-cn/docs/admin/introduction.html
当然按照官方给出的构建、启动dubbo-admin的方式也是可以的,获取的是最新的 dubbo-admin 平台
官方给出如下操作:
git clone https://github.com/apache/dubbo-admin.git cd dubbo-admin mvn clean package cd dubbo-admin-distribution/target java -jar dubbo-admin-0.1.jar
访问 http://localhost:8080
次方式获取的 dubbo-admin 平台是最新的 spring boot 项目,如果需要 war 包的方式获取dubbo-admin 则可以采取以下操作
1、下载对应的源码版本 https://github.com/apache/dubbo/tree/dubbo-2.5.10
2、切记如下操作是一个漫长的等待过程,笔者也曾失败过,不过反复操作,修改配置等重要获得的 war 包
cd dubbo-admin
mvn clean package
进入target 目录小有一个 dubbo-admin-2.5.10.war 文件即是我们所需的war 包,解压,修改文件文件夹名称为 dubbo-admin 放入 tomcat 目录下
3、启动 tomcat 访问 http://127.0.0.1:8080/dubbo-admin/ ,如下:
图为dubbo登录的首页 用户名密码默认为 root / root
图为dubbo 中已有注册的2个服务
图为dubbo-admin常用配置,服务启用、禁用,路由,负载均衡等
dubbo-admin 平台构建是一个漫长的过程,需耐心等待
dubbo 官方网站上对应配置,入门案例等等,都做了详细的介绍,实为学习dubbo的最佳平台。