- 提供接口 与接口实现
package com.wfs.samples.echo.api;
public interface EchoService {
String echo(String msg);
}
package com.alibaba.dubbo.samples.echo.impl;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.samples.echo.api.EchoService;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author yiji@apache.org
*/
public class EchoServiceImpl implements EchoService {
public String echo(String message) {
String now = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("[" + now + "] Hello " + message
+ ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return message;
}
}
- 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务提供方应用名称, 方便用于依赖跟踪 -->
<dubbo:application name="echo-provider"/>
<!-- 使用本地zookeeper作为注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 只用Dubbo协议并且指定监听端口 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 通过xml方式配置为bean, 让spring托管和实例化 -->
<bean id="echoService" class="com.wfs.samples.echo.impl.EchoServiceImpl"/>
<!-- 声明服务暴露的接口,并暴露服务 -->
<dubbo:service interface="com.wfs.samples.echo.api.EchoService" ref="echoService"/>
</beans>
- 服务注册
package com.wfs.samples.echo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class EchoProvicer {
public static void main(String[] args) throws IOException {
//指定暴露配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("echo-provider.xml");
//启动Spring容器并暴露服务
context.start();
System.in.read();
}
}
-
服务调用
在cmd窗口调用(不支持telnet可百度下)
telnet localhost 端口号(建议20880)
invoke com.wfs.samples.echo.api.EchoService.echo("")
- 项目文件配置所需jar包
<properties>
<curator.version>2.12.0</curator.version>
<dubbo.version>2.6.3</dubbo.version>
<curator-client.version>2.12.0</curator-client.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--curator-framework:对zookeeper的底层api的一些封装
curator-client:提供一些客户端的操作,例如重试策略等
curator-recipes:封装了一些高级特性,如:Cache事件监听、选举、分布式锁、分布式计数器、分布式Barrier等
处。-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator-client.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
</dependencies>
- 服务消费端设置
package com.wfs.samples.echo;
import com.wfs.samples.echo.api.EchoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EchoConsumer {
public static void main(String[] args) {
//加载配置
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("echo-consumer.xml");
//获取消费代理
context.start();
EchoService echoService= (EchoService) context.getBean("echoService");
echoService.echo("hello word");
System.out.println("---------");
}
}
```java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务消费方应用名称, 方便用于依赖跟踪 -->
<dubbo:application name="echo-provider"/>
<!-- 使用本地zookeeper作为注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 指定要消费的服务-->
<dubbo:reference id ="echoService" interface="com.wfs.samples.echo.api.EchoService" check="false"></dubbo:reference>
</beans>
- 启动项目需本地先启动zk
总结:dubbo框架启动时默认会通过Spring的容器来启动,本质上也是通过 ClassPathXmlApplicationContext来启动服务
服务提供方: 1.设置协议 2.设置注册中心 3.设置提供者配置
服务消费方: 1.设置注册中心 2.设置消费配置
api方式与注解方式参考:api方式与注解方式参考