第四步:服务消费方dubbo-customer相关配置

在pom.xml文件中添加dubbo使用依赖,以及zookeeper的依赖:
SpringBoot-Dubbo
com.ly
0.0.1-SNAPSHOT
4.0.0
com.ly
dubbo-customer
0.0.1-SNAPSHOT
dubbo-customer
org.springframework.boot
spring-boot-starter-web
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
org.apache.zookeeper
zookeeper
3.4.13
org.slf4j
slf4j-log4j12
log4j
log4j
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
修改配置文件:application.yml
server:
port: 8082 #Tomcat端口号
dubbo:
application:
name: dubbo-customer #应用名称
registry:
address: zookeeper://127.0.0.1:2181 #zookeeper服务的地址以及端口号
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
第五步:在dubbo-api中新建接口,并在dubbo-provider中实现

在dubbo-api中创建一个接口:HelloService,使用时需要在dubbo-provider以及dubbo-customer
中添加依赖:
public interface HelloService {
String hello();
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
在dubbo-provider中实现HelloService接口:

创建接口实现类HelloServiceImpl用来实现HelloService接口:实现代码如下,注意这里使用的注解@Service不是用spring的service注解,而是使用的dubbo的注解:import com.alibaba.dubbo.config.annotation.Service;
@Service //发布服务
public class HelloServiceImpl implements HelloService {
@Override
public String hello() {
return "Hello World...";
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
并在启动器DubboProviderApplication中添加注解:@EnableDubbo

第六步:试着在dubbo-customer中远程调用HelloService接口:

新建一个HelloController类,代码如下,想要引入远程服务时需要使用dubbo的Reference注解:
@RestController
public class HelloController {
@Reference //引用服务
private HelloService helloService;
@GetMapping("/hello")
private String hello(){
System.out.println("Hello World...");
return helloService.hello();
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
测试代码,先启动服务提供方:dubbo-provider,发生错误,因为我们还没有启动zookeeper

下载安装zookeeper,打开连接选择合适的版本直接安装即可:https://zookeeper.apache.org/releases.html


下载下来的压缩包可以在linux或windows中使用,这里以windows为例:

解压压缩包,找到bin目录并打开:
目录如下,找到zkServer.cmd,双击启动即可:

运行结果如下:

再次依次启动:服务提供发:dubbo-provider
,服务消费方:dubbo-customer
,打开浏览器进行测试,结果如下:

更多Java学习指南以及最新项目场景题,需要的宝子
Java学习包传送门
data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/weixin_42924914/article/details/145458291","extend1":"pc","ab":"new"}">>
评论记录:
回复评论: