Dubbo Apache Dubbo™ (incubating)是一款高性能Java RPC框架。 Spring Boot 应用场景的开发。同时也整合了 Spring Boot 特性:
自动装配 (比如: 注解驱动, 自动装配等).
Production-Ready (比如: 安全, 健康检查, 外部化配置等).
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
本例子用于展示dubbo的基本特性。
1、父pom:
xml version="1.0" encoding="UTF-8"?> <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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.4.RELEASEversion> <relativePath/> parent> <groupId>com.example.dubbogroupId> <artifactId>dubbodemoartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>pompackaging> <name>demo for dubboname> <description>The parent project of Demo project for Dubbodescription> <modules> <module>demo-apimodule> <module>demo-consumermodule> <module>demo-producermodule> modules> project>
2. 接口demo-api:
package com.example.dubbo.demo.service; import com.example.dubbo.demo.domain.DemoBean; /** * API */ public interface DemoService { /** * 说hello接口 * @param msg * @return */ String sayHelo(String msg); /** * 登陆接口 * @param demoBean * @return */ String login(DemoBean demoBean); }
package com.example.dubbo.demo.domain; import java.io.Serializable; public class DemoBean implements Serializable { private static final long serialVersionUID = 1L; private String userName; private String passwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } }
xml version="1.0" encoding="UTF-8"?> <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.0modelVersion> <parent> <groupId>com.example.dubbogroupId> <artifactId>dubbodemoartifactId> <version>0.0.1-SNAPSHOTversion> parent> <groupId>com.example.dubbogroupId> <artifactId>demo-apiartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>demo for apiname> <description>Demo project for Dubbo APIdescription> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <scope>compilescope> dependency> dependencies> <properties> <java.version>1.8java.version> properties> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> plugin> plugins> build> project>
3. 服务提供者demo-producer:
package com.example.dubbo.demoproducer.service; import com.example.dubbo.demo.domain.DemoBean; import com.example.dubbo.demo.service.DemoService; import org.apache.dubbo.config.annotation.Service; /** * 注册并提供服务 */ @Service public class DemoServiceImpl implements DemoService { @Override public String sayHelo(String msg) { return "hello " + msg; } @Override public String login(DemoBean demoBean) { if (demoBean == null || demoBean.getUserName() == null || demoBean.getPasswd() == null) { return "parameters error."; } else if(demoBean.getUserName().equals("wlf") && demoBean.getPasswd().equals("123")) { return "Welcome: " + demoBean.getUserName(); } return "Login failed."; } }
配置文件application.properties:
#端口配置 server.port=9188 #Dubbo服务生产者配置 spring.application.name=producer #组播注册中心 dubbo.registry.address=multicast://224.5.6.7:1234 #zk注册中心 #dubbo.registry.address=zookeeper://127.0.0.1:2181 #本地缓存文件 dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache #rpc通信协议 dubbo.protocol.name=dubbo #rpc通信端口 dubbo.protocol.port=1234 #用于服务暴露的扫描路径 dubbo.scan.base-packages=com.example.dubbo.demoproducer.service #服务监控 dubbo.monitor.protocol=registry
pom文件:
xml version="1.0" encoding="UTF-8"?> <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.0modelVersion> <parent> <groupId>com.example.dubbogroupId> <artifactId>dubbodemoartifactId> <version>0.0.1-SNAPSHOTversion> parent> <groupId>com.example.dubbogroupId> <artifactId>demo-producerartifactId> <version>0.0.1-SNAPSHOTversion> <name>demo-producername> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>2.7.1version> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubboartifactId> <version>2.7.1version> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-dependencies-zookeeperartifactId> <version>2.7.1version> <type>pomtype> dependency> <dependency> <groupId>com.example.dubbogroupId> <artifactId>demo-apiartifactId> <version>0.0.1-SNAPSHOTversion> <scope>compilescope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
3. 服务消费者demo-consumer:
package com.example.dubbo.democonsumer.controller; import com.example.dubbo.demo.domain.DemoBean; import com.example.dubbo.demo.service.DemoService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.*; /** * 服务消费者 */ @RestController public class ConsumerController { // 引入API @Reference(check = false) DemoService demoService; @ResponseBody @RequestMapping("/hello") public String sayHelo(@RequestParam(value = "msg") String msg) { return demoService.sayHelo(msg); } @ResponseBody @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(DemoBean demoBean) { return demoService.login(demoBean); } }
配置文件application.properties:
#端口配置 server.port=8182 #Dubbo服务消费者配置 spring.application.name=consumer #组播注册中心 dubbo.registry.address=multicast://224.5.6.7:1234 #zk注册中心 #dubbo.registry.address=zookeeper://127.0.0.1:2181 #本地缓存文件 dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache #监控中心 dubbo.monitor.protocol=registry
一个简单的index.html:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcometitle>
head>
<body>
<form method="post" action="/login">
UserName: <input type="text" name="userName" /><br/>
PassWord: <input type="text" name="passwd" /><br/>
<input type="submit" value="login">
form>
body>
html>
pom文件:
xml version="1.0" encoding="UTF-8"?> <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.0modelVersion> <parent> <groupId>com.example.dubbogroupId> <artifactId>dubbodemoartifactId> <version>0.0.1-SNAPSHOTversion> parent> <groupId>com.example.dubbogroupId> <artifactId>demo-consumerartifactId> <version>0.0.1-SNAPSHOTversion> <name>demo-consumername> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>2.7.1version> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubboartifactId> <version>2.7.1version> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-dependencies-zookeeperartifactId> <version>2.7.1version> <type>pomtype> dependency> <dependency> <groupId>com.example.dubbogroupId> <artifactId>demo-apiartifactId> <version>0.0.1-SNAPSHOTversion> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
把消费者和服务提供者跑起来,浏览器敲入:http://127.0.0.1:8182/hello?msg=world
如果继续敲入http://127.0.0.1:8182/index.html的话,会有一个弹出框:
用户名和密码分别敲入wlf和123,会跳出:
你可以本地起两个provider,不过必须先把1234端口跑起来,再跑另一个不同的端口(IDEA启动同一个项目的多实例参见IDEA同一项目启动多个实例):
#rpc通信端口
dubbo.protocol.port=1234
这个小例子默认使用组播作为注册中心,你也可以把组播的配置在application.properties中注释掉,改用zookeeper作为注册中心试试。当然了,这时需要你本地起一个zookeeper。另外在application.properties中配置了本地缓存,所以只要起过zookeeper进行服务注册后,我们可以关闭它,让dubbo根据本地缓存进行服务发现,自己路由到调用接口的路径。如果你想通过web界面看监控中心,进行服务治理,请参见官网,上面告诉你了怎么通过maven下载监控中心的源码、编译成jar包、以及如何启动内嵌web容器并登陆web界面。