编写配置文件application.yml,注意dubbo.registry.address的值,除了是广播模式,还要添加unicast=false,这样才能保证多个消费者进程都能收到广播:
dubbo: application: name: springboot-multicast-consumer id: springboot-multicast-consumer qosEnable: false registry: address: multicast://224.5.6.7:1234?unicast=false id: registry protocol: name: dubbo port: 20880 server: port: 8081编写调用远程服务的代码,如下,可见如果想调用远程服务,只要对接口做@Reference注释即可,另外还通过timeout属性增加了超时配置:
package com.bolingcavalry.springbootmulticastconsumer.service; import com.bolingcavalry.dubbopractice.service.DemoService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; @Service public class RemoteInvokeServiceImpl { @Reference(timeout = 2000) private DemoService demoService; public String sayHello(String name) { return "from dubbo remote (multicast mode) : " + demoService.sayHello(name); } }再编写对外提供web服务的Controller类:
package com.bolingcavalry.springbootmulticastconsumer.controller; import com.bolingcavalry.springbootmulticastconsumer.service.RemoteInvokeServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/demo") @Api(tags = {"DemoController"}) public class DemoController { @Autowired private RemoteInvokeServiceImpl remoteInvokeService; @ApiOperation(value = "获取dubbo service provider的响应", notes="\"获取dubbo service provider的响应") @ApiImplicitParam(name = "name", value = "昵称", paramType = "path", required = true, dataType = "String") @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String sayHello(@PathVariable String name){ return remoteInvokeService.sayHello(name); } }还要添加swagger配置类:
package com.bolingcavalry.springbootmulticastconsumer; 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.springbootmulticastconsumer.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("dubbo远程调用服务的操作(广播模式)") //创建人 .contact(new Contact("程序员欣宸", "http://github.com/zq2599/blog_demos", "zq2599@gmail.com")) //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }