在Dubbo中,服务暴露和服务引用是两个核心概念,分别对应服务提供者和服务消费者的操作。
1. 服务暴露(Service Exposure)
服务暴露是指服务提供者将其提供的服务注册到注册中心,使其能够被服务消费者发现和调用。
服务暴露的步骤:
- 定义服务接口:服务提供者和消费者都需要共享的接口。
- 实现服务接口:服务提供者提供具体的服务实现。
- 配置服务暴露:将服务实现通过Dubbo暴露出去。
代码示例
- 定义服务接口
package com.example;
public interface DemoService {
String sayHello(String name);
}
- 实现服务接口
package com.example;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
- 配置服务暴露
在Spring配置文件中:
<dubbo:application name="dubbo-demo-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.example.DemoService" ref="demoServiceImpl" />
或者在Spring Boot配置文件中:
dubbo:
application:
name: dubbo-demo-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.example
2. 服务引用(Service Reference)
服务引用是指服务消费者从注册中心获取服务提供者的地址信息,并通过Dubbo框架调用远程服务。
服务引用的步骤:
- 定义服务接口:服务提供者和消费者共享的接口。
- 引用远程服务:在消费者端配置服务引用。
- 调用远程服务:通过Dubbo框架调用远程服务。
代码示例
- 定义服务接口
(同上,服务接口由提供者和消费者共享)
- 引用远程服务
在Spring配置文件中:
<dubbo:application name="dubbo-demo-consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="demoService" interface="com.example.DemoService" />
或者在Spring Boot配置文件中:
dubbo:
application:
name: dubbo-demo-consumer
registry:
address: zookeeper://127.0.0.1:2181
scan:
base-packages: com.example
- 调用远程服务
package com.example;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceConsumer {
@DubboReference
private DemoService demoService;
public void execute() {
String message = demoService.sayHello("World");
System.out.println(message);
}
}
服务暴露和服务引用的工作原理
-
服务暴露:服务提供者在启动时,将其提供的服务注册到注册中心(如ZooKeeper)。注册中心保存了所有服务提供者的地址信息。
-
服务引用:服务消费者在启动时,从注册中心订阅所需的服务列表。注册中心返回所有可用的服务提供者信息。消费者通过Dubbo框架,利用负载均衡策略选择一个服务提供者进行调用。
综合示例
下面是一个完整的示例,展示了服务暴露和服务引用的过程。
服务提供者
- 服务接口
package com.example;
public interface DemoService {
String sayHello(String name);
}
- 服务实现
package com.example;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
- Spring Boot配置文件
dubbo:
application:
name: dubbo-demo-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.example
服务消费者
- 服务接口
(同上,服务接口由提供者和消费者共享)
- 引用远程服务
package com.example;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceConsumer {
@DubboReference
private DemoService demoService;
public void execute() {
String message = demoService.sayHello("World");
System.out.println(message);
}
}
- Spring Boot配置文件
dubbo:
application:
name: dubbo-demo-consumer
registry:
address: zookeeper://127.0.0.1:2181
scan:
base-packages: com.example
总结
服务暴露和服务引用是Dubbo的核心概念。服务暴露使得服务提供者可以将其服务注册到注册中心,供消费者发现和调用;服务引用使得服务消费者可以从注册中心获取服务提供者的信息,并通过Dubbo框架调用远程服务。通过上述代码示例,可以清晰地看到服务暴露和服务引用的具体操作。