首页 技术JAVA正文

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

薄洪涛 JAVA 2023-03-19 452 1 dubbozookeeper

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


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

精彩评论