dubbo分布式项目开发____dubbo服务调用开发

//创建工程名 cnse-customer-user

导入jar包 

//工程结构


//spring 文件配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://code.alibabatech.com/schema/dubbo  
            			http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
	<!-- 其他context引用  -->
    <!-- 引入调用dubbo服务的资源文件 -->
    <import resource="dubbo-customer-user.xml"/>
    
</beans>
//service接口

package com.cnse.provider.user.service;

import java.util.List;

import com.cnse.provider.user.service.po.User;


/**
 * @author Administrator
 * 
 */
public interface UserService {
	//检测用户登录的接口
	public String checkLoginUser(String username,String password);
	//获取用户列表的接口
	public List<User>getUsers();

}

//service空实现 以为服务端不会告诉你具体的实现 但自己要做一个空实现 

package com.cnse.provider.user.service.impl;

import java.util.List;

import com.cnse.provider.user.service.UserService;
import com.cnse.provider.user.service.po.User;

/**
 * @author Administrator
 * 服务端是不会给客户端具体的实现方法的
 * 所有要做一个空实现 调用的时候都是调用的服务端的
 */
public class UserServiceImpl implements UserService {

	@Override
	public String checkLoginUser(String username, String password) {
		return null;
	}

	@Override
	public List<User> getUsers() {
		return null;
	}
	
}

//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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://code.alibabatech.com/schema/dubbo  
            			http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">

	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="cnse-customer-user" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
	<dubbo:reference id="userService" interface="com.cnse.provider.user.service.UserService" />

</beans>
//log日志文件

#开发日志将在本地输出,并输出SQL

log4j.rootLogger=INFO,A1,DRF
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
log4j.appender.A1.layout.ConversionPattern=%d %5p [%F:%L] : %m%n

log4j.appender.DRF=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DRF.Threshold=INFO
log4j.appender.DRF.DatePattern='.'yyyy-MM-dd
log4j.appender.DRF.File=logs/cnse_customer_user.log
log4j.appender.DRF.Append=true
log4j.appender.DRF.layout=org.apache.log4j.PatternLayout
log4j.appender.DRF.layout.ConversionPattern=[%-5p][%d{yyyyMMdd HH:mm:ss,SSS}][%C{1}:%L] %m%n



//webxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>cnse-customer-user</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context-customer.xml</param-value>
  </context-param>
</web-app>

//调用服务接口的测试类

package com.cnse.customer.user.test;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cnse.provider.user.service.UserService;
import com.cnse.provider.user.service.po.User;


/**
 * @author Administrator
 *
 */
public class UserConsumer {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "spring-context-customer.xml" });
		context.start();

		UserService userService = (UserService) context.getBean("userService");
		String respStr = userService.checkLoginUser("admin","123456");
		System.out.println("调用登录服务的接口__respStr__:"+respStr);

		List<User>list = userService.getUsers();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				System.out.println("调用用户列表服务的接口__respStr__:"+list.get(i));
			}
		}
		System.in.read();
	}

}

//运行结果 


//代码和资源文件  百度资源地址:http://pan.baidu.com/s/1kVLnEkR   dubbo客户端和服务端的代码

posted @ 2017-02-25 15:20  蜜獾互联网  阅读(2)  评论(0编辑  收藏  举报  来源