dubbo分布式项目开发____dubbo服务接口开发

//1.创建服务提供者工程名 cnse-provider-user 

//2.导入jar包和创建工程包名


//配置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: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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 ">
    <!-- 常规的spring加载 -->
		<!-- 采用注释的方式配置bean -->
		<context:annotation-config />
		<!-- 配置要扫描的包 -->
		<context:component-scan base-package="com.cnse" />
	<!-- 引入dubbo提供服务的资源文件 -->	
    	<import resource="dubbo-provider-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();

}
//实现包

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

import java.util.ArrayList;
import java.util.List;

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

public class UserServiceImpl implements UserService {
	//校验用户的方法
	public String checkLoginUser(String username,String password) {
		String replyBodyStr="";
		if(username.equals("admin")&&password.equals("123456")){
				replyBodyStr="欢迎你的登录____:"+username;
			return replyBodyStr; 
		}else{
				replyBodyStr="你的用户名或密码错误...";
			return replyBodyStr;
		}
	}

	public List<User> getUsers() {
		//模拟从数据库查询出的对象
		List<User>list = new ArrayList<User>();
		//对象1
		User u1 = new User();
			//初始化数据
			u1.setUserid("1");
			u1.setUsername("admin");
			u1.setPassword("123456");
		list.add(u1);
		//对象2
		User u2 = new User();
			//初始化数据
			u2.setUserid("2");
			u2.setUsername("test");
			u2.setPassword("123456");
		list.add(u2);
		
		return list;
	}
}

//pouser对象

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

import java.io.Serializable;

public class User implements Serializable{
	//serid
	private static final long serialVersionUID = 1L;
	//po
	private String userid;
	private String username;
	private String password;
	//encap
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	//tosring
	@Override
	public String toString() {
		return "User [userid=" + userid + ", username=" + username + ", password=" + password + "]";
	}
	
}

//dubbo配置文件

<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 ">
<!-- 具体的实现bean -->
	<bean id="userService" class="com.cnse.provider.user.service.impl.UserServiceImpl" />

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="cnse-provider-user" />

	<!-- 使用multicast广播注册中心暴露服务地址 -->
	<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
	
	<!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="com.cnse.provider.user.service.UserService" ref="userService" />

</beans>
#开发日志将在本地输出,并输出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_provider_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-provider-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-user-provider.xml</param-value>
  </context-param>
</web-app>

//其他包都是空实现

测试类

package com.cnse.provider.user.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 手动调用类
 */
public class UserProvider {
	
	
	private static final Log logger = LogFactory.getLog(UserProvider.class);
	

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider-user.xml");
			context.start();
		} catch (Exception e) {
			logger.error("== UserProvider context start error:",e);
		}
		synchronized (UserProvider.class) {
			while (true) {
				try {
					UserProvider.class.wait();
				} catch (InterruptedException e) {
					logger.error("== synchronized error:",e);
				}
			}
		}
	}
    
}

//开启zookeeper注册中心 上节已经说了怎么开启和配置

执行测试的类

//运行结果






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

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