SpringBoot2.0之整合Dubbo
Dubbo支持协议
Dubbo支持dubbo、rmi、hessian、http、webservice、thrift、redis等多种协议,但是Dubbo官网是推荐我们使用Dubbo协议的。
Spring Cloud 项目feign客户端 继承依赖方式实现重构项目
jar: 打成jar包
pom:依赖被被人继承的 公共依赖 字符模块
war:达成一个web项目 里面包含jar包、WEB-INF
toov5-dubbo-parent pom
toov5-public-api-service pom 提供会员接口 jar类型 pom项目没有Java代码的哦
toov5-api-member-service
toov5-api-member-service-impl 会员接口实现类(这个不是公开的) (点击parent创建的,需要引入 实现的interface )
toov5-dubbo-order-web jar(springboot) 订单项目 调用会员项目 ps:订单调用会员时候 引入公共接口 底层调用dubbo协议 帮助实现 *(dubbo原理 拿到类的class的地址信息,反射。)
parent引入共同依赖:
<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>toov5-dubbo-parent </groupId> <artifactId>dubbo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <curator-framework.version>4.0.1</curator-framework.version> <zookeeper.version>3.4.13</zookeeper.version> <dubbo.starter.version>0.2.0</dubbo.starter.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.starter.version}</version> </dependency> <!-- <dependency 在上面已经实现好了> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator-framework.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <modules> <module>toov5-api-member-service-impl</module> <module>toov5-public-api-service</module> </modules> </project>
点击parent 创建 maven pom :toov5-public-api-service (公共接口maven model) 点击 toov5-public-api-service 创建 toov5-api-member-service jar (写接口 interface)
点击parent 创建 maven jar : toov5-api-member-service-impl 并且 引入: toov5-api-member-service 的依赖 dependency
接口:
package com.toov.api.member.service; public interface MemberService { public String getUser(); }
实现类:
package com.toov.api.member.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.toov.api.member.service.MemberService; @Service public class MemberServiceImpl implements MemberService { public String getUser() { System.out.println("订单服务调用会员服务"); return "订单服务调用会员服务成功"; } }
启动类:
package com.toov.api.member.service.impl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @EnableDubbo @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
yml:
server: port: 8080 dubbo: application: name: member #表示采用的dubbo协议 protocol: name: dubbo #发布的端口号 port: 20880 registry: address: zookeeper://192.168.91.5:2181
PS:如果需要写版本号的 springboot没有帮你整合 如果不需要写版本号 已经帮忙整合了
Dubbo发布时候 采用注解方式! 使用 @Service注解 进行发布服务
记得采用 Dubbo 而不是 spring 的注解!
区别: spring 提供的 是注入到Spring容器中 dubbo是注册到注册中心去!
扫的实现类 注册到dubbo 注册中心中去
启动效果:
点击parent 创建 toov5-dubbo-order-web jar 然后引入接口依赖!
package toov5.order.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.toov.api.member.service.MemberService; @RestController public class OrderController { @Reference //dubbo提供的 而不是@Autowired private MemberService memberService; @RequestMapping("/orde") public String orderToMember() { return memberService.getUser(); } }
启动类:
package toov5.order.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @EnableDubbo @SpringBootApplication public class AppOrder { public static void main(String[] args) { SpringApplication.run(AppOrder.class, args); } }
yml
server: port: 8082 #tomcat端口号 ###dubbo 注册服务名称 dubbo: application: name: order registry: address: zookeeper://192.168.91.5:2181 consumer: #调用服务的超时时间 timeout: 5000
运行并且访问:
大家可以多启动几个 发布服务端口 然后 玩玩 就集群效果 我就不做展示了