dubbo注解方式(直连,开发用)实现:
1.服务提供方
@Service
@Component
public class TestServiceIm implements TestService {
@Autowired
TestDao testDao;
public Date getNow() {
return testDao.getNow();
}
}
配置文件:dubbo注解扫描,放在spring注解扫描的前后都可以
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.exam.service" />
<!--注解方式,扫描dubbo注解-->
<dubbo:annotation package="cn.exam.service"/>
<!-- 配置dubbo的服务提供 -->
<!-- 设置服务提供者的名字 (名字唯一.) -->
<dubbo:application name="exam-provider"/>
<!-- 以直连的方式提供服务.(方式2) -->
<dubbo:registry address="N/A"/>
<!-- 指定dubbo使用的端口号 -->
<dubbo:protocol name="dubbo" port="20880"/>
2.服务消费者
@RestController
public class TestController {
//必须指定url为dubbo协议本地地址及端口号
@Reference(url = "dubbo://127.0.0.1:20880")
TestService testService;
@RequestMapping("/test")
public String now(){
Date date=testService.getNow();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
return time;
}
}
配置文件:(先让dubbo注解扫描,要不spring先注入过了,会出现NullPointerException)
<dubbo:annotation package="cn.exam.web"/>
<!-- 1.开启web层注解扫描 -->
<context:component-scan base-package="cn.exam.web" />