dubbo注解式服务(2)

一、配置服务提供方


1、首先配置spring配置



 
    
    
 
    
    
 
    
    
 
    
    
 
    
    
    
 


2、编写服务提供方的实现类

package com.alibaba.dubbo.annotation.service.impl;
import com.alibaba.dubbo.annotation.service.DemoService;
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		 System.out.print("annotation say"+name);
		return name;
	}
}

DemoService 接口类类同上一篇,封装在soa-common项目中

提供方的启动类不在赘述,参考作者上一篇文章

二、配置服务消费者

1、首先配置spring




    
    
 
    
    
 
    
    
 
 


2、调用类如下;

package com.alibaba.dubbo.action;

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.annotation.service.DemoService;
import com.alibaba.dubbo.config.annotation.Reference;

@Component
public class DemoAction {
	@Reference(version = "1.0.0")
	private DemoService demoService;

	public void say() {
		demoService.sayHello("hello");
	}
}

3、然后测试类如下;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.action.DemoAction;
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.start();
        DemoAction demoService = (DemoAction)context.getBean("demoAction"); // 获取远程服务代理
         demoService.say(); // 执行远程方法
    }
}



你可能感兴趣的:(soa,框架学习)