原创作者:田超凡(程序员田宝宝)
版权所有,转载请注明原作者,严禁复制转载
ittcf-dubbo-parent
--------------ittcf-dubbo-api
------------------------ittcf-dubbo-api-member
--------------ittcf-dubbo-api-member-impl
--------------ittcf-dubbo-api-order-impl
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.1.RELEASEversion> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <curator-framework.version>4.0.1curator-framework.version> <zookeeper.version>3.4.13zookeeper.version> <dubbo.starter.version>0.2.0dubbo.starter.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.alibaba.bootgroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>${dubbo.starter.version}version> dependency> <dependency> <groupId>org.apache.curatorgroupId> <artifactId>curator-frameworkartifactId> <version>${curator-framework.version}version> dependency> <dependency> <groupId>org.apache.zookeepergroupId> <artifactId>zookeeperartifactId> <version>${zookeeper.version}version> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> |
<dependencies> <dependency> <groupId>com.ittcfgroupId> <artifactId>ittcf-dubbo-api-memberartifactId> <version>0.0.1-SNAPSHOTversion> dependency> dependencies> |
server: port: 8080 dubbo: application: name: member protocol: name: dubbo port: 20880 registry: address: zookeeper://127.0.0.1:2181 scan: base-packages: com.ittcf.api.member.impl |
@Service public class MemberServiceImpl implements MemberService { @Override public String getUser() { return "订单服务调用会员服务接口"; } } |
注意:@Service 注解引入为阿里巴巴注解
@EnableDubbo @SpringBootApplication public class AppMember { public static void main(String[] args) { SpringApplication.run(AppMember.class, args); } } |
@EnableDubbo 开启Dubbo服务功能
<dependencies> <dependency> <groupId>com.ittcfgroupId> <artifactId>ittcf-dubbo-api-memberartifactId> <version>0.0.1-SNAPSHOTversion> dependency> dependencies> |
server: port: 8081 ###dubbo 注册服务名称 dubbo: application: name: order ###dubbo服务地址 protocol: name: dubbo port: 20880 registry: address: zookeeper://localhost:2181 consumer: timeout: 5000 |
@Reference private MemberService memberService; @RequestMapping("/orderToMember") public String orderToMember() { return memberService.getUser(); } |
使用该 @Reference调用dubbo接口
@EnableDubbo @SpringBootApplication public class AppOrder { public static void main(String[] args) { SpringApplication.run(AppOrder.class, args); } } |
本文部分素材转载自蚂蚁课堂