码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot整合Dubbo

时间:2018-10-24 22:13:15      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:package   ota   过程   1.0   启用   tac   account   one   gap   

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

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

 

技术分享图片

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

  <groupId>com.boot.dubbo.demo</groupId>
  <artifactId>dubbo-api</artifactId>
  <version>1.0-SNAPSHOT</version>

实体类:

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:

package com.boot.dubbo.demo.api;

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

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

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

技术分享图片

pom

 <!--依赖API-->
        <dependency>
            <groupId>com.boot.dubbo.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

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

<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配置项

spring:
  dubbo:
    application:
      name: dubbo-provider
    protocol:
      name: dubbo
      port: 20880
    registry:
      address: zookeeper://192.168.3.101:2181

 

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;
    }
}

 

package com.boot.dubbo.demo.dubboprovider;

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

@SpringBootApplication
@EnableDubboConfiguration // 启用dubbo自动配置
public class DubboProviderApplication {

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

 

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

技术分享图片

pom依赖与工程二相同

application.yml配置:

spring:
  dubbo:
    application:
      name: dubbo-provider
    protocol:
      name: dubbo
      port: 20880
    registry:
      address: zookeeper://192.168.3.101:2181
server:
  port: 8081

Controller

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);
    }
}

 

package com.boot.dubbo.demo.dubboconsumer;

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

@SpringBootApplication
@EnableDubboConfiguration 
public class DubboConsumerApplication {

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

 

 

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

SpringBoot整合Dubbo

标签:package   ota   过程   1.0   启用   tac   account   one   gap   

原文地址:https://www.cnblogs.com/lm970585581/p/9845746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!