注解方式

服务端

<?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-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<context:component-scan base-package="com.enjoy.dao"/>

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

<!-- 使用zookeeper注册中心暴露服务地址 -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->

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

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

</beans>

注意,@Service使用import com.alibaba.dubbo.config.annotation.Service;

客户端

<?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-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

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

<!-- 使用zookeeper注册中心暴露服务地址 -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>-->

<dubbo:annotation package="com.enjoy.controller" />

</beans>

注入使用:

 @Reference
 private UserService userService;

Api方式

provider

public static void initDubbo() throws IOException {
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("StoreServerApi");

// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("192.168.46.133:2181");

// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("rmi");
protocol.setPort(21880);
protocol.setThreads(100);

// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
ServiceConfig<VipUserService> service = new ServiceConfig<>();

service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(VipUserService.class);
service.setRef(new VipUserServiceImpl());

// 暴露及注册服务
service.export();

System.out.println("服务已经启动");
System.in.read();

}

consumer

public class StoreConsumer {
public static void main(String[] args) throws IOException {
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("StoreServerClientApi");

// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("192.168.46.133:2181");

// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20882);
protocol.setThreads(100);

// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<VipUserService> reference = new ReferenceConfig<>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(VipUserService.class);

// 和本地bean一样使用xxxService
VipUserService vipUserService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
String ret = vipUserService.getVipDetail("123");
reference.destroy();
System.out.println(ret);



}

}

标签有个继承关系,

------下层会继承上层属性配置

------ 消费方,会继承服务方属性配置:服务只提供者自己知道,配什么属性最合适