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 服务消费者
分类: Java