赞
踩
本地:springboot-2.2.6 + jdk1.8 + dubbo-2.7.3
服务器: docker + zookeeper:latest
我们先新建一个空项目,然后分别引入
三个模块,以下,贴出api模块构建过程:
1、新建空项目
2、创建api模块
我们默认先引入web依赖
3、创建customer和provider模块,过程同上,结构图如下:
api模块依赖不动,provider——>pom.xml:加入以下内容
<!--导入公共接口模块--> <dependency> <groupId>com.test</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
customer——>pom.xml添加同上
同样的api不用动,customer模块的配置如下:
#dubbo configuration
dubbo.application.name=consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://你的ip:2181
# provider使用8080
server.port=8081
provider模块:
dubbo.application.name=provider-ticket
dubbo.registry.address=zookeeper://你的ip:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 自动扫描
dubbo.scan.base-packages=com.test.provider.service
package com.test.api.service;
/**
* 公共接口
*/
public interface TicketService {
public String getTicket();
}
package com.test.provider.service; import com.alibaba.dubbo.config.annotation.Service; import com.test.api.service.TicketService; import org.springframework.stereotype.Component; @Component @Service(version = "1.0.0" ,interfaceClass = TicketService.class) // 将服务暴露 public class TicketServiceImpl implements TicketService{ @Override public String getTicket() { return "厉害了,我的国"; } } // 注意:@Service注解的import不是平时spring用的那个
package com.test.customer.controller; import com.test.api.service.TicketService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { // 获取服务 @Reference(version = "1.0.0") private TicketService ticketService; @GetMapping("/ticket") public String hello(){ String ticket = ticketService.getTicket(); System.out.println("买到票了" + ticket); return "success"; } }
http://localhost:8081/ticket
,显示如下:赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。