需求:a作为服务提供方,被b服务消费者调用。
项目规划
- c-common:将公共的实体类、接口放入该模块下。
- a-provider:服务提供者。
- b-consumer:服务消费者。
引入依赖
在c-common
下的pom.xml
中,加入如下依赖:
<dependencies>
<!-- 引入dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 使用zookeeper作为注册中心
dubbo 2.6以前的版本引入zkclient操作zookeeper
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本2选1即可
-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
同时,a-provider
与b-consumer
两个模块引入c-common
<dependencies>
<!--引入公共模块-->
<dependency>
<groupId>org.yun</groupId>
<artifactId>c-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
开发c-common
很简单,就是定义服务提供者与服务消费者的接口。
/**
* 服务提供接口
*/
public interface AService {
void provider();
}
/**
* 服务消费接口
*/
public interface BService {
void consumer();
}
开发a-provider
a作为服务提供者要实现AService
接口。
import com.alibaba.dubbo.config.annotation.Service;
import org.yun.common.service.AService;
/**
* 注意:这个是dubbo包下的@Service
* 表示对外暴露的服务
*/
@Service
public class AServiceImpl implements AService {
@Override
public void provider() {
System.out.println("a-provider");
System.out.println("我被b-consumer调用了...");
}
}
在provider.xml
中,配置具体的服务。
<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="a-provider"/>
<!--指定服务的注册地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--指定服务注册的通信协议与端口号-->
<dubbo:protocol name="dubbo" port="20890"/>
<!-- 要扫描dubbo服务提供者实现类的包 -->
<dubbo:annotation package="org.yun.provider.service.impl"/>
</beans>
编写服务提供者启动类
public class ProviderMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("provider.xml");
ctx.start();
System.out.println("服务启动了...");
System.in.read();
}
}
启动服务,看到..
注意:因为使用Zookeeper作为注册中心,所以要先启动Zookeeper服务,然后再启动 a-provider 。
至此,服务提供者开发完毕。接下来,开发服务消费者。
开发b-consumer
b作为服务消费者要实现BService
接口。
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.yun.common.service.AService;
import org.yun.common.service.BService;
//也可以是@Service(是Spring下的)。表示被Spring容器管理的组件。
@Component
public class BServiceImpl implements BService {
//要引用的远程服务
@Reference //该注解是Dubbo下的,表示要调用的远程服务
private AService aService;
@Override
public void consumer() {
aService.provider();//调用远程服务
System.out.println("b-consumer");
System.out.println("我消费了a-provider服务...");
}
}
在consumer.xml
中,配置具体的服务。
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--指定服务应用的名称-->
<dubbo:application name="b-consumer"/>
<!--指定服务的注册地址。要与服务提供者的地址一致。-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 要扫描dubbo服务消费者实现类的包 -->
<dubbo:annotation package="org.yun.consumer.service.impl"/>
<!-- 指定spring要扫描的包 -->
<context:component-scan base-package="org.yun.consumer.service.impl"/>
</beans>
编写服务消费者启动类
public class ConsumerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("consumer.xml");
ctx.start();//启动服务
BService bService = ctx.getBean(BService.class);
bService.consumer(); //服务消费者,消费服务
System.in.read();
}
}
启动服务,看到..