使用zooker作为注册中心,将现有通过http调用的项目改造成dubbo调用
公共项目
规定接口
public interface OrderService { public String getOrderId(); }
服务提供者
配置
dubbo.application.name=provider-application dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.registry.address=zookeeper://127.0.0.1:2181
maven
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-rpc-dubbo</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency>
启动类
@SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
实现类
@DubboService public class OrderServiceImpl implements OrderService { /** * 获取orderid * @return String */ public String getOrderId() { return "boht"; } }
消费者
配置
dubbo.application.name=consumer-application dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.registry.address=zookeeper://127.0.0.1:2181
maven
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-rpc-dubbo</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency>
启动类
@SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
实现类
import com.boht.common.OrderService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Service; @Service public class UserService { @DubboReference private OrderService orderService; /** * 获取orderid * @return String */ public String getOrderId() { return orderService.getOrderId(); } }