栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 云计算 > 云平台

SpringBoot整合Dubbo

云平台 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Dubbo

SpringBoot整合Dubbo
  • 1.接口工程
  • 2.提供者
  • 3. 消费者
  • 4. 测试

1.接口工程

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.yl
    boot-interface
    0.0.1-SNAPSHOT
    boot-interface
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.yl
            boot-interface
            0.0.1-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.实体类

package com.yl.bootinterface.model;

import java.io.Serializable;

public class Student implements Serializable {
    private Integer id;
    private String name;
    private Double score;
    private Integer sex;

    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 Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }
}

3.接口

package com.yl.bootinterface.service;

import com.yl.bootinterface.model.Student;

public interface StudentService {

    Student getStudentById(Integer id);
}

2.提供者

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.tl
    boot-provider
    0.0.1-SNAPSHOT
    boot-provider
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        
        
            com.yl
            boot-interface
            0.0.1-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.application.properties

server.port=8080
# 提供者名称,唯一
dubbo.application.name=boot-provider
# 配置注册中心
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
# 配置协议以及端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 配置超时时间,默认为1000ms
dubbo.registry.timeout=5000
# 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接
dubbo.provider.retries=3
# 配置监控中心要监控的地址,监控注册中心的地址
dubbo.monitor.address=registry

3.接口实现类

package com.tl.bootprovider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yl.bootinterface.model.Student;
import com.yl.bootinterface.service.StudentService;

//暴露服务,注意是com.alibaba这个包下的
@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public Student getStudentById(Integer id) {
        Student student = new Student();
        student.setId(id);
        student.setName("tom");
        student.setScore(99.0);
        student.setSex(0);
        return student;
    }
}

4.主程序

package com.tl.bootprovider;

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

@EnableDubbo //开启基于dubbo的注解功能
@SpringBootApplication
public class BootProviderApplication {

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

}

3. 消费者

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.yl
    boot-consumer
    0.0.1-SNAPSHOT
    boot-consumer
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.yl
            boot-interface
            0.0.1-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.application.properties

server.port=8081
# 消费者名称,唯一
dubbo.application.name=boot-consumer
# 配置注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 配置超时时间,默认为1000ms
dubbo.registry.timeout=5000
# 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接
dubbo.consumer.retries=3
# 配置监控中心要监控的地址
dubbo.monitor.address=registry

3.controller

package com.yl.bootconsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yl.bootinterface.model.Student;
import com.yl.bootinterface.service.StudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    @Reference //引用远程服务
    StudentService studentService;

    @GetMapping("/getStudentById")
    public Student getStudentById(Integer id) {
        return studentService.getStudentById(id);
    }
}

4.主程序

package com.yl.bootconsumer;

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

@SpringBootApplication
@EnableDubbo //开启基于dubbo的注解功能
public class BootConsumerApplication {

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

}

4. 测试

1.启动本地的zookeeper
2.启动提供者和服务器
3.访问测试接口

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

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

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