springboot 整合dubbo(用zookeeper注册中心)
1.新建Springboot工程,引入dubbo依赖
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
2.编写公用接口
public interface AddrService { List<Addr> findAddrByUserId(String userId); }
3.配置provider.properties
dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.application.name=AddServer-provider dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.monitor.protocol=registry
4.实现接口
@Service @com.alibaba.dubbo.config.annotation.Service public class AddrServiceImpl implements AddrService { @Override public List<Addr> findAddrByUserId(String userId) { List<Addr> addrs = new ArrayList<>(); addrs.add(new Addr().setSheng("江西").setShi("南昌").setXian("青山湖")); addrs.add(new Addr().setSheng("江西").setShi("南昌").setXian("西湖")); return addrs; } }
5.配置consumer.properties
dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.application.name=AddServer-consumer dubbo.monitor.protocol=registry
6.调用接口
@Service public class AddrServiceImpl implements AddrService { @Reference private AddrService addrService; @Override public List<Addr> findAddrByUserId(String userId) { return addrService.findAddrByUserId(userId); } }
注意:需要先启动zookeeper注册中心
浙公网安备 33010602011771号