SpringBoot集成dubbo

生产者

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.1-SNAPSHOT</version>
    </dependency>

    <!-- Dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.5</version>
    </dependency>

    <!-- Spring Context Extras -->
    <dependency>
        <groupId>com.alibaba.spring</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>1.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
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

消费者:

 

@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同上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值