官网:https://dubbo.apache.org/zh/
Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。
服务发现:
Dubbo 基于消费端的自动服务发现能力,其基本工作原理如下图:
在本片中笔者将用到zookeeper为注册中心实现远程调用,zookeeper的安装在另一篇文章中有相关的介绍。
创建父工程的主要目的是为了方便管理
3.创建dubbo-api (公共类接口)工程目录:
创建接口ApiService
public interface ApiService {
String test();
}
4.创建生产者(dubbo-provider)
工程目录
pom文件引入依赖
org.springframework.boot spring-boot-starter-parent 2.6.4 4.0.0 dubbo-provider1 org.springframework.boot spring-boot-starter-web 2.5.4 org.apache.dubbo dubbo-spring-boot-starter 2.7.1 org.apache.dubbo dubbo 2.7.1 org.apache.dubbo dubbo-dependencies-zookeeper 2.7.1 pom com.porridge.www dubbo-api 1.0-SNAPSHOT
application.yml
server:
port: 8001
dubbo:
application:
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181
DubboProviderApplication
//开启Dubbo远程调用
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
IndexServiceImpl
**注意:**这里的@Service注解是Dubbo包下的注解
@Service
public class IndexServiceImpl implements ApiService {
@Override
public String test() {
return "This is test()";
}
}
4.创建消费者(dubbo-consumer)
工程目录
pom
org.springframework.boot spring-boot-starter-parent 2.6.4 4.0.0 dubbo-comsumer org.springframework.boot spring-boot-starter-web 2.5.4 org.apache.dubbo dubbo-spring-boot-starter 2.7.1 org.apache.dubbo dubbo 2.7.1 org.apache.dubbo dubbo-dependencies-zookeeper 2.7.1 pom com.porridge.www dubbo-api 1.0-SNAPSHOT
application.yml
server:
port: 8002
dubbo:
application:
name: dubbo-comsumer
registry:
address: zookeeper://127.0.0.1:2181
DubboComsumerApplication
@SpringBootApplication
@EnableDubbo
public class DubboComsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboComsumerApplication.class, args);
}
}
IndexController
这里依赖注入使用@Reference
@RestController
public class IndexController {
@Reference
ApiService apiService;
@RequestMapping("/")
public String index(){
return apiService.test();
}
}
5.测试
访问接口localhost:8002



