这篇文章总结结构上的服务提供方,消费方demo,用于学习交流:
1.打开idea,新建一个项目,这里要用到聚合项目搭建,首先新建一个maven或者SpringBoot项目的外壳,
结构层次是:
<modules> <module>house-api</module> <module>house-provider</module> <module>house-consumer</module> </modules>
创建好maven聚合项目的外壳goss-house后,检查goss-house的pom文件,然后创建子项目,如图:
2.创建定义接口的项目,house-api,这个项目是接口的定义,创建为常规的maven项目,在后面的house-provider,house-consumer需要依赖到,
<groupId>com.yyds.house</groupId> <artifactId>house-api</artifactId> <version>0.0.1-SNAPSHOT</version>
3.house-provider 项目的模块项目创建成SpringBoot项目,这里要注意好各个模块项目的结构,包目录定义
创建好house-provider项目后,管理pom文件,增加项目说明,版本号定义,添加spring-boot关联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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yyds.house</groupId> <artifactId>house-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>house-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.yyds.house</groupId> <artifactId>house-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4.house-consumer的模块项目,也是创建成为SpringBoot项目,创建完成给house-consumer增加项目说明,版本号定义,用于编译打包的本地仓库;
<groupId>com.yyds.house</groupId> <artifactId>house-consumer</artifactId> <version>0.0.1-SNAPSHOT</version>
house-consumer的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yyds.house</groupId> <artifactId>house-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>house-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.yyds.house</groupId> <artifactId>house-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5.在house-provider的application.properties文件中添加dubbo的配置:
server.port=7004 spring.dubbo.application.name=house-provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 #spring.dubbo.scan=com.yyds.house.service spring.dubbo.scan=com.yyds.house.houseprovider
6.在house-consumer的application.properties文件中添加dubbo的配置:
server.port=7003 #dubbo config spring.dubbo.application.name=house-consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.scan=com.yyds.house.consumer
7.在house-api项目中,分别定义 AddressInfoApi, HouseInfoApi , house-provider依赖 house-api,实现具体的业务, house-consumer 依赖 house-provider 进行消费调用;
8.各个模块编译代码完成:
1-编译 install house-api,
2-house-provider刷新maven依赖,house-consumer刷新maven依赖;
3-启动house-provider
4-启动house-consumer, 根据端口号,地址访问消费;http://localhost:7003/qHouse?houseNo=9098
地址服务消费:
9.在dubb-admin 可以查看到对应服务发布信息:
注意:
1-先启动zookeeper服务
2-启动提供方 house-provider
3-启动消费方 house-consumer
4-进行服务调用
5-启动dubb-admin监控服务,查看提供方,消费方的服务情况