dubbo+Springboot入门实例
2017-12-07 22:43
591 查看
dubbo介绍
dubbo是一套基于java的高可用的分布式服务框架。由阿里巴巴开发,并对外开源。和其他RPC框架一样,在服务端声明接口服务,并实现,同时将服务注册到服务注册中心(开源使用zookeeper作为注册中心);客户端声明与服务端一致的接口,然后调用即可。dubbo概念
provider:服务提供者,也就是声明服务接口,并暴露服务的一方consumer:服务消费者,也就是调用服务的一方
registry:服务注册中心,用于服务发现与注册,开源基于zookeeper
monitor:服务监控,用于dubbo服务调用的监控
container:运行容器,个人理解这不属于dubbo体系中的概念。是在paas类平台中声明的基础运行环境。
官方dubbo架构图:
1、安装zookeeper
从官网下载zookeeper服务器,并在本地运行。这里我们下载的是3.4.6版本。复制zoo_sample.cfg为zoo.cfg。zookeeper启动时默认加载zoo.cfg配置。zoo.cfg里面配置了数据文件、日志文件、心跳、端口等信息。进入zookeeper安装目录/bin,双击zkServer.cmd启动zookeeper。默认zookeeper运行在2181端口。2、创建dubbo服务provider
在idea中创建maven项目,并在pom.xml文件中引入springboot和dubbo依赖。<?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.0</modelVersion> <groupId>com.nmm.study</groupId> <artifactId>dubboprovider</artifactId> <version>1.0-SNAPSHOT</version> <!--引入springboot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入dubbot--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.7</version> </dependency> <!--引入zookeeper--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!--引入zookeeper客户端--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
声明服务接口,并实现。
package com.nmm.study.service; /** * @description 声明服务接口 * @date 2017/12/7 * @author Niemingming */ public interface DubboServiceProvider { //声明服务方法 public String sayHello(String name); }
package com.nmm.study.service.impl; import com.nmm.study.service.DubboServiceProvider; import org.springframework.stereotype.Component; /** * @description 实现接口服务 * @date 2017/12/7 * @author Niemingming */ @Component("dubboServiceProvider") public class DubboServiceProviderImpl implements DubboServiceProvider { @Override public String sayHello(String name) { return "hello " + name; } }
创建dubbo配置文件。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="dubboproviderhello" /> <!--配置服务注册中心,dubbo不仅仅支持zookeeper--> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!--声明对外暴露的服务--> <dubbo:service interface="com.nmm.study.service.DubboServiceProvider" ref="dubboServiceProvider" /> </beans>
创建服务启动类:
package com.nmm.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:/dubbo.xml") public class ProviderApplication { public static void main(String[] args){ SpringApplication.run(ProviderApplication.class,args); } }
3、dubbo客户端调用consumer
类似2,创建maven项目。在pom.xml文件中引入相同内容,同时创建与2一样的接口声明。在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="dubboproviderhello" /> <!--配置服务注册中心,dubbo不仅仅支持zookeeper--> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!--声明服务引用,与服务声明接口类型一致--> <dubbo:reference interface="com.nmm.study.service.DubboServiceProvider" id="dubboServiceProvider" /> </beans>
我们这里偷懒,在服务启动类中直接声明服务调用和注入。
package com.nmm.study; import com.nmm.study.service.DubboServiceProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @ImportResource("classpath:/dubbo.xml") @RestController public class ConsumerApplication { //注入声明的服务 @Autowired private DubboServiceProvider dubboServiceProvider; public static void main(String[] args){ SpringApplication.run(ConsumerApplication.class,args); } //测试服务调用 @ResponseBody @RequestMapping("/sayhello/{name}") public String hello(@PathVariable String name){ return dubboServiceProvider.sayHello(name); } }
启动服务,在浏览器中访问http://localhost:8082/sayhello/lisi查看结果如下:
可以看到已经调用成功。
代码放在github上地址为:
* dubboprovider地址
* dubboconsumer地址
参考资料:
*
* dubbo官网
* zookeeper下载地址
相关文章推荐
- IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例
- SpringBoot集成dubbo实例
- dubbo+spring+zookeeper的集成入门实例
- Spring Boot Dubbo 整合和入门理解
- SpringBoot 入门教程实例详解(一) 开发第一个SpringBoot应用程序实例
- spring-boot入门实例
- Spring-boot 入门实例+mybatis
- Spring Boot 集成Dubbo框架实例
- spring boot入门实例
- 一个资深程序员教你jvm,redis,spring boot,dubbo,tomcat入门到精通
- spring boot + jpa + kotlin入门实例
- spring boot + jpa + kotlin入门实例详解
- Spring Boot快速入门及实例
- dubbo整合springboot入门案例
- Spring boot入门实例 简易登录(精通Spring+4.x++企业应用开发实战 学习笔记二)
- Spring boot 入门 实例
- 【Spring Cloud】--Spring boot入门实例
- springBoot入门实例
- spring boot框架入门实例 笔记
- Spring boot 入门实例