美文网首页
Spring Boot配置Dubbo

Spring Boot配置Dubbo

作者: _52Hertz | 来源:发表于2019-09-26 15:38 被阅读0次

    搭建dubbo基础环境

    • 创建项目
    • 创建提供者和消费者两个module

    目录结构如下图所示

    父module:cateye_center_base
    子module:cateye_center_order cateye_center_user
    提供者:cateye_center_user
    消费者:cateye_center_order

    image.png

    提供者服务

    官网

    • pom.xml添加依赖
    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    
    • 配置文件application.properties
    spring.application.name=dubbo-spring-boot-starter
    spring.dubbo.server=true
    spring.dubbo.registry=N/A
    
    • Spring Boot Application的上添加@EnableDubboConfiguration
    package com.evan;
    
    import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @EnableDubboConfiguration
    public class CateyeCenterUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CateyeCenterUserApplication.class, args);
        }
    
    }
    
    • 编写dubbo服务
      ApiService
    package com.evan.dubbo.api;
    
    public interface ApiService {
        String sendMessage(String message);
    }
    

    ProviderServiceImpl

    package com.evan.dubbo.api.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.evan.dubbo.api.ApiService;
    import org.springframework.stereotype.Component;
    
    @Component
    @Service(interfaceClass = ApiService.class)
    public class ProviderServiceImpl implements ApiService {
    
        @Override
        public String sendMessage(String message) {
            return "provider:"+message;
        }
    }
    
    • 启动提供者服务


      image.png

    消费者服务

    • 配置文件application.properties
    spring.application.name=dubbo-spring-boot-starter
    
    • Spring Boot Application的上添加@EnableDubboConfiguration
    package com.evan;
    
    import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
    import com.evan.dubbo.api.impl.ConsumerServiceImpl;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    @EnableDubboConfiguration
    public class CateyeCenterOrderApplication {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(CateyeCenterOrderApplication.class, args);
    
            ConsumerServiceImpl consumerServiceImpl = (ConsumerServiceImpl) context.getBean("consumerServiceImpl");
            consumerServiceImpl.sendMessage("hello");
        }
    }
    
    • 编写dubbo服务,通过@Reference注入需要使用的interface
      ApiService
    package com.evan.dubbo.api;
    
    public interface ApiService {
        String sendMessage(String message);
    }
    

    ConsumerServiceImpl

    package com.evan.dubbo.api.impl;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.evan.dubbo.api.ApiService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ConsumerServiceImpl {
        private static final Logger logger = LoggerFactory.getLogger(ConsumerServiceImpl.class);
    
        @Reference(url = "dubbo://localhost:20880")
        ApiService apiService;
    
        public void sendMessage(String message){
            logger.info(apiService.sendMessage(message));
        }
    }
    
    • 启动消费者服务


      image.png

    集成注册中心

    前提是已经安装zookeeper并且启动

    • 添加zookeeper依赖
    <dependency>
          <groupId>com.101tec</groupId>
          <artifactId>zkclient</artifactId>
          <version>0.9</version>
    </dependency>
    <dependency>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
          <version>3.4.9</version>
          <type>pom</type>
    </dependency>
    
    • 修改提供者配置文件
      spring.dubbo.registry=N/A修改为spring.dubbo.registry=zookeeper://localhost:2181

    • 修改消费者配置文件
      添加spring.dubbo.registry=zookeeper://localhost:2181

    • 修改@Reference

    @Reference(interfaceClass = ApiService.class)
    

    相关文章

      网友评论

          本文标题:Spring Boot配置Dubbo

          本文链接:https://www.haomeiwen.com/subject/mvieyctx.html