栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

初学SpringBoot--ch08-SpringBoot 集成 Dubbo

初学SpringBoot--ch08-SpringBoot 集成 Dubbo

ch08-SpringBoot 集成 Dubbo

1.1 公共项目

1.1.1 项目坐标1.1.2 创建 Student 实体类1.1.3 创建服务接口 1.2 服务提供者

1.2.1 Maven依赖1.2.2 编写 application.properties 文件1.2.3 创建接口的实体类1.2.4 修改主启动类 Application 1.3 创建消费者

1.3.1 Maven依赖1.3.2 编写 application.properties 文件1.3.3 创建 Controller1.3.4 修改主启动类 Application 1.4 测试应用

1.4.1 启动zookeeper1.4.2 运行服务提供者1.4.3 运行消费者1.4.4 执行测试

1.1 公共项目

创建Maven项目:ch14-interface-api

1.1.1 项目坐标
  com.suyv
  ch14-interface-api
  1.0.0
1.1.2 创建 Student 实体类
package com.suyv.model;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 1318357199475675242L;

    private Integer id;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }
}
1.1.3 创建服务接口
package com.suyv.service;

import com.suyv.model.Student;

public interface StudentService {
    Student queryStudent(Integer id);
}
1.2 服务提供者

创建SpringBoot项目:ch15-service-provider

1.2.1 Maven依赖

        
        
            com.suyv
            ch14-interface-api
            1.0.0
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            2.7.8
            pom
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
1.2.2 编写 application.properties 文件
#服务名称
spring.application.name=studentservice-provider

#zookeeper 注册中心
dubbo.registry.address=zookeeper://localhost:2181

#dubbo 注解所在的包名
dubbo.scan.base-packages=com.suyv.service

# 配置Dubbo协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

1.2.3 创建接口的实体类
package com.suyv.service.impl;

import com.suyv.model.Student;
import com.suyv.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;


@Component
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 500)
public class StudentServiceImpl implements StudentService {

    @Override
    public Student queryStudent(Integer id) {
        Student student = new Student();
        if( 1001 == id){
            student.setId(1001);
            student.setName("1001-张三");
            student.setAge(20);
        }else if( 1002 == id){
            student.setId(1002);
            student.setName("1002-李四");
            student.setAge(25);
        }
        return student;
    }
}
1.2.4 修改主启动类 Application
package com.suyv;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@EnableDubbo
public class Application {

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

}
1.3 创建消费者

创建SpringBoot项目:ch16-service-consumer

1.3.1 Maven依赖

        
        
            com.suyv
            ch14-interface-api
            1.0.0
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            2.7.8
            pom
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
1.3.2 编写 application.properties 文件
#服务名称
spring.application.name=studentservice-consumer

#zookeeper注册中心
dubbo.registry.address=zookeeper://localhost:2181
1.3.3 创建 Controller
package cm.suyv.controller;

import com.suyv.model.Student;
import com.suyv.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboController {

    @DubboReference(interfaceClass = StudentService.class,version = "1.0")
    private StudentService studentService;

    @GetMapping("/query")
    public String queryStudent(){
        Student student = studentService.queryStudent(1001);
        return "调用远程接口:获取对象:" + student;
    }
}
1.3.4 修改主启动类 Application
package cm.suyv;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class Application {

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

}
1.4 测试应用 1.4.1 启动zookeeper

1.4.2 运行服务提供者

1.4.3 运行消费者

1.4.4 执行测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/758166.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号