Apache Dubbo 分布式开发时
1. 提供者里的service方法的注解 @Service注意要导包 dubbo的 代表的是 将其发布为服务
提供者applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--
提供方
1. 应用名称 dubbo:application
2. 服务注册中心 dubbo:registry 注册 registry
要配置ip地址 zookeeper
3. 注册 协议和port dubbo:protocol
4. 扫描包 dubbo:annotation 注意service层添加service注解的时候导的包是 dubbo的
-->
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo_provider"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 注册 协议和port 端口默认是20880 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包,加上@Service注解的类会被发布为服务 -->
<dubbo:annotation package="com.itheima.service.Impl" />
</beans>
service 层
package com.itheima.service.Impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.service.HelloService;
import org.springframework.transaction.annotation.Transactional;
/*这里配置Service注解要注意导的包是 dubbo的 代表发布为服务 */
@Service
@Transactional
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
2. 消费者里调用提供者的接口 要用@Reference注解
消费者 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
消费方
1. 应用名称 dubbo:application
2. 服务注册中心 dubbo:registry 注册 registry
要配置ip地址 zookeeper
3. 扫描包 dubbo:annotation 注意service层添加service注解的时候导的包是 dubbo的
-->
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo-consumer" />
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 包扫描的方式 引用服务 扫描@Reference -->
<dubbo:annotation package="com.itheima.controller" />
<!--启动时检查-->
<dubbo:consumer check="false"></dubbo:consumer>
</beans>
controller
package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class HelloController {
//@Reference 代表的是到注册中心查找服务
@Reference(loadbalance = "random")
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//远程调用
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}
运行报空指针异常解决方法
1. 配置文件的问题
考虑扫包 端口占用 zookeeper地址问题
2. 注解问题
注意提供者用的 service注解 以及 消费者用的 reference注解 是否为 dubbo的
3. zookeeper问题
重启zookeeper 防止因不当操作造成zookeeper停止运行(鼠标左击最容易造成这个问题)