1. 程式人生 > 實用技巧 >springboot 整合dubbo(用zookeeper註冊中心)

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註冊中心