spring-boot-starter-dubbox

<h1>前言</h1>

當今微服務架構發展非常迅速,應用也比較廣泛,涌現出一批非常優秀的微服務架構,比如NetflixSpring Cloud 當當網的dubbox是應用最多的兩個框架,項目也傾向于SpringBoot來構建。

我們項目準備應用Dubbox作用微服務框架,選址它處于以下兩點考慮

  • rpc性能上的提升主要是增加了序列化的處理(內部項目應用之間的調用rpc性能比較理想)。

  • 同時也支持Rest。

<h1>spring-boot-starter-dubbox</h1>

spring-boot-starter-dubbox,將springBoot與dubbox實現集成實現形成一個dubbo、Rest服務框架。

目前所有的項目都是通過SpringBoot來創建的大大簡化了之前的XML操作,當時就想能不能把dubbox形成一個組件可以插拔化用的時候只需要引入包加上啟動注解@EnableDubbox即可使用,當時第一反應就上看看GitHub有木有做個這個事情,然后并沒有,但是找到了阿里同事對dubbo做了這樣的一個事情https://github.com/alibaba/spring-boot-starter-dubbo,所以決定在上面做擴展主要支持Rest。spring-boot-starter-dubbox代碼需要的同學我會上傳GitHub或者以微信形式問我306570696。

<h1>如何發布dubbox服務</h1>

How to publish dubbo

  • add Dependencies:
    <dependency>
        <groupId>com.euond</groupId>
        <artifactId>spring-boot-starter-dubbox</artifactId>
        <version>3.0.1-SNAPSHOT</version>
    </dependency>
  • add dubbo configuration in application.properties, demo:
server:
  port: 7001
spring:
  http:
    encoding:
      charset: UTF-8
      enabled: true
  dubbo:
    appname: spring-boot-starter-dubbo-test
    registry: zookeeper://127.0.0.1:2181
    version: 1.0.0
    dubbox-dubbo:   #dubbo服務
      protocol: dubbo
      port: 20801
    dubbox-rest:    #rest服務
      server: tomcat
      port: 8889
      protocol: rest
    dubbox-webservice:  #webservice服務
      protocol: webservice
      port: 8080
      server: jetty
  • then add @EnableDubboConfiguration on Spring Boot Application, indicates that dubbo is enabled.(web or non-web application can use dubbo provider)
@SpringBootApplication
@EnableDubboConfiguration
public class DubboProviderLauncher {
  //...
}
  • code your dubbo service, add @Service(import com.alibaba.dubbo.config.annotation.Service) on your service class, and interfaceClass is the interface which will be published.
@Service(interfaceClass = IHelloService.class)
public class HelloServiceImpl implements IHelloService {
  //...
}

@Service(interfaceClass = AnotherUserRestService.class,protocol="rest")
@Component
public class AnotherUserRestServiceImpl implements AnotherUserRestService {

    @Override
    public String getString(Long id) {
        // TODO Auto-generated method stub
        return "Holle,Word";
    }

    @Override
    public String getTest() {
        // TODO Auto-generated method stub
        return "Holle,Word";
    }

}
  • start Spring Boot.

How to consume Dubbo

  • add Dependencies:
    <dependency>
        <groupId>com.euond</groupId>
        <artifactId>spring-boot-starter-dubbox</artifactId>
        <version>3.0.1-SNAPSHOT</version>
    </dependency>
  • add dubbo configuration in application.properties, demo:
server:
  port: 7001
spring:
  http:
    encoding:
      charset: UTF-8
      enabled: true
  dubbo:
    appname: spring-boot-starter-dubbo-test
    registry: zookeeper://127.0.0.1:2181
    version: 1.0.0
    dubbox-dubbo:   #dubbo服務
      protocol: dubbo
      
  • then add @EnableDubboConfiguration on Spring Boot Application
@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerLauncher {
  //...
}
  • injection interface by the @DubboConsumer annotation.
@Component
public class HelloConsumer {
  @DubboConsumer
  private IHelloService iHelloService;

}

/**
 * 
 * @author tan.bin
 * 非dubbo的消費端調用dubbo的REST服務(non-dubbo --> dubbo)
 */
@RestController
public class DemoNoDubooRestHello {
    private static final Logger logger = LoggerFactory.getLogger(DemoNoDubooRestHello.class);
    @RequestMapping("/hello2.json")
    public Map<String,Object> restMap(){
        Map<String,Object> restMap = new HashMap<String,Object>();
        Map<String,String> restParams =new HashMap<String,String>();
        restParams.put("name", "tan.bin");
        String url="http://localhost:8889/rest-api/service1/person";
        String results="";
        try {
            results=HttpUtils.get(url, restParams);
            logger.info("請求成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        restMap.put("results", results);
        return restMap;
    }
}

/**
 * 
 * @author tan.bin
 * dubbo消費端調用dubbo的REST服務 (dubbo --> dubbo)
 */
@RestController
public class DemoRestHello {
    
    @DubboConsumer(version="1.0.0")
    IHelloDubboRestService helloDubboRestService;
    
    @RequestMapping("/hello1.json")
    public Map<String,Object> restMap(){
        Map<String,Object> restMap = new HashMap<String,Object>();
        String results=helloDubboRestService.hello("tan.bin");
        restMap.put("results", results);
        return restMap;
    }
}

Reference

https://github.com/yefengyueyuan/spring-boot-starter-dubbox

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容