1.整合 dubbo
有的人或许会说已经有 spring-cloud 了,你整合 dubbo 干什么,其实没啥意图,主要就是想整合一下,毕竟 dubbo 在国内使用的还是很多的,你会一点点总不至于让你显得那么尴尬。
创建 spring boot 父模块,并在其 pom.xml 添加依赖:
<properties> <spring-boot.version>2.1.6.RELEASE</spring-boot.version> <dubbo.version>2.7.3</dubbo.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 因为使用的是 zookeeper 作为注册中心,所以要添加 zookeeper 依赖 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> </dependencies>
添加依赖需要注意的地方:
- 使用 idea 等工具生成的骨架可能或者继承于 spring-boot-starter-parent 这个项目,也就是在 parent 标签下添加 spring-boot-starter-parent 那么恭喜你可能会因为版本冲突而启动不起来,所以在上面添加依赖时把 spring-boot-starter-parent 添加到 dependencyManagement 里面。
- 使用 zookeeper 作为注册中心时记得添加相关的依赖,使用 nacos 或者其他的可以自行度娘即可。
- 我创建的项目里没有使用 tomcat 而是使用了 jetty,喜欢使用 tomcat 的自行添加即可,切勿照搬!
2.创建提供者 (provider) 和消费者 (consumer) 模块
利用 ide 工具创建好相应的模块,让提供模块和消费者模块都继承于前面创建的父模块。
假设我们都已经创建好了消费者和提供者模块,为了方便我们将消费者模块命名为 consumer,提供者模块命名为 provider ,然后分别添加配置文件和启动器
provider 模块配置文件和启动器
配置文件 —— application.yml:
server: port: 8081 spring: application: name: springboot-dubbo-provider dubbo: scan: # 包名根据自己的实际情况写 base-packages: com.wxp.service protocol: port: 20881 name: dubbo registry: address: 127.0.0.1:2181 # 注册到 zookeeper ,上面地址是 zookeeper 的默认地址 protocol: zookeeper # 假如使用新的 dubbo-admin 的话记得添加元数据配置,这样可以在 dubbo-admin 用来调试 provider 的服务 metadata-report: address: zookeeper://127.0.0.1:2181
启动器 —— ProviderApplication.java
// 包名添加启动器类所在的包名 package com.wxp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
创建 provider 服务
创建 DemoSevice 接口类(为了方便我们可以将接口类创建为一个公共的模块,让 provider 和 consumer)都依赖于该模块。
package com.wxp.service;; public interface DemoService { String sayHello(String name); }
创建 DemoService 实现类 DemoServiceImpl
package com.wxp.service.impl; import com.wxp.service.DemoService; import org.apache.dubbo.config.annotation.Service; // dubbo 的注解,不要和 spring 的弄混了 @Service(version = "1.0.0") public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
这样差不多就可以了,可以启动该模块试试。启动前记得先启动 zookeeper,假如有 dubbo-admin 的话应该会看到提供者模块服务已经启动,可以在dubbo-admin 进行测试。
consumer 模块配置文件和启动器
配置文件 —— application.yml
server: port: 8082 spring: application: name: springboot-dubbo-consumer dubbo: scan: base-packages: com.wxp.controller registry: protocol: zookeeper address: 127.0.0.1:2181 consumer: # check 为 false 的话那么 consumer 启动时不会去检查 provider 启动与否 check: false timeout: 60000
启动器 —— ConsumerApplication.java
package com.wxp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
创建控制器类HelloController测试上面创建的服务
package com.wxp.controller; import com.wxp.service.DemoService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { // dubbo 的 @Reference 注解,不要和 spring 的注解弄混了 @Reference(version = "1.0.0") private DemoService demoService; @GetMapping("/hello/index") public String index() { return demoService.sayHello("wxp"); } }
测试
分别启动 provider 和 consumer ,在浏览器输入 http://localhost:8082/hello/index ,应该可以看到浏览器会输出 Hello wxp 字样
结语
第一次在 猿圈 发帖,主要是记录下自己使用的方法,不然怕忘记了,暂时还是小白一枚,望大神提点!
© 著作权归作者所有
发表评论