springboot 整合dubbo最佳實踐 (使用redis做爲註冊中心)

一 概述
 


       Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠程方法調用,智能容錯和負載均衡,以及服務自動註冊和發現。 

  • 面向接口代理的高性能RPC調用

    提供高性能的基於代理的遠程調用能力,服務以接口爲粒度,爲開發者屏蔽遠程調用底層細節。web

  • 智能負載均衡

    內置多種負載均衡策略,智能感知下游節點健康情況,顯著減小調用延遲,提升系統吞吐量。redis

  • 服務自動註冊與發現

    支持多種註冊中心服務,服務實例上下線實時感知。spring

  • 高度可擴展能力

    遵循微內核+插件的設計原則,全部核心能力如Protocol、Transport、Serialization被設計爲擴展點,平等對待內置實現和第三方實現。api

  • 運行期流量調度

    內置條件、腳本等路由策略,經過配置不一樣的路由規則,輕鬆實現灰度發佈,同機房優先等功能。app

  • 可視化的服務治理與運維

    提供豐富服務治理、運維工具:隨時查詢服務元數據、服務健康狀態及調用統計,實時下發路由策略、調整配置參數。負載均衡

二 整合dubbo 

1 建立公共服務接口module  :  wj-dubbo-api

  

 

定義公共接口:   SpringService框架

package example.service;

/**
 * @author: wangjun
 * @create: 2018/8/7
 **/
public interface SpringService {

  /**
   * dubbo調用測試
   * @return
   */
  String dubboExample();
}

 

2 建立服務提供方項目  wj-dubbo-server 

    添加依賴:運維

<dependencies>
    <!--dubbo 公共接口依賴-->
    <dependency>
      <groupId>com.wj.project</groupId>
      <artifactId>wj-dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <!--dubbo 依賴-->
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>0.1.0</version>
      <!--<version>0.2.0</version>-->
    </dependency>

    <!--使用redis作註冊中心-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

  </dependencies>

   添加dubbo 配置類 ide

package com.wj.project.server.common.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;



/**
 * @author: wangjun
 * @create: 2018/8/7
 **/
@Configuration
@DubboComponentScan("com.wj.project.server.example") // 掃描 Dubbo 組件  
public class ProviderConfiguration {

  /**
   * 當前應用配置
   */
  @Bean("dubbo-annotation-provider")
  public ApplicationConfig applicationConfig() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("wj-dubbo-server");
    return applicationConfig;
  }

  /**
   * 當前鏈接註冊中心配置
   */
  @Bean("my-registry")
  public RegistryConfig registryConfig() {
    RegistryConfig registryConfig = new RegistryConfig();
//    registryConfig.setAddress("zookeeper://localhost:2181");
    registryConfig.setAddress("redis://localhost:6379");
    return registryConfig;
  }

  /**
   * 當前鏈接註冊中心配置
   */
  @Bean("dubbo")
  public ProtocolConfig protocolConfig() {
    ProtocolConfig protocolConfig = new ProtocolConfig();
    protocolConfig.setName("dubbo");
    protocolConfig.setPort(12345);
    return protocolConfig;
  }
}

定義服務實現類:spring-boot

package com.wj.project.server.example.service;

import com.alibaba.dubbo.config.annotation.Service;

import example.service.SpringService;

/**
 * @author: wangjun
 * @create: 2018/8/7
 **/
@Service
public class SpringServiceImpl implements SpringService {

  @Override
  public String dubboExample() {
    return "hello world ,hello dubbo";
  }
}

3 建立服務消費方項目 :wj-dubbo-consumer、

   添加依賴:

<dependencies>

    <!--dubbo 公共接口依賴-->
    <dependency>
      <groupId>com.wj.project</groupId>
      <artifactId>wj-dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <!--dubbo 依賴-->
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>0.1.0</version>
    </dependency>

    <!--使用redis作註冊中心-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

  </dependencies>

添加消費方配置:

package com.wj.project.consumer.common.dubbo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.wj.project.consumer.example.service.SpringConsumerService;

/**
 * @author: wangjun
 * @create: 2018/8/7
 **/
@Configuration
@DubboComponentScan  //默認掃描當前包及子包
public class ConsumerConfiguration {

  /**
   * 當前應用配置
   */
  @Bean
  public ApplicationConfig applicationConfig() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("wj-dubbo-consumer");
    return applicationConfig;
  }

  /**
   * 當前鏈接註冊中心配置
   */
  @Bean
  public RegistryConfig registryConfig() {
    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress("redis://localhost:6379");
    return registryConfig;
  }

  /**
   * 僅供測試時使用,非測試時可註釋
   * <p>
   * 註冊 AnnotationDemoServiceConsumer,@DubboComponentScan 將處理其中 @Reference 字段。
   * 若是 AnnotationDemoServiceConsumer 非 Spring Bean 的話,
   * 即便 @DubboComponentScan 指定 package 也不會進行處理,與 Spring @Autowired 同理
   */
  @Bean
  public SpringConsumerService SpringConsumerService() {
    return new SpringConsumerService();
  }
}

添加消費service:

@Service
public class SpringConsumerService {

  @Reference(url = "dubbo://10.9.26.208:12345")
  private SpringService springService;

  public String dubboExample() {
    return springService.dubboExample();
  }
}

4 添加測試類

測試前需啓動 服務方項目

package com.wj.project.consumer.example.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.wj.project.consumer.common.dubbo.ConsumerConfiguration;
import com.wj.project.consumer.example.service.SpringConsumerService;

/**
 * @author: wangjun
 * @create: 2018/8/7
 **/
public class ConsumerBootstrap {

  public static void main(String[] args) {
    // 啓動而且返回服務消費方上下文
    ApplicationContext consumerContext = startConsumerContext();
    // 獲取 AnnotationDemoServiceConsumer Bean
    SpringConsumerService consumer = consumerContext.getBean(SpringConsumerService.class);
    // 執行 doSayHello 方法
    String message = consumer.dubboExample();
    // 輸出執行結果
    System.out.println(message);
  }

  /**
   * 啓動而且返回服務消費方上下文
   *
   * @return AnnotationConfigApplicationContext
   */
  private static ApplicationContext startConsumerContext() {
    // 建立服務消費方 Annotation 配置上下文
    AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
    // 註冊服務消費方配置 Bean
    consumerContext.register(ConsumerConfiguration.class);
    // 啓動服務消費方上下文
    consumerContext.refresh();
    // 返回服務消費方 Annotation 配置上下文
    return consumerContext;
  }
}

5 結果

相關文章
相關標籤/搜索