初学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 创建 Controller
    • 1.3.4 修改主启动类 Application
  • 1.4 测试应用
    • 1.4.1 启动zookeeper
    • 1.4.2 运行服务提供者
    • 1.4.3 运行消费者
    • 1.4.4 执行测试

1.1 公共项目

创建Maven项目:ch14-interface-api

1.1.1 项目坐标

  <groupId>com.suyvgroupId>
  <artifactId>ch14-interface-apiartifactId>
  <version>1.0.0version>

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.suyvgroupId>
            <artifactId>ch14-interface-apiartifactId>
            <version>1.0.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>2.7.8version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-dependencies-zookeeperartifactId>
            <version>2.7.8version>
            <type>pomtype>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12artifactId>
                    <groupId>org.slf4jgroupId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        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.suyvgroupId>
            <artifactId>ch14-interface-apiartifactId>
            <version>1.0.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>2.7.8version>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-dependencies-zookeeperartifactId>
            <version>2.7.8version>
            <type>pomtype>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12artifactId>
                    <groupId>org.slf4jgroupId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        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

初学SpringBoot--ch08-SpringBoot 集成 Dubbo_第1张图片

1.4.2 运行服务提供者

初学SpringBoot--ch08-SpringBoot 集成 Dubbo_第2张图片

1.4.3 运行消费者

初学SpringBoot--ch08-SpringBoot 集成 Dubbo_第3张图片

1.4.4 执行测试

初学SpringBoot--ch08-SpringBoot 集成 Dubbo_第4张图片

你可能感兴趣的:(SpringBoot学习,spring,boot,java,分布式)