导入的service包
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
消费者的service 代码
@Service
public class MeetingPubServiceImpl implements MeetingPubService {
@Autowired(required = true)
private UserService userService;
@Override
public MeetingPub selectByPcode(String pcode) {
//查询数据库
MeetingPub meetingPub = new MeetingPub();
meetingPub.setId("1001");
meetingPub.setPcode(pcode);
meetingPub.setTitle("学习dubbo");
meetingPub.setContent("Dubbo是一个RPC框架,是一个分布式服务解决方案");
meetingPub.setUid("123");
//根据用户id查询用户信息
//调用的 user-service-provider信息
System.out.println("远程服务访问 " + userService);
User user = userService.selectById(meetingPub.getUid());
meetingPub.setUser(user);
return meetingPub;
}
}
消费者的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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.liu.service.impl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="meeting-service-consumer" />
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.liu.service.UserService" />
<dubbo:provider server="netty4"/>
</beans>
对应的main
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(
new String[] {"classpath:consumer.xml"});
context.start();
MeetingPubService meetingPubService = context.getBean(MeetingPubService.class);
//通过会议编号查询会议信息
MeetingPub meetingPub = meetingPubService.selectByPcode("10001011");
// 显示调用结果
System.out.println( meetingPub );
//添加阻塞
System.in.read();//按任意键退出
}
}
消费者的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liu</groupId>
<artifactId>meeting-service-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>meeting-service-consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dubbo.version>2.7.5</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>com.liu</groupId>
<artifactId>meeting-interface-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!--dubbo依赖 zookeeper依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</project>