SpringBoot2.1.7 整合Apache Dubbo2.7.3

  1. 添加SpringBoot和Dubbo的相关依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>
  1. 新建接口工程,新建一个实体类(如果需要使用dubbo传递,需实现Serializable接口)和一个服务接口
import java.io.Serializable;
public class User implements Serializable {
    private String name;
    private Integer age;
    private Boolean gender;
    private String description;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getGender() {
        return gender;
    }
    public void setGender(Boolean gender) {
        this.gender = gender;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}
import java.util.concurrent.CompletableFuture;

public interface IHelloService {
    String sayHi(String name);
    CompletableFuture<String> sayHello(String name);
    User save(User user);
}
  1. 创建服务的提供者,实现上述的服务接口,并注册到zookeeper上。
import com.sjb.entity.User;
import com.sjb.interfaces.IHelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.CompletableFuture;

#这里的Service注解不再是SpringBoot提供的,而是由apache的dubbo提供
@Service
public class HelloService implements IHelloService {
    @Value("${server.port}")
    private Integer port;

    @Override
    public String sayHi(String name) {
        return "Hi, " + name;
    }

    @Override
    public CompletableFuture<String> sayHello(String name) {
        return CompletableFuture.completedFuture(sayHi(name));
    }

    @Override
    public User save(User user) {
        user.setDescription(this.port.toString());
        return user;
    }
}

配置文件:

spring:
  application:
    name: provider
dubbo:
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://192.168.182.128:2181
  consumer:
    check: false
    loadbalance: roundrobin
  scan:
    base-packages: com.wiki.dubbo.dubbo.service
server:
  port: 9000
  1. 创建服务的消费者,并注册到zookeeper上
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Reference
    private IHelloService helloService;

    @RequestMapping
    public String sayHi() {
        return helloService.sayHi("pegasus");
    }

    @RequestMapping("sayHello")
    public String sayHello() {
        helloService.sayHello("异步").whenComplete((s, throwable) -> {
            if (throwable == null) {
                System.out.println(s);
            } else {
                throwable.printStackTrace();
            }
        });
        return "执行中...";
    }


    @RequestMapping("save")
    public User save() {
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        user.setGender(true);
        user.setDescription("");
        return helloService.save(user);
    }
}

配置文件:

spring:
  application:
    name: consumer
dubbo:
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://192.168.182.128:2181
  consumer:
    check: false
    loadbalance: roundrobin
server:
  port: 80
  1. git地址:https://gitee.com/PegasusKID_admin/dubbo.git
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值