Dubbo的Api+Provider+Customer示例(IDEA+Maven+Springboot+dubbo)
项目结构
dubbo-demo
dubbo-api:提供api接口,一般存储实体类和接口服务
dubbo-provider:dubbo生产者提供服务,一般存储接口具体实现
dubbo-customer:dubbo消费者使用服务,分发请求
一、新建项目dubbo-demo
1、新建Maven项目,File-->new-->Project,选择Maven
2、填充GroupId、ArtifactId和Version
3、一路Next,最后选择项目名称(之前创建过dubbo的项目,这里重命名为dubbo_demo)
4、生成项目,修改pom.xml(不修改也不影响子模块),删除src
二、添加基于SpringBoot的三个子模块dubbo-api、dubbo-provider、dubbo-customer
1、添加子模块dubbo-api,项目右键-->new-->Module
最后生成的dubbo-api 如下:
2、以相同的方式创建dubbo-provider和dubbo-customer如下:
三、各个模块添加代码示例
1、dubbo-api
phone(手机实体类):
package com.mirana.api.model; import java.io.Serializable;
import java.math.BigDecimal; /**
* Title:手机 实体类
*
* @CreatedBy Mirana
* @DateTime 2018/5/25 8:43
*/
public class Phone implements Serializable { // 主键id
private Long id;
// 手机名称
private String name;
// 价格
private BigDecimal price; public Long getId () {
return id;
} public void setId (Long id) {
this.id = id;
} public String getName () {
return name;
} public void setName (String name) {
this.name = name;
} public BigDecimal getPrice () {
return price;
} public void setPrice (BigDecimal price) {
this.price = price;
} }
IPhoneService手机接口
package com.mirana.api.service; import com.mirana.api.model.Phone; /**
* Title:手机服务接口
*
* @CreatedBy Mirana
* @DateTime 2018/5/25 8:45
*/
public interface IPhoneService { /**
* 根据id查找手机
*
* @param id
* @return
*/
Phone findById (Long id);
}
在application.properties中定义端口
server.port=9011
2、dubbo-provider
pom.xml添加dubbo-api(具体实现类要实现dubbo-api的IPhoneService接口)和Apache Dubbo的Maven依赖,红色部分为新增依赖
<?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.mirana.provider</groupId>
<artifactId>dubbo-provider</artifactId>
<version>v1.1</version>
<packaging>jar</packaging> <name>dubbo-provider</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 新增dubbo-api依赖 -->
<dependency>
<groupId>com.mirana.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>v1.1</version>
</dependency> <!-- 新增dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<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>
添加实现类PhoneServiceImpl,@service注解是dubbo的注解,而非Spring的注解
package com.mirana.provider.service; import com.alibaba.dubbo.config.annotation.Service;
import com.mirana.api.model.Phone;
import com.mirana.api.service.IPhoneService; import java.math.BigDecimal; /**
* Title:手机服务service
*
* @CreatedBy Mirana
* @DateTime 2018/5/25 10:32
*/
@Service(version = "1.0.0")
public class PhoneServiceImpl implements IPhoneService { /**
* 根据id查找手机
*
* @param id
* @return
*/
@Override
public Phone findById (Long id) {
Phone phone = new Phone();
phone.setId(id);
phone.setName("iphonex");
phone.setPrice(BigDecimal.valueOf(8848.88));
return phone;
}
}
新增dubbo服务提供者的配置文件:dubbo-provider.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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="demo-provider" owner="demo_mirana" organization="dubbo_mirana"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用 dubbo 协议实现定义好的 Service Api 接口-->
<dubbo:service interface="com.mirana.api.service.IPhoneService" ref="phoneService" protocol="dubbo"/>
<!--具体实现该接口的 bean-->
<bean id="phoneService" class="com.mirana.provider.service.PhoneServiceImpl"/>
</beans>
在SpringBoot的启动类DubboProviderApplication上导入dubbo-provider.xml的配置
package com.mirana.provider; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@ImportResource(value = {"classpath:dubbo-provider.xml"})
public class DubboProviderApplication { public static void main (String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
在application.properties中定义端口
server.port=9012
3、dubbo-customer
1、pom.xml,与provider相似,添加dubbo-api和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.mirana.customer</groupId>
<artifactId>dubbo-customer</artifactId>
<version>v1.1</version>
<packaging>jar</packaging> <name>dubbo-customer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- 新增dubbo-api -->
<dependency>
<groupId>com.mirana.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>v1.1</version>
</dependency> <!-- 新增dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<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>
2、添加视图控制器PhoneController
package com.mirana.customer.controller; import com.mirana.api.model.Phone;
import com.mirana.api.service.IPhoneService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* Title:TODO
*
* @CreatedBy Mirana
* @DateTime 2018/5/25 10:59
*/
@RestController
@RequestMapping("/phone")
public class PhoneController { @Resource
private IPhoneService phoneService; /**
* @return
*/
@RequestMapping("/findById")
public Phone findById (Long id) {
return phoneService.findById(id);
} }
3、添加dubbo服务消费者的配置文件dubbo-customer.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="demo-consumer" owner="mirana" organization="dubbo_mirana"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper"/>
<!--使用 dubbo 协议调用定义好的 api 接口-->
<dubbo:reference id="phoneService" interface="com.mirana.api.service.IPhoneService"/> </beans>
4、在SpringBoot的启动类DubboCustomerApplication上导入dubbo-customer.xml的配置
package com.mirana.customer; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@ImportResource(value = {"classpath:dubbo-customer.xml"})
public class DubboCustomerApplication { public static void main (String[] args) {
SpringApplication.run(DubboCustomerApplication.class, args);
}
}
在application.properties中定义端口
server.port=9013
三、请求服务消费者的地址
查找id为3的手机:http://localhost:9013/phone/findById?id=3,返回的json数据如图
三、运行程序,查看dubbo-admin
1、先运行dubbo-api
运行DubboApiApplication的main( )方法
2、再运行dubbo-provider
运行DubboProviderApplication的main( )方法
3、最后运行dubbo-customer
运行DubboCustomerApplication的main( )方法
4、查看dubbo-admin的服务
http://localhost:8080/dubbo/,输入用户名root密码root
4.1提供的服务
4.2 服务提供者
4.3 服务消费者
Dubbo的Api+Provider+Customer示例(IDEA+Maven+Springboot+dubbo)的更多相关文章
- Dubbo与Zookeeper、Spring整合使用 maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat | - ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(一)
这几天项目还没来,所以就自己试着参考网上的一些资料,搭建了一个SpringBoot+dubbo+zookeeper+maven框架,网上参考的很多资料照着他们一步一步搭建,最后很多都运行不通,很是郁闷 ...
- springboot+dubbo+zookeeper+mybatis
参考地址:https://www.cnblogs.com/gaopengfirst/p/9555240.html 首先创建一个maven项目: 再在该父项目中创建3个module,分别是:provid ...
- dubbo+zookeeper下生产者和消费者配置(基于springboot开发)
一.总共分为三个目录: dubbo-api 服务的接口用于对接客户端和服务端 dubbo-client 客户端配置文件为:consumer.xml dubbo-service 服务 ...
- SpringBoot&Dubbo&Zookeeper远程调用项目搭建
序言 Dubbo一款分布式服务框架,作为阿里巴巴SOA服务化治理方案的核心框架,通过高性能和透明化的RPC实现服务的远程调用,对服务的负载均衡以及项目的耦合性提供很强的解决方式;具体Dubbo的介绍和 ...
- dubbo+zookeeper+jsp+springmvc+spring+mybatis+mysql+maven完整示例
项目分为三部分,这里分为三个maven项目(基于web,所以最后一个为maven创建的web项目) 1.接口定义以及实体类定义(api+pojo) --- maven创建java项目,打包成jar 2 ...
- IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例
概述 首先声明,本文是学习总结类型的博客内容,如有雷同纯属学习.本位主要结合zookeeper和dubbo做个简单实例.目前来说,一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越 ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(二)
上一篇文章是关于搭建SpringBoot+dubbo+zookeeper+maven框架的,但是里面的功能还不够完善,今天就日志管理方面做一些改善. 下了demo的网友可能会发现项目在启动时会有警告: ...
- MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建
: 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程 API Pom.xml:不用引用任何东西 Provider Pom.xml:要denpend ...
随机推荐
- [GO]使用select实现斐波那契
package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...
- 关于Qt官方下载页的最新变动
时间过得很快,现在Qt已经迎来了5.10版本,但是当我们去下载页下载对应安装包的时候,已经找不到之前的offline安装包了.你能够看到的只有在线安装包,并且我自己有做过测试,国内的网络基本上没有机会 ...
- C++对象在内存中的布局
(1)C++对象模型 (2)单一继承无虚函数 (3)单一继承有虚函数 (4)多重继承 (5)虚拟继承(vc++) (6)虚拟继承(g++) 参考:<深度探索C++对象模型>
- delphi将工程文件转成dll
1.点击[File]—>[New]—>[Other]菜单项,打开[New Items],选择[New]: 2.选择[Dll Wizard]选项卡,点击ok,DLL工程创建成功. 3.点击[ ...
- [LeetCode 题解]: Interger to Roman
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an i ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- winfrom 右下角弹窗(渐渐消失)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 从FastDFS官网下载我们需要的安装包
第一步:大家在浏览器地址栏输入:https://sourceforge.net/projects/fastdfs/files,我们会看到如下图所示界面. 第二步:下载FastDFS的Server安装包 ...
- OCP 12c最新考试原题及答案(071-5)
5.(4-12) choose two: You executed the following CREATE TABLE statement that resulted in an error: SQ ...
- “全栈2019”Java第二十二章:控制流程语句中的决策语句if-else
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...