W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
示例使用 Spring XML 配置方式進行演示。
除此之外,Dubbo Java 還提供了包括注解、API、配置文件、spring boot等多種啟動與接入方式,具體可參見配置章節(jié)具體描述。
示例代碼在 dubbo-samples 中
$ git clone -b master https://github.com/apache/dubbo-samples.git
$ cd dubbo-samples/dubbo-samples-basic
在 dubbo-samples-basic 目錄
$ mvn clean package -Pprovider
$ java -jar ./target/provider.jar
$ mvn clean package -Pconsumer
$ java -jar ./target/consumer.jar
DemoService.java
package org.apache.dubbo.samples.basic.api;
public interface DemoService {
String sayHello(String name);
}
DemoServiceImpl.java
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name +
", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
provider.xml:
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
consumer.xml:
<dubbo:reference id="demoService" check="true" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
Consumer.java
public static void main(String[] args) {
...
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("world");
System.out.println(hello);
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: