当前位置: 首页 > 图灵资讯 > 技术篇> 集成Dubbo

集成Dubbo

来源:图灵教育
时间:2023-06-07 09:47:37

1.75简介Dubbo

看看SpringBoot继承Dubo的文档

阿里巴巴提供dubo集成springboot开源项目,

可以在GitHub上httpsps://github.com/apache/dubbo-spring-boot-project查看入门教程

2.实现步骤762.1创建公共项目766公共项目

根据Dubbo的官方开发建议,该项目只定义接口和model类,创建一个接口项目。

这个项目是一个普通的maven项目

项目名称:course12

pom.xml76
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.bjpowernode</groupId>    <artifactId>course12</artifactId>    <version>1.0.0</version></project>
实体Student766
package com.bjpowernode.model;import java.io.Serializable;//实体类 76public class Student implements Serializable {    private static final long serialVersionUID = 1901229007746699151L;    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 +                '}';    }}
StudentService766服务接口
package com.bjpowernode.service;import com.bjpowernode.model.Student;//服务接口  76public interface StudentService {        Student queryStudent(Integer id);}
2.2777服务提供商的服务提供商

在api项目中实现接口

项目名称:course12_12代码

使用Springbootinitializer创建项目,不选择依赖

pom.xml77
<?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.11</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bjpowernode</groupId>    <artifactId>course12_</artifactId>    <version>1.0.0</version>    <name>course12_</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!--gav加入公共项目 我们的course12gav  77-->        <dependency>            <groupId>com.bjpowernode</groupId>            <artifactId>course12</artifactId>            <version>1.0.0</version>        </dependency>        <!--依赖dubbo  77-->        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-spring-boot-starter</artifactId>            <version>2.7.8</version>        </dependency>        <!--zookeper依赖于-->        <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>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
application.properties79
#配置服务名称 dubbo:application name="名称"   79spring.application.name=studentservice-provider#配置扫描包,扫描的是@DubboServicedubbo.scan.base-packages=com.bjpowernode.service#配置Dubbo协议#dubbo.protocol.name=dubbo#dubbo.protocol.port=注册中心dubbo20881#.registry.address=zookeeper://localhost:2181
StudentServiceimpl78
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服务提供商  接口实现类  78/** * 在dubo中使用@duboservice注释暴露服务  78 */@Component ///可以不加@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)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;    }}
Provideraplication8启动
package com.bjpowernode;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;////dubbo服务提供商  80/** * @Enabledubo注释 使用Dubo * @EnableDubboConfig * @DubboComponentScan */@SpringBootApplication@EnableDubbopublic class ProviderApplication {    public static void main(String[] args) {        SpringApplication.run(ProviderApplication.class, args);    }}

dubbo提供商或消费者项目的启动是,有日志提示81

集成Dubbo_maven

依赖SLF4j的日志多次加入。只需要依赖一次。

解决方案:消除多余的SLF4j依赖,供应商和消费者项目都需要这样做

course12_1pom.在xml中修改

<!--zookeper依赖于-->        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-dependencies-zookeeper</artifactId>            <version>2.7.8</version>            <type>pom</type>            <exclusions>                <exclusion>                    <groupId>org.slf4j</groupId>                    <artifactId>slf4j-log4j12</artifactId>                </exclusion>            </exclusions>        </dependency>
2.3消费者82

项目名称:course12_2

使用Springbootinitializer创建项目,选择web依赖

pom.xmldubbo依赖82
<?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.11</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bjpowernode</groupId>    <artifactId>course12_2</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>course12_2</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!--起步依赖web  82-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!--gav加入公共项目 我们的course12gav  82-->        <dependency>            <groupId>com.bjpowernode</groupId>            <artifactId>course12</artifactId>            <version>1.0.0</version>        </dependency>        <!--依赖dubbo  82-->        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-spring-boot-starter</artifactId>            <version>2.7.8</version>        </dependency>        <!--zookeper依赖于  82-->        <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>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

application.properties82

#指定服务名称   82spring.application.name=consumer-application#指定注册中心dubbo.registry.address=zookeeper://localhost:2181
创建Dubocontroller82
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;消费者//Dubbo  控制类 82@RestControllerpublic class DubboController {    //使用@Duboreference注释    //引用远程服务创建良好的代理对象,注入studentservice  82    @DubboReference(interfaceClass = StudentService.class,version = "1.0")    private StudentService studentService;    @GetMapping("/query")    public String queryStudent(){        Student student = studentService.queryStudent(1001);        return 调用远程接口,获取对象:“+student;    }}
2.4测试83

这里使用zookeeper。如果没有安装,请安装

我的在E:\java\tools\apache-zookeeper-3.5.5-bin
进入bin双击zkserverver.cmd启动

zookeper启动后进入项目,首先运行服务提供商Provideraplication运行消费者Consumeraplication

最后,访问浏览器http://localhost:8080/query

集成Dubbo_spring_03

2.5一些小细节844

消费者Dubocontroller不使用interfaceclass,默认是引用类型的数据类型来解释接口类型

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;消费者//Dubbo  控制类 82@RestControllerpublic class DubboController {    //使用@Duboreference注释    //引用远程服务创建良好的代理对象,注入studentservice  82    //@DubboReference(interfaceClass = StudentService.class,version = "1.0")    /**     * 不使用interfaceClass,默认的就是 引用类型的 数据类型  84     */    @DubboReference(version = "1.0")    private StudentService studentService;    @GetMapping("/query")    public String queryStudent(Integer id){        Student student = studentService.queryStudent(id);        return 调用远程接口,获取对象:“+student;    }}

集成Dubbo_Dubbo_04

@Component不需要84

StudentServiceimple接口实现

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服务提供商  接口实现类  78/** * 在dubo中使用@duboservice注释暴露服务  78 *///@Component ///可以不加@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)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;    }}

 

集成Dubbo_Dubbo_05

上一篇:

java中equals与matches区别

下一篇:

集成Redis