生成dubbo使用实例,并分别给生产者消费者写yml版的配置文件
时间: 2024-03-15 22:45:41 浏览: 57
以下是一个使用Dubbo的示例:
假设我们有一个服务接口 `UserService`,其中有一个方法 `getUserById(long id)`,返回一个 `User` 对象。
```java
public interface UserService {
User getUserById(long id);
}
```
现在我们要使用Dubbo实现该接口,为了方便演示,我们将服务提供者和消费者都放在同一个项目中。
1. 定义服务提供者
```java
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUserById(long id) {
// 从数据库中查询用户信息
User user = new User();
user.setId(id);
user.setName("张三");
user.setAge(18);
return user;
}
}
```
2. 配置 Dubbo 服务提供者
```java
@Configuration
public class DubboProviderConfig {
@Value("${dubbo.application.name}")
private String applicationName;
@Value("${dubbo.registry.address}")
private String registryAddress;
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(applicationName);
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(registryAddress);
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
return protocolConfig;
}
@Bean
public ServiceConfig<UserService> userServiceServiceConfig() {
ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(UserService.class);
serviceConfig.setRef(new UserServiceImpl());
serviceConfig.setVersion("1.0.0");
return serviceConfig;
}
}
```
3. 配置 Dubbo 服务消费者
```java
@RestController
public class UserController {
@Reference(version = "1.0.0")
private UserService userService;
@GetMapping("/users/{id}")
public User getUserById(@PathVariable long id) {
return userService.getUserById(id);
}
}
@Configuration
public class DubboConsumerConfig {
@Value("${dubbo.application.name}")
private String applicationName;
@Value("${dubbo.registry.address}")
private String registryAddress;
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(applicationName);
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(registryAddress);
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
return protocolConfig;
}
@Bean
public ReferenceConfig<UserService> userServiceReferenceConfig() {
ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setInterface(UserService.class);
referenceConfig.setVersion("1.0.0");
return referenceConfig;
}
}
```
4. 配置文件
生产者的yml配置文件示例:
```yaml
spring:
dubbo:
application:
name: dubbo-provider #应用名称
registry:
address: zookeeper://localhost:2181 #注册中心地址
protocol:
name: dubbo #协议名称
port: 20880 #协议端口号
```
消费者的yml配置文件示例:
```yaml
spring:
dubbo:
application:
name: dubbo-consumer #应用名称
registry:
address: zookeeper://localhost:2181 #注册中心地址
protocol:
name: dubbo #协议名称
port: 20880 #协议端口号
```
阅读全文