还要添加swagger配置类:
package com.bolingcavalry.springbootzkconsumer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Tag; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .tags(new Tag("DemoController", "演示服务")) .select() // 当前包路径 .apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootzkconsumer.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("dubbo远程调用服务的操作(zk注册中心)") //创建人 .contact(new Contact("程序员欣宸", "http://github.com/zq2599/blog_demos", "zq2599@gmail.com")) //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }最后是启动类SpringBootZKConsumerApplication.java:
package com.bolingcavalry.springbootzkconsumer; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class SpringBootZKConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootZKConsumerApplication.class, args); } }至此,服务消费方编码完成,直接在IDEA上运行SpringBootZKConsumerApplication类即可启动,如下图,可见服务消费方也连接Zookeeper成功:
8. 通过浏览器访问swagger,地址是::8081/swagger-ui.html ,如下图,点击红框位置展开接口详情:
如下图,输入web接口参数发起请求:
10. 下图红框中就是响应的数据,内容是springbootzkconsumer远程调用springbootzkprovider的服务得到的: 查看Zookeeper上的数据
再回顾一下官方的有关Zookeeper注册中心中数据的描述,如下图:
按照上述信息,咱们去Zookeeper看看数据,验证和上图是否一致,登录Zookeeper后执行zkCli.sh进度交互模式;
查看根目录,果然有名为dubbo的子目录:
[zk: localhost:2181(CONNECTED) 18] ls / [dubbo, log_dir_event_notification, isr_change_notification, zookeeper, admin, consumers, cluster, config, latest_producer_id_block, controller, brokers, controller_epoch]再看dubbo目录下,有远程服务的子目录:
[zk: localhost:2181(CONNECTED) 19] ls /dubbo [com.bolingcavalry.dubbopractice.service.DemoService, config, metadata]查看远程服务的providers节点,果然有服务提供者的信息:
[zk: localhost:2181(CONNECTED) 16] get /dubbo/com.bolingcavalry.dubbopractice.service.DemoService/providers 169.254.38.208 cZxid = 0x552 ctime = Sat Oct 17 04:05:52 UTC 2020 mZxid = 0x552 mtime = Sat Oct 17 04:05:52 UTC 2020 pZxid = 0x5ec cversion = 27 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 1再看看consumer节点的内容:
[zk: localhost:2181(CONNECTED) 17] get /dubbo/com.bolingcavalry.dubbopractice.service.DemoService/consumers 169.254.38.208 cZxid = 0x55f ctime = Sat Oct 17 04:22:58 UTC 2020 mZxid = 0x55f mtime = Sat Oct 17 04:22:58 UTC 2020 pZxid = 0x5f0 cversion = 33 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 1可见Zookeeper上记录的数据与官方文档描述的一致;
至此,dubbo与springboot集成的实战就完成了,希望能给您提供一些参考,助您快速开发dubbo应用;
欢迎关注公众号:程序员欣宸微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
你不孤单,欣宸原创一路相伴Java系列
Spring系列
Docker系列
kubernetes系列
数据库+中间件系列
DevOps系列
欢迎关注公众号:程序员欣宸微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos