spring-boot-starter-dubbox

<h1>前言</h1>

當(dāng)今微服務(wù)架構(gòu)發(fā)展非常迅速,應(yīng)用也比較廣泛,涌現(xiàn)出一批非常優(yōu)秀的微服務(wù)架構(gòu),比如NetflixSpring Cloud 當(dāng)當(dāng)網(wǎng)的dubbox是應(yīng)用最多的兩個(gè)框架,項(xiàng)目也傾向于SpringBoot來構(gòu)建。

我們項(xiàng)目準(zhǔn)備應(yīng)用Dubbox作用微服務(wù)框架,選址它處于以下兩點(diǎn)考慮

  • rpc性能上的提升主要是增加了序列化的處理(內(nèi)部項(xiàng)目應(yīng)用之間的調(diào)用rpc性能比較理想)。

  • 同時(shí)也支持Rest。

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

spring-boot-starter-dubbox,將springBoot與dubbox實(shí)現(xiàn)集成實(shí)現(xiàn)形成一個(gè)dubbo、Rest服務(wù)框架。

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

<h1>如何發(fā)布dubbox服務(wù)</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服務(wù)
      protocol: dubbo
      port: 20801
    dubbox-rest:    #rest服務(wù)
      server: tomcat
      port: 8889
      protocol: rest
    dubbox-webservice:  #webservice服務(wù)
      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服務(wù)
      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的消費(fèi)端調(diào)用dubbo的REST服務(wù)(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("請(qǐng)求成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        restMap.put("results", results);
        return restMap;
    }
}

/**
 * 
 * @author tan.bin
 * dubbo消費(fèi)端調(diào)用dubbo的REST服務(wù) (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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容