zoukankan      html  css  js  c++  java
  • SpringBoot整合Dubbo

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

    工程一: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>boot-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.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配置:

    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.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调用服务端提供的方法了

  • 相关阅读:
    重构了一波代码,聊聊后端也聊聊游戏后端
    浅谈游戏开发中常见的设计模式
    一次查内存泄露
    sql语句技巧
    python后端链接数据库-----MySQLdb
    web的应用模式
    静态文件
    django配置文件
    视图
    django子应用
  • 原文地址:https://www.cnblogs.com/lm970585581/p/9845746.html
Copyright © 2011-2022 走看看