SpringBoot中集成DubboDubbo 实现服务注册和发现
用于理解和学习Dubbo工作原理、和集成方法
版本说明
-
Spring boot版本
2.3.9.RELEASE
-
dubbo-spring-boot-starter版本
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
- zookeeper版本
3.8.1
<!--ZooKeeper客户端-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
- Dubbo版本
2.7.15
Dubbo入门知识
- 基本架构
- 参考文档地址:
https://cn.dubbo.apache.org/zh-cn/overview/home/
https://zhuanlan.zhihu.com/p/529704258
https://developer.aliyun.com/article/808571
Dubbo测试项目
生产者工程 dubbo-provider
- 1.Springboot主类上增加注解
@EnableDubboConfig
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfig
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- 2.创建OderService接口
package com.stu.dubbo.order.service;
public interface OderService {
/**
* 查询订单信息
* @param orderId 订单Id
* @return
*/
String getOrderInfo(String orderId);
}
- 3.创建OderService实现类,在需要提供出去的服务类上增加
@DubboService(version = "1.0.0")
注解
package com.stu.dubbo.order.service.impl;
import com.stu.dubbo.order.service.OderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@DubboService(version = "1.0.0")
public class OderServiceImpl implements OderService {
private static Logger logger = LoggerFactory.getLogger(OderServiceImpl.class);
@Override
public String getOrderInfo(String orderId) {
logger.info("orderId:{}",orderId);
return String.format("getOrderInfo: %s", orderId);
}
}
- 4.配置文件
application.properties
说明
server.port=8090
spring.application.name=dubbo-provider-order
#dubbo服务扫描接口路径
dubbo.scan.base-packages=com.stu.dubbo.order.service
## Dubbo Registry
dubbo.registry.address=zookeeper://localhost:2181
#日志配置
logging.level.root=info
logging.level.com.stu=debug
消费者工程 dubbo-consumer
- 1.Springboot主类上增加注解
@EnableDubboConfig
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfig
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- 1.Springboot主类上增加注解
@EnableDubboConfig
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfig
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
-
2.引入
OderService
服务,包名定义需要和dubbo-provider保持一致,实际开发中可以把接口抽出,放到单独的api服务 -
3.创建测试类
HelloController
,注意@DubboReference(version = "1.0.0")
注解是必须
package com.stu.dubbo.user.controller;
import com.stu.dubbo.order.service.OderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dubbo")
public class HelloController {
@DubboReference(version = "1.0.0")
private OderService oderService;
@GetMapping("/oder")
public String hello(String orderId) {
return oderService.getOrderInfo(orderId);
}
}
启动项目验证接口
-
启动生产者工程。
-
启动消费者工程,启动之前生产工程需要正常启动并注册,否则消费者会报错。
-
服务都启动之后,zookeeper中数据状态如下,可以看到生产者和消费者都注册到zk上了。
- 调用接口测试,结果可以正常访问
其他说明
标签:集成,Dubbo,SpringBoot,dubbo,boot,import,org,public,String From: https://www.cnblogs.com/jishuwu/p/17292820.html实验和测试的时候,应该注意 dubbo-spring-boot-starter、zookeeper、Bubbo服务版本,否则项目会无法正常启动