dubbo-start

API 配置方式

Define service interfaces

package org.apache.dubbo.samples.api;

public interface GreetingService {
    String sayHello(String name);
}

See api/GreetingService.java on GitHub.html

Implement service interface for the provider

package org.apache.dubbo.samples.provider;
 
import org.apache.dubbo.samples.api.GreetingService;
 
public class GreetingServiceImpl implements GreetingService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

See provider/GreetingServiceImpl.java on GitHub.java

Start service provider

package org.apache.dubbo.demo.provider;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.samples.api.GreetingService;

import java.io.IOException;
 
public class Application {

    public static void main(String[] args) throws IOException {
        ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<GreetingService>();
        serviceConfig.setApplication(new ApplicationConfig("first-dubbo-provider"));
        serviceConfig.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
        serviceConfig.setInterface(GreetingService.class);
        serviceConfig.setRef(new GreetingServiceImpl());
        serviceConfig.export();
        System.in.read();
    }
}

See provider/Application.java on GitHub.git

Build and run the provider

# mvn clean package
# mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.demo.provider.Application exec:java

Call remote service in consumer

package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.samples.api.GreetingService;

public class Application {
    public static void main(String[] args) {
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<GreetingService>();
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        referenceConfig.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
        referenceConfig.setInterface(GreetingService.class);
        GreetingService greetingService = referenceConfig.get();
        System.out.println(greetingService.sayHello("world"));
    }
}
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <dubbo.version>2.7.0</dubbo.version>
    <skip_maven_deploy>true</skip_maven_deploy>
</properties>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
        </exclusion>
    </exclusions>
</dependency>

API 配置

API 屬性與配置項一對一,各屬性含義,請參見:配置參考手冊,好比:ApplicationConfig.setName("xxx") 對應 <dubbo:application name="xxx" /> [1]github

服務提供者

import org.apache.dubbo.rpc.config.ApplicationConfig;
import org.apache.dubbo.rpc.config.RegistryConfig;
import org.apache.dubbo.rpc.config.ProviderConfig;
import org.apache.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;
 
// 服務實現
XxxService xxxService = new XxxServiceImpl();
 
// 當前應用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
 
// 鏈接註冊中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
 
// 服務提供者協議配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
 
// 注意:ServiceConfig爲重對象,內部封裝了與註冊中心的鏈接,以及開啓服務端口
 
// 服務提供者暴露服務配置
ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此實例很重,封裝了與註冊中心的鏈接,請自行緩存,不然可能形成內存和鏈接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多個註冊中心能夠用setRegistries()
service.setProtocol(protocol); // 多個協議能夠用setProtocols()
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");
 
// 暴露及註冊服務
service.export();

服務消費者

import org.apache.dubbo.rpc.config.ApplicationConfig;
import org.apache.dubbo.rpc.config.RegistryConfig;
import org.apache.dubbo.rpc.config.ConsumerConfig;
import org.apache.dubbo.rpc.config.ReferenceConfig;
import com.xxx.XxxService;
 
// 當前應用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
 
// 鏈接註冊中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
 
// 注意:ReferenceConfig爲重對象,內部封裝了與註冊中心的鏈接,以及與服務提供方的鏈接
 
// 引用遠程服務
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與註冊中心的鏈接以及與提供者的鏈接,請自行緩存,不然可能形成內存和鏈接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多個註冊中心能夠用setRegistries()
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
 
// 和本地bean同樣使用xxxService
XxxService xxxService = reference.get(); // 注意:此代理對象內部封裝了全部通信細節,對象較重,請緩存複用

特殊場景

下面只列出不一樣的地方,其它參見上面的寫法spring

方法級設置

...
 
// 方法級配置
List<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method.setName("createXxx");
method.setTimeout(10000);
method.setRetries(0);
methods.add(method);
 
// 引用遠程服務
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與註冊中心的鏈接以及與提供者的鏈接,請自行緩存,不然可能形成內存和鏈接泄漏
...
reference.setMethods(methods); // 設置方法級配置
 
...

點對點直連

...
 
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與註冊中心的鏈接以及與提供者的鏈接,請自行緩存,不然可能形成內存和鏈接泄漏
// 若是點對點直連,能夠用reference.setUrl()指定目標地址,設置url後將繞過註冊中心,
// 其中,協議對應provider.setProtocol()的值,端口對應provider.setPort()的值,
// 路徑對應service.setPath()的值,若是未設置path,缺省path爲接口名
reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService"); 
 
...

  1. API使用範圍說明:API 僅用於 OpenAPI, ESB, Test, Mock 等系統集成,普通服務提供方或消費方,請採用XML 配置方式使用 Dubbo ↩︎apache

-----------------------api

 

 基於 Spring 的 Schema 擴展 緩存

快速啓動

Dubbo 採用全 Spring 配置方式,透明化接入應用,對應用沒有任何 API 侵入,只需用 Spring 加載 Dubbo 的配置便可,Dubbo 基於 Spring 的 Schema 擴展 進行加載。app

若是不想使用 Spring 配置,能夠經過 API 的方式 進行調用。maven

服務提供者

完整安裝步驟,請參見:示例提供者安裝

定義服務接口

DemoService.java [1]

package org.apache.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}

在服務提供方實現接口

DemoServiceImpl.java [2]

package org.apache.dubbo.demo.provider;
 
import org.apache.dubbo.demo.DemoService;
 
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

用 Spring 配置聲明暴露服務

provider.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露服務地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 聲明須要暴露的服務接口 -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
 
    <!-- 和本地bean同樣實現服務 -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
</beans>

加載 Spring 配置

Provider.java:

import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
        context.start();
        System.in.read(); // 按任意鍵退出
    }
}

服務消費者

完整安裝步驟,請參見:示例消費者安裝

經過 Spring 配置引用遠程服務

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
    <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" />
</beans>

加載Spring配置,並調用遠程服務

Consumer.java [3]

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
 
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 獲取遠程服務代理
        String hello = demoService.sayHello("world"); // 執行遠程方法
        System.out.println( hello ); // 顯示調用結果
    }
}

  1. 該接口需單獨打包,在服務提供方和消費方共享 ↩︎

  2. 對服務消費方隱藏實現 ↩︎

  3. 也能夠使用 IoC 注入 ↩︎

 

 

 

http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

https://github.com/apache/incubator-dubbo

相關文章
相關標籤/搜索