1.sts 创建spring boot 工程,选择web组件
2.DubboService.java
package com.example.demo; public interface DubboService { public String sayHello(); }
3.DubboServiceImp.java
package com.example.demo; import org.springframework.stereotype.Service; @Service("DubboService") public class DubboServiceImp implements DubboService{ @Override public String sayHello() { return "Hello Dubbo"; } }
3.ZDubboService1Application.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:provider.xml") public class ZDubboService1Application { public static void main(String[] args) { SpringApplication.run(ZDubboService1Application.class, args); } }
4.src/main/resources 下增加provider.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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 服务应用名称 --> <dubbo:application name="provider"/> <!-- 使用zookeeper做为注册中心 --> <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/> <!-- ref中的值要跟服务实现类中的@Server的值一致 --> <dubbo:service interface="com.example.demo.DubboService" ref="DubboService"></dubbo:service> </beans>