1. 程式人生 > >Dubbox學習

Dubbox學習

Dubbox-噹噹網

Producer:

<?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://code.alibabatech.com/schema/dubbo"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	
	<!-- 引入zc-com通用類屬性注入配置檔案 -->
	<util:properties id="zcparams" location="classpath:params.properties"></util:properties>
	
	
    <dubbo:application name="provider" owner="programmer" organization="dubbox"/>

	<!-- zookeeper註冊中心 -->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <dubbo:annotation package="com.service" />    
   
    <dubbo:protocol name="dubbo"  />

    
	
	
	
	
	
</beans>
#HttpCaller
max_total_connections = 1600
max_route_connections = 1200
connect_timeout = 30000
read_timeout = 30000
import bhz.entity.Simple;

public interface SimpleService {
	
	public String sayHello(String name);
 
	public Simple getSimple();
}
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import bhz.entity.Simple;
import bhz.service.SimpleService;

@Service("simpleService")
@com.alibaba.dubbo.config.annotation.Service(interfaceClass=bhz.service.SimpleService.class, protocol={"dubbo"}, retries=0)
public class SimpleServiceImpl implements SimpleService{

	@Override
	public String sayHello(String name) {
		return "hello" + name;
	}

	@Override
	public Simple getSimple() {
        Map<String,Integer> map = new HashMap<String, Integer>(2);  
        map.put("zhang0", 1);  
        map.put("zhang1", 2);  
        return new Simple("zhang3", 21, map);
	}

}
import java.io.Serializable;
import java.util.Map;

public class Simple implements Serializable
{  
     private static final long serialVersionUID = -4914434736682797743L;  
     private String name;  
     private int age;  
     private Map<String,Integer> map;  
     public Simple(){  
  
     }  
     public Simple(String name,int age,Map<String,Integer> map){  
         this.name = name;  
         this.age = age;  
         this.map = map;  
     }  
  
     public String getName() {  
       return name;  
     }  
  
     public void setName(String name) {  
        this.name = name;  
     }  
  
     public int getAge() {  
        return age;  
     }  
  
     public void setAge(int age) {  
        this.age = age;  
     }  
  
     public Map<String, Integer> getMap() {  
        return map;  
     }  
  
     public void setMap(Map<String, Integer> map) {  
        this.map = map;  
     }  
  
  
}  
log4j.rootLogger=INFO, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=D:/002_developer/workspace_001/zcmoni.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.org.springframework=WARN
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "dubbo-provider.xml" });
		context.start();
		System.in.read(); // 為保證服務一直開著,利用輸入流的阻塞來模擬
	}
}

 

Consumer:

<?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://code.alibabatech.com/schema/dubbo"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	
	<!-- 引入zc-com通用類屬性注入配置檔案 -->
	<util:properties id="zcparams" location="classpath:params.properties"></util:properties>

    <dubbo:application name="consumer" owner="programmer" organization="dubbox"/>
    
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- kryo實現序列化 serialization="kryo" optimizer="bhz.utils.SerializationOptimizerImpl"   -->
    <dubbo:protocol name="dubbo" />
    
    <!-- 生成遠端服務代理,可以像使用本地bean -->
	<dubbo:reference interface="com.service.UserService" id="userService" check="false" />
    <!-- 生成遠端服務代理,可以像使用本地bean -->
	<dubbo:reference interface="com.service.SimpleService" id="simpleService" />
	
</beans>
import java.util.Map;

public class Simple // implements Serializable
{  
     private static final long serialVersionUID = -4914434736682797743L;  
     private String name;  
     private int age;  
     private Map<String,Integer> map;  
     public Simple(){  
  
     }  
     public Simple(String name,int age,Map<String,Integer> map){  
         this.name = name;  
         this.age = age;  
         this.map = map;  
     }  
  
     public String getName() {  
       return name;  
     }  
  
     public void setName(String name) {  
        this.name = name;  
     }  
  
     public int getAge() {  
        return age;  
     }  
  
     public void setAge(int age) {  
        this.age = age;  
     }  
  
     public Map<String, Integer> getMap() {  
        return map;  
     }  
  
     public void setMap(Map<String, Integer> map) {  
        this.map = map;  
     }  
  
  
}  
import bhz.entity.Simple;

public interface SimpleService {
	
	public String sayHello(String name);

	public Simple getSimple();
}
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.entity.Simple;
import com.service.SimpleService;

public class Consumer {

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

		SimpleService ss = (SimpleService) context.getBean("simpleService");
		String hello = ss.sayHello("tom");
		System.out.println(hello);
		Simple simple = ss.getSimple();
		System.out.println(simple.getName());
		System.out.println(simple.getAge());
		System.out.println(simple.getMap().get("zhang1"));
		

//		UserService us = (UserService) context.getBean("userService");
//		
//		System.out.println(us.getUser().getName());
//		System.out.println(us.getUser().getId());
		
		
		
	}

}