SpringBoot集成dubbo

生产者

SpringBoot集成dubbo_第1张图片


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

    
        com.alibaba.boot
        dubbo-spring-boot-starter
        0.2.1-SNAPSHOT
    

    
    
        com.alibaba
        dubbo
        2.6.5
    

    
    
        com.alibaba.spring
        spring-context-support
        1.0.2
    

    
    
        org.apache.curator
        curator-framework
        2.12.0
    


    
        org.springframework.boot
        spring-boot-starter-test
        test
    
public interface ProducerService {
    String sayHello(String name);
}
@Service//com.alibaba.dubbo.config.annotation.Service;
public class ProducerServiceImpl implements ProducerService {
    @Override
    public String sayHello(String name) {
        return "hello," + name;
    }
}

 

@SpringBootApplication
public class SpringBootProducerApplication {

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

}
dubbo:
  application:
    name: producer
    id: producer
  registry:
    address: zookeeper://127.0.0.1:2181
  scan:
    base-packages: com.example.producer.service
  protocol:
    port: 12345
    id: dubbo
    name: dubbo

消费者:

 

SpringBoot集成dubbo_第2张图片

@Service
public class ConsumerService {

    @Reference
    ProducerService producerService;

    public void sayHello() {
        String s = producerService.sayHello("world");
        System.out.println(s);
    }
}
dubbo:
  application:
    name: consumer
    id: consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    port: 12344
    id: dubbo-consumer
    name: dubbo-consumer
//注意路径要跟生产者一样
public interface ProducerService {
    String sayHello(String name);
}

pom同上

你可能感兴趣的:(springboot)