dubbo实战之三:使用Zookeeper注册中心 (3)

创建名为springbootzkconsumer的子工程,pom.xml内容如下,同样需要依赖dubbo-spring-boot-starter和dubbo-dependencies-zookeeper:

<?xml version="1.0" encoding="UTF-8"?> <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 "> <parent> <artifactId>dubbopractice</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.bolingcavalry</groupId> <artifactId>springbootzkconsumer</artifactId> <version>1.0-SNAPSHOT</version> <name>springbootzkconsumer</name> <description>Demo project for dubbo service consumer from Spring Boot</description> <!--不用spring-boot-starter-parent作为parent时的配置--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- swagger依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <dependency> <groupId>com.bolingcavalry</groupId> <artifactId>practiceinterface</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <type>pom</type> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${springboot.version}</version> </plugin> </plugins> </build> </project>

编写配置文件application.yml,注意dubbo.registry.address的值:

dubbo: application: #application-name 本模块名字 name: springboot-zk-consumer registry: address: zookeeper://192.168.50.43:2181 server: port: 8081

编写调用远程服务的代码,如下,可见如果想调用远程服务,只要对接口做@Reference注释即可,另外还通过timeout属性增加了超时配置:

package com.bolingcavalry.springbootzkconsumer.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 (zk registry center mode) : " + demoService.sayHello(name); } }

再编写对外提供web服务的Controller类:

package com.bolingcavalry.springbootzkconsumer.controller; import com.bolingcavalry.springbootzkconsumer.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); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssgwz.html