SpringBoot整合Dubbo方式之XML

,1,创建3个Maven模块,一个dubbo-server作为服务方,一个dubbo-consumer作为消费方,一个common-service作为公共的接口jar包,dubbo-server和dubbo-consumer都引入common-service的依赖。注意启动顺序,首先启动ZK,然后启动dubbo-server,最后启动dubbo-consumer

SpringBoot整合Dubbo方式之XML_第1张图片

项目文件层级如图:

SpringBoot整合Dubbo方式之XML_第2张图片

1,引入依赖,写springboot-test的pom.xml里面,

     
        org.springframework.boot
        spring-boot-starter-parent
        1.5.22.RELEASE
    


     
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
     



    
      com.alibaba
      dubbo
      2.5.9
    
    
    
      org.apache.zookeeper
      zookeeper
      3.4.6
      
        
          log4j
          log4j
        
      
    
    
    
      com.github.sgroschupf
      zkclient
      0.1
    
   

2,在common-service建立公共的接口,DemoService

public interface DemoService {
    public void test();
}

3,dubbo-server服务方配置

@SpringBootApplication
@ImportResource("dubbo-server.xml")
public class ServerApplication implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(ServerApplication.class);

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>[ClientApplication start success]<<<<<<<<<<<<<");
        log.info(">>>>>>>>>>>>>>>[ServerApplication start success]<<<<<<<<<<<<<");
    }

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}

dubbo配置文件dubbo-server.xml:




    
    

    
    

    
    

    
    
    
    

服务具体实现类:

import com.tiger.test.DemoService;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: tiger
 * @create: 2020-04-22
 */
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    public void test() {
        System.out.println("hello,我是服务方");
    }
}

4,dubbo-consumer 消费方配置:

@SpringBootApplication
@ImportResource("dubbo-consumer.xml")
public class ComsumerApplication implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(ComsumerApplication.class);

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>[ClientApplication start success]<<<<<<<<<<<<<");
        log.info(">>>>>>>>>>>>>>>[ComsumerApplication start success]<<<<<<<<<<<<<");
    }

    public static void main(String[] args) {
        SpringApplication.run(ComsumerApplication.class, args);
    }

}

dubbo配置文件dubbo-consumer.xml:





    
    
    
    
    
    

5,测试类:

@RestController
public class TestController {

    @Autowired(required = false)
    DemoService demoService;

    @RequestMapping("/test")
    public void test(){
        demoService.test();
    }
}

 

SpringBoot整合Dubbo方式之XML_第3张图片

选择合适的dubbo的版本,否则启动各种各样的报错。

你可能感兴趣的:(dubbo)