SpringBoot整合Dubbo

2020-09-03 16:57:04 浏览数 (1)

通过三个工程演示整合过程

工程一:API工程:服务端与客户端共同需要引入的工程

pom:无特殊依赖,下边内容供服务端与客户端引用此工程使用

代码语言:javascript复制
  <groupId>com.boot.dubbo.demo</groupId>
  <artifactId>dubbo-api</artifactId>
  <version>1.0-SNAPSHOT</version>

实体类:

代码语言:javascript复制
package com.boot.dubbo.demo.domain;

import java.io.Serializable;

/**
 * 用户信息类
 */
public class UserInfo implements Serializable {
    private String account;
    private String password;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

API:

代码语言:javascript复制
package com.boot.dubbo.demo.api;

import com.boot.dubbo.demo.domain.UserInfo;

public interface UserService {
    // 定义用户登录的api
    UserInfo login(UserInfo user);
}

工程二:生产者工程:服务端

pom

代码语言:javascript复制
 <!--依赖API-->
        <dependency>
            <groupId>com.boot.dubbo.demo</groupId>
            <artifactId>boot-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

dubbo必须依赖项:(使用zk)

代码语言:javascript复制
<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.10</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

application.yml:配置dubbo配置项

代码语言:javascript复制
spring:
  dubbo:
    application:
      name: dubbo-provider
    protocol:
      name: dubbo
      port: 20880
    registry:
      address: zookeeper://192.168.3.101:2181
代码语言:javascript复制
package com.boot.dubbo.demo.dubboprovider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.boot.dubbo.demo.api.UserService;
import com.boot.dubbo.demo.domain.UserInfo;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = UserService.class) //注意是dubbo的注解,向外暴露接口
public class UserServiceImpl implements UserService {
    public UserInfo login(UserInfo user) {
        UserInfo reUser = new UserInfo();
        reUser.setAccount("登录的账号为:" user.getAccount());
        reUser.setPassword("登录的密码为:" user.getPassword());

        return reUser;
    }
}
代码语言:javascript复制
package com.boot.dubbo.demo.dubboprovider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo // 启用dubbo
public class DubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

工程三:消费者工程:客户端

pom依赖与工程二相同

application.yml配置:

代码语言:javascript复制
spring:
  dubbo:
    application:
      name: dubbo-provider
    protocol:
      name: dubbo
      port: 20880
    registry:
      address: zookeeper://192.168.3.101:2181
server:
  port: 8081

Controller

代码语言:javascript复制
package com.boot.dubbo.demo.dubboconsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.dubbo.demo.api.UserService;
import com.boot.dubbo.demo.domain.UserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Reference // 引用dubbo服务器提供服务器接口
    private UserService userService;

    @GetMapping("/login")
    public UserInfo login(UserInfo userInfo) {
        return userService.login(userInfo);
    }
}
代码语言:javascript复制
package com.boot.dubbo.demo.dubboconsumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}

这样客户端就可以通过Dubbo调用服务端提供的方法了

0 人点赞