SpringBoot:SpringBoot集成Dubbo

需要创建3个Model,服务接口,服务提供者、服务消费者

 

首先:创建Model接口工程:

 

 Student类:

package com.bjpowernode.model;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 271104779966662895L;
    private Integer id;
    private String name;
    private Integer 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;
    }

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

StudentService接口:

package com.bjpowernode.service;

import com.bjpowernode.model.Student;

public interface StudentService {
    Student queryStudent(Integer id);
}

创建Model选择SpringBoot方式创建

 

 

pom.xml:加入Dubbo和zookeeper依赖:

 

<?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.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>023-service-provider</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--加入公共项目的jav-->
        <dependency>
            <groupId>com.bjpowernode</groupId>
            <artifactId>022-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>

        <!--zookeepoer注册中心-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
        </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>

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

</project>

 接口的实现类:

StudentServiceImpl:

package com.bjpowernode.service.impl;

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

/*使用dubbo的注解暴露服务
*注解@DubboService
* */
@Component //注解创建对象放到容器中,可以不加,使用下面的一个注解@DubboService,就可以把对相关注入到容器当中
@DubboService(interfaceClass = StudentService.class,version = "1.0")
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(22);
        }
        return student;
    }
}

在springbooot配置文件中:进行dubbo的外部化配置:

application.properties:

#配置服务的名称 相当于dubbo:application name="名称"
spring.application.name=studentservice-provider

#配置扫描的包,扫描的@DubboService
dubbo.scan.base-packages=com.bjpowernode.service

#配置dubbo协议和端口号
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20881

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

主启动类加上启动dubbo的注解:

ProviderApplication:

package com.bjpowernode;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*@EnableDubbo 启用Dubbo
* 包含:
*   @EnableDubboConfig
*   @DubboComponentScan
* */
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {

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

}

运行项目会报一个错误:slf4j日志引入多次,在pom.xml中需要排除 

 

<!--zookeepoer注册中心-->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.8</version>
    <type>pom</type>
    <!--上面那个已经包含,这里需要排除slf4j依赖-->
    <exclusions>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

 创建服务的消费者:创建springboot项目的时候选上web依赖

 

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.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>024-consumer</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--加入公共项目的jav-->
        <dependency>
            <groupId>com.bjpowernode</groupId>
            <artifactId>022-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>

        <!--zookeepoer注册中心-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
            <!--上面那个已经包含,这里需要排除slf4j依赖-->
            <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>

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

</project>

 

 创建控制类:DubboController:

package com.bjpowernode.controller;

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

@RestController  // @Controller和@ResponseBody的组合    @ResponseBody:把String返回值当做数据来返回给浏览器
public class DubboController {

    //@DubboReference注解引用远程服务,把创建好的代理对象,注入给studentService
    @DubboReference(interfaceClass = StudentService.class,version = "1.0")
    private StudentService studentService;

    @GetMapping("/query")
    public String queryStudent(){
        Student student = studentService.queryStudent(1001);
        return "调用远程接口,获取对象:"+student;
    }
}

主启动类ConsumerApplication:加上启动Dubbo的注解

package com.bjpowernode;

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

@SpringBootApplication
@EnableDubbo  //注解启用Dubbo
public class ConsumerApplication {

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

}

配置文件application.properties:

#配置服务的名称 相当于dubbo:application name="名称"
spring.application.name=consumer-application

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

在bin目录下启动zookeeper:

 双击zkServer.cmd

 这样注册中心就启动了

 一次启动提供者、消费者,在浏览器地址栏输入:

 DubboController:控制器当中的

@DubboReference(interfaceClass = StudentService.class,version = "1.0")中的

interfaceClass = StudentService.class可以去掉

在更改一下方法传递一个变量参数id

package com.bjpowernode.controller;

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

@RestController  // @Controller和@ResponseBody的组合    @ResponseBody:把String返回值当做数据来返回给浏览器
public class DubboController {

    //@DubboReference注解引用远程服务,把创建好的代理对象,注入给studentService
    //@DubboReference(interfaceClass = StudentService.class,version = "1.0")
    
    @DubboReference(version = "1.0")  //没有使用interfaceClass,默认的就是引用类型的数据类型
    private StudentService studentService;

    @GetMapping("/query")
    public String queryStudent(Integer id){
        Student student = studentService.queryStudent(id);
        return "调用远程接口,获取对象:"+student;
    }
}

 步骤:

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

喵俺第一专栏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值