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 ...
随机推荐
- 3.3.6-1 ArrayBlockingQueue简单分析
构造方法:public ArrayBlockingQueue(int capacity) { this(capacity, false); } public ArrayBlockingQueue(in ...
- mysql数据库引擎MyISAM与InnoDB的区别浅说
mysql的存储引擎包括:MyISAM.InnoDB.BDB.MEMORY.MERGE.EXAMPLE.NDBCluster.ARCHIVE.CSV.BLACKHOLE.FEDERATED等,其中In ...
- mysql建外键失败
1. 修改引擎:(设计表-->选项-->引擎:InnoDB) 2. 检查外键类型是否一致:
- Logstash 算术运算操作
需求:input为json,output为ES,需使用filter提取json中的某个字段,并执行加法.加法.乘法等算法操作 思路:mutate过滤器+ruby过滤器实现 避坑:根据ES及Logsta ...
- hdu1561之树形dp
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Rsync+Inotify实现文件自动同步
1>rsync概述 rsync的优点与不足 rsync与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求 ...
- [LeetCode 题解]:Gray Code
题目描述: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- Linq to SQL 中将数字转换为字符串
使用LINQ to Entities中的SqlFunctions调用数据库中的函数 添加引用System.Data.Entity 引用命名空间 using System.Data.Objects.Sq ...
- C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典
ConcurrentDictionary<Tkey,Tvalue> Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...
- Ubuntu16.04实用python脚本 - JDK的配置!
前提已经把Oracle JDK解压缩到指定目录了,我的JDK目录是:“/usr/jdk1.8.0_121” 全部脚本: # coding=utf-8 ,中文注释需要加入编码格式 #这是我的测试文件,在 ...