码农日记

薄洪涛的个人博客

基于dubbo改造现有http调用项目

使用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();
    }
}


  • 评论列表

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

版权所有 | 转载请标明出处