为什么要用dubbo
- 服务治理框架
- 服务的监控
- 服务的注册发现
- 服务的通信
- 服务的容错
- 服务的负载均衡
dubbo支持的注册中心
- consul
- zookeeper
- eureka
- redis
- etcd
- nacos
- ....
创建 Demo
创建工程、在工程下创建三个模块、分别是 api、consumer、provider
provider & consumer 的 pom 依赖
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.2.1.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
先在 api 模块中新增一个接口
public interface DemoService {
String sayHello(String name);
}
后续在 provider 模块中实现该接口
@DubboService(version = "1.0.0")
public class DefaultDemoService implements DemoService {
/**
* The default value of ${dubbo.application.name} is ${spring.application.name}
*/
@Value("${dubbo.application.name}")
private String serviceName;
public String sayHello(String name) {
return String.format("[%s] : Hello, %s", serviceName, name);
}
}
编写 Spring Boot 启动类
@EnableDubbo
@SpringBootApplication
public class DubboProviderDemo {
public static void main(String[] args) {
SpringApplication.run(DubboProviderDemo.class,args);
}
}
在 resource 目录下新建 application.properties
spring.application.name=dubbo-auto-configuration-provider-demo
# 服务发布的协议
dubbo.protocol.name=dubbo
# dubbo 接收请求的端口
dubbo.protocol.port=12345
# 注册中心的协议和地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
启动 Zookeeper
./zkServer.sh start
./zkServer.sh status
查看 Zookeeper 上面的节点信息
获取并对其进行解码、存储的该接口的一些基本信息
dubbo://127.0.0.1:12345/com.demo.api.DemoService
?anyhost=true
&application=dubbo-auto-configuration-provider-demo
&deprecated=false
&dubbo=2.0.2
&dynamic=true
&generic=false
&interface=com.demo.api.DemoService
&methods=sayHello
&pid=78391
&release=2.7.7
&revision=1.0.0
&side=provider
×tamp=1642253598103
&version=1.0.0
编写消费端
@SpringBootApplication
public class DubboAutoConfigurationConsumerBootstrap {
@DubboReference(version = "1.0.0")
private DemoService demoService;
public static void main(String[] args) {
SpringApplication.run(DubboAutoConfigurationConsumerBootstrap.class).close();
}
@Bean
public ApplicationRunner runner() {
return new ApplicationRunner() {
public void run(ApplicationArguments args) throws Exception {
System.out.println(demoService.sayHello("mercyblitz"));
}
};
}
}
配置文件
spring.application.name=dubbo-auto-configure-consumer-sample
server.port=8090
dubbo.registry.address=zookeeper://127.0.0.1:2181
控制台输出
[dubbo-auto-configuration-provider-demo] : Hello, mercyblitz