0
点赞
收藏
分享

微信扫一扫

springboot 集成dubbo

i奇异 2023-09-18 阅读 48

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>

  1. 配置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

  1. 定义服务接口和实现类

在spring Boot项目中,定义服务接口和实现类。例如:


 public interface GreetingService {  
 
     String sayHello(String name);  
 
 }  
 
   
 
 @Service  
 
 public class GreetingServiceImpl implements GreetingService {  
 
     @Override  
 
     public String sayHello(String name) {  
 
         return "Hello, " + name;  
 
     }  
 
 }

  1. 配置服务提供者和消费者

在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;  
 
     }  
 
 }

  1. 启动服务提供者和消费者
举报

相关推荐

0 条评论