原來dubbo發佈服務如此簡單

Dubbo是Alibaba開源的分佈式服務框架,它最大的特點是按照分業務的架構,使用這種方式可以使各個業務之間解耦合(或者最大限度地鬆耦合)。從服務模型的角度來看,Dubbo採用的是一種非常簡單的模型,要麼是提供方提供服務,要麼是消費方消費服務,所以基於這一點可以抽象出服務提供方(Provider)和服務消費方(Consumer)兩個角色。關於註冊中心、協議支持、服務監控等內容,詳見後面描述。spring-boot是近幾年越來越來越流行的庫,Spring Boot 可以大大提升使用 Spring 框架時的開發效率。spring-boot-starter-dubbo則是藉助spring-boot高效,整合dubbo的實現,讓dubbo的使用變得平民化。

快速入門

1.在maven管理的spring-boot項目中引入依賴,(建議使用spring-boot版本1.5以上,1.5以下未測試過)

    <dependency>
        <groupId>com.gitee.reger</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>${spring-boot-starter-dubbo.version}</version>
    </dependency>

2.在spring-boot項目的配置文件’application.yml’中增加dubbo的配置項

服務發佈者增加
spring:
  dubbo: 
    application:
      name: demo-provider
    base-package: com.test.dubbo.provider  # dubbo服務發佈者所在的包
    registry:
      address: 127.0.0.1                   # zookeeper註冊中心的地址
      port: 2181                           # zookeeper註冊中心的端口
    protocol:
      name: dubbo
      serialization: hessian2
    provider:
      retries: 0                           # 服務調用重試次數,服務發佈者不給重試,讓服務調用者自己重試
服務調用者增加
spring:
  dubbo: 
    application:
      name: demo-consumer
    base-package: com.test.dubbo.consumer  # dubbo服務調用者所在的包  
    registry:
      address: 127.0.0.1                   # zookeeper註冊中心的地址
      port: 2181                           # zookeeper註冊中心的端口
    consumer:
      timeout: 1000 
      check: true                          # 服務啓動時檢查被調用服務是否可用
      retries: 2                           # 服務調用重試次數 

3. 定義服務接口,

在api項目中增加接口

package com.test.dubbo.service;

public interface DemoService {
    Integer add(Integer a,Integer b);
}

4. 服務提供者

服務提供者項目中增加業務類

package com.test.dubbo.provider;
import com.test.dubbo.service.DemoService;
import com.alibaba.dubbo.config.annotation.Service;

@Service
public class DemoServiceImpl implements DemoService{

    public Integer add(Integer a,Integer b){
        System.err.printf("方法add被調用 %s+%s", a, b);
        System.err.println();
        if(a==null||b==null){
            return 0;
        }
        return a+b;
    }
}

5. 服務調用者

服務調用者項目中增加業務類

package com.test.dubbo.consumer;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.reger.dubbo.annotation.Inject;

import com.test.dubbo.service.DemoService;

@Component
public class DemoConsumer implements CommandLineRunner {

    // 使用兼容注入,可以使用dubbo原生註解@Reference注入
    @Inject DemoService service; 

    @Override
    public void run(String... args){  
        int a=1;
        int b =2;
        System.err.printf("%s+%s=%s", a, b, service.add(a,b));
        System.err.println(); 
    }
}

6.啓動服務提供者,啓動服務調用者。

服務提供者spring-boot的main方法的示例

package com.test.dubbo.main;

import java.util.concurrent.TimeUnit;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class SpringDubboConfigApplication implements CommandLineRunner {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(SpringDubboConfigApplication.class, args);
        TimeUnit.MINUTES.sleep(10); //提供者main線程暫停10分鐘等待被調用
        System.err.println("服務提供者------>>服務關閉");
    }

    @Override
    public void run(String... args) throws Exception {
        System.err.println("服務提供者------>>啓動完畢");
    } 
}

服務調用者spring-boot的main方法的類示例

package com.test.dubbo.main;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class SpringDubboConfigApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringDubboConfigApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.err.println("服務調用者------>>啓動完畢");
    }
}

相關資源

spring-boot-starter-dubbo-example碼雲地址
spring-boot-starter-dubbo 碼雲地址
dubbo官網
spring-boot

項目推薦

使用了後端通過jar包發佈的rpc協議庫,然後與前端app h5 微信交互使用restful api,你或許很有必要使用這個restful文檔插件spring-boot-starter-swagger

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章