SpringBoot整合Dubbo

SpringBoot整合Dubbo,第1张

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

1.接口工程

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>boot-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-interface</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>boot-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tl</groupId>
    <artifactId>boot-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>boot-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>boot-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>boot-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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.访问测试接口

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存