生产者的项目结构:
消费者的项目结构:
生产者项目的具体内容:
pom.xml依赖的jar
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
注:依赖的Spring的jar略…详见:http://blog.csdn.net/w_x_z_/article/details/52705153
User.java
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String userName;
private Integer age;
// setter和getter
//..
}
UserService.java 接口
public interface UserService {
String saveUser(User user);
List<User> findAll();
}
UserServiceImpl.java 实现类
@Service("userService")
public class UserServiceImpl implements UserService {
public static List<User> userList = new ArrayList<User>();
@Override
public String saveUser(User user) {
userList.add(user);
return user.getId();
}
@Override
public List<User> findAll() {
return userList;
}
}
Main.java
public class Main {
public static void main(String[] args) throws IOException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
System.out.println("服务端:服务启动...");
System.in.read();
}
}
spring.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 引入配置文件 -->
<import resource="classpath:spring-*.xml"/>
<!-- 加载config.properties配置文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
<bean id="userService" class="com.netant.service.impl.UserServiceImpl" />
</beans>
spring-dubbo.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="Dubbo_HelloWorld" />
<!-- 注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${zookeeper.connect}"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.netant.service.UserService" ref="userService" protocol="dubbo" timeout="5000000"/>
</beans>
消费者项目的具体内容:
需要依赖生产者提供的实体类User.java和UserService接口
UserAccess.java
通过此类调用UserService提供的服务
@Component
public class UserAccess {
@Autowired
private UserService userService;
public String saveUser(String id ,String userName ,Integer age){
User user = new User();
user.setId(id);
user.setUserName(userName);
user.setAge(age);
return userService.saveUser(user);
}
public List<User> findAllUser(){
return userService.findAll();
}
}
Main.java
public class Main {
public static void main(String[] args) throws IOException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
UserAccess userAccess = ctx.getBean(UserAccess.class);
// String userId = userAccess.saveUser("456", "Jack1", 20);
//
// System.out.println("保存用户成功之后返回的userId:" + userId);
List<User> userList = userAccess.findAllUser();
for (User user : userList) {
System.out.println("遍历获取的所有用户信息:" + user.getId() + " " + user.getUserName());
}
}
}
spring.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 扫描注解注入 -->
<context:component-scan base-package="com.netant" />
<!-- 引入配置文件 -->
<import resource="classpath:spring-*.xml"/>
<!-- 加载config.properties配置文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
<bean id="userAccess" class="com.netant.UserAccess" />
</beans>
spring-dubbo.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="Dubbo_HelloWorld_Ref" />
<!-- 注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${zookeeper.connect}"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 生成远程服务代理,可以和本地bean一样使用userService -->
<dubbo:reference id="userService" interface="com.netant.service.UserService" protocol="dubbo" check="false" retries="0"/>
</beans>