亲宝软件园·资讯

展开

springboot dubbo启动

波波的。 人气:0

前言

现在用dubbo的太多了,我到现在还不熟悉,这太不应该了,这次好好看了一下dubbo,终于把基本的启动框架搭好了。dubbo的角色宽泛的分三类provider,comsumer,注册中心。我这里的注册中心用的是zookeeper,并且是在windows环境下做的。

dubbo和springboot整合的依赖:

<dependency>
   <groupId>com.alibaba.boot</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>0.2.0</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

实操

1、创建一个大的maven项目,将里面的src目录删掉,这只是为了在这个大的maven项目里面添加provider和comsumer两个模块。之后创建两个springboot项目,分别是provider和comsumer,创建好的结构:

在这里插入图片描述

这里的common模块我是把entity和接口类抽出来,不抽出来也是可以的

2、common(这里面没有任何依赖,就是个maven项目)

在这里插入图片描述

entity:

public class User {
    private Long id;
    private String name;
    private Integer age;
    public User() {
    }
    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    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;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

UserService:

public interface UserService {
    User query(Long id);
}

OrderService:

public interface OrderService {
    String getName(Long id);
}

3、provider(添加dubbo依赖并把刚才写的公共模块依赖进来)

<dependency>
    <groupId>org.example</groupId>
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

在这里插入图片描述

OrderServiceImpl:

import com.alibaba.dubbo.config.annotation.Service;
//这里的service注解用的是dubbo
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserService userService;
    @Override
    public String getName(Long id) {
        User query = userService.query(id);
        return query.getName();
    }
}

UserServiceImpl:

import com.alibaba.dubbo.config.annotation.Service;
@Service
public class UserServiceImpl implements UserService {
    @Override
    public User query(Long id) {
        User user = new User(1L, "张三", 12);        
        return user;
    }
}

启动类上添加@EnableDubbo

application.properties:

#服务名称
dubbo.application.name=provice
#注册中心
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#通信协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

4、comsumer(添加dubbo依赖,common模块,web依赖)

在这里插入图片描述

OrderController:

import com.alibaba.dubbo.config.annotation.Reference;
@RestController
public class OrderController {
//添加dubbo的reference注解,进行远程调用
    @Reference
    OrderService orderService;

    @RequestMapping("getName")
    String getName(Long id){
        String name = orderService.getName(id);
        return name;
    }
}

启动类上加上@EnableDubbo

application.properties:

#服务名称
dubbo.application.name=comsumer
#注册中心
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181

测试

启动zookeeper,provider和comsumer.

在这里插入图片描述

注:

1、首先要启动zookeeper,下载windows版压缩包,修改conf配置文件,启动zkServer.cmd命令。具体怎么弄这里就不写了,网上很多,我这里主要练习的是dubbo.
2、启动类上需要添加@EnableDubbo
3、comsumer中调用服务用@Reference,因为是远程调用,所以 @Autowired肯定是没用的了
4、provider中的实现类需要用dubbo的@Service注解

加载全部内容

相关教程
猜你喜欢
用户评论