1.添加Dubbo和Zookeeper依赖
在Spring Boot项目的pom.xml文件中,添加Dubbo和Zookeeper相关依赖。例如:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.13</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.13</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
- 配置Dubbo和Zookeeper
在Spring Boot项目的application.properties或application.yml文件中,添加Dubbo和Zookeeper相关配置。例如:
# Dubbo配置
dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# Zookeeper配置
zookeeper.connect=localhost:2181
- 定义服务接口和实现类
在spring Boot项目中,定义服务接口和实现类。例如:
public interface GreetingService {
String sayHello(String name);
}
@Service
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
- 配置服务提供者和消费者
在Spring Boot项目中,配置服务提供者和消费者。例如:
@Configuration
public class DubboConfig {
@Bean
public ServiceConfig<GreetingService> greetingServiceConfig() {
ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>();
serviceConfig.setApplication(new ApplicationConfig("dubbo-provider"));
serviceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));
serviceConfig.setInterface(GreetingService.class);
serviceConfig.setRef(new GreetingServiceImpl());
return serviceConfig;
}
}
- 启动服务提供者和消费者