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

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

com.bolingcavalry

practiceinterface

${project.version}

org.apache.dubbo

dubbo-dependencies-zookeeper

pom

org.projectlombok

lombok

org.apache.dubbo

dubbo-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

${springboot.version}

  1. 配置文件application.yml,要注意的是registry.address的值zookeeper://192.168.50.43:2181,这就是Zookeeper注册中心的配置:

dubbo:

application:

#application-name 本模块名字

name: springboot-zk-provider

registry:

address: zookeeper://192.168.50.43:2181

protocol:

name: dubbo

port: 20880

  1. 编写服务实现类DemoServiceImpl.java,注意@Service注解将当前类的实例作为远程服务对外暴露:

package com.bolingcavalry.springbootzkprovider;

import com.bolingcavalry.dubbopractice.service.DemoService;

import lombok.extern.slf4j.Slf4j;

import org.apache.dubbo.config.annotation.Service;

import org.apache.dubbo.rpc.RpcContext;

@Slf4j

@Service

public class DemoServiceImpl implements DemoService {

@Override

public String sayHello(String name) {

log.info("I’m springboot-zk-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

return "I’m springboot-zk-provider, Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();

}

}

  1. 编写SpringBoot启动类SpringBootZKProviderApplication.java,注意要添加@EnableDubbo注解:

package com.bolingcavalry.springbootzkprovider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class SpringBootZKProviderApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootZKProviderApplication.class, args);

}

}

  1. 至此服务提供方编码完成,直接在IDEA上运行SpringBootZKProviderApplication类即可启动服务,启动成功后的日志输出如下图,如红框所示,已连上了Zookeeper:

在这里插入图片描述

编码(服务消费方)

  • 现在网络上已经有了服务,咱们再来编写服用消费方的代码,一共要创建6个文件,创建顺序和功能如下表:

| 创建顺序 | 文件名 | 作用 |

| — | — | — |

| 1 | pom.xml | 工程的pom文件 |

| 2 | src/main/resources/application.yml | 配置文件 |

| 3 | RemoteInvokeServiceImpl.java | service层,在这里远程调用服务提供方的服务 |

| 4 | DemoController.java | web接口类,对外提供web服务 |

| 5 | SwaggerConfig.java | swagger配置类,便于通过页面测试接口 |

| 6 | SpringBootZKConsumerApplication.java | 启动类 |

  • 完整的文件位置如下图:

在这里插入图片描述

  • 接下来逐个创建上述文件;
  1. 创建名为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 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

dubbopractice

com.bolingcavalry

1.0-SNAPSHOT

4.0.0

com.bolingcavalry

springbootzkconsumer

1.0-SNAPSHOT

springbootzkconsumer

Demo project for dubbo service consumer from Spring Boot

org.springframework.boot

spring-boot-dependencies

2.3.3.RELEASE

pom

import

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

io.springfox

springfox-swagger2

io.springfox

springfox-swagger-ui

com.bolingcavalry

practiceinterface

${project.version}

org.apache.dubbo

dubbo-dependencies-zookeeper

pom

org.projectlombok

lombok

org.apache.dubbo

dubbo-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

${springboot.version}

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

dubbo:

application:

#application-name 本模块名字

name: springboot-zk-consumer

registry:

address: zookeeper://192.168.50.43:2181

server:

port: 8081

  1. 编写调用远程服务的代码,如下,可见如果想调用远程服务,只要对接口做@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);

}

}

  1. 再编写对外提供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);

}

}

  1. 还要添加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();

}

  • 26
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值