初学SpringBoot--ch08-SpringBoot 集成 Dubbo

1.1 公共项目

创建Maven项目:ch14-interface-api

1.1.1 项目坐标

  <groupId>com.suyv</groupId>
  <artifactId>ch14-interface-api</artifactId>
  <version>1.0.0</version>

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依赖

<dependencies>
        <!--加入公共依赖-->
        <dependency>
            <groupId>com.suyv</groupId>
            <artifactId>ch14-interface-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--加入Dubbo依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--zookeeper依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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;

/**
 * 使用Dubbo中的注解暴露服务
 */
@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;

/**
 * 启用Dubbo
 */
@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依赖

<dependencies>
        <!--加入公共依赖-->
        <dependency>
            <groupId>com.suyv</groupId>
            <artifactId>ch14-interface-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--加入Dubbo依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--zookeeper依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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 执行测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

憨憨浩浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值