欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot整合dubbo的简单案例

程序员文章站 2022-04-06 12:58:51
springboot整合dubbo的简单案例: 1、开发dubbo服务接口 2、开发dubbo服务提供者 3、开发dubbo服务消费者 ......

使用框架:

  jdk 1.8

  springboot-2.1.3 

  dubbo-2.6

  spring-data-jpa-2.1.5

一、开发dubbo服务接口:

按照dubbo官方开发建议,创建一个接口项目,该项目只定义接口和model类;

1、创建springboot工程 spring-boot-demo-dubbo-interface

坐标:

<groupid>com.example</groupid>
<artifactid>spring-boot-demo-dubbo-interface</artifactid>
<version>0.0.1-snapshot</version>

添加spring-data-jpa 依赖:

<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>

2、创建model

package com.example.demo.model;

@entity public class user implements serializable{ private static final long serialversionuid = 1l; @id @generatedvalue private long id; private string username; private string password; private int age; public long getid() { return id; } //省略set get 方法

3、创建接口:

package com.example.demo.service;

import com.example.demo.model.user;

public interface userservice {
    
    public void save(user user);
    
    public string sayhello(string word);
}

4、使用命令  clean install   打包安装到maven仓库。

 

 

阿里巴巴提供的dubbo集成springboot开源项目;

参考文档:

https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/readme_cn.md

本工程采用该项目的jar包进行继承:

<dependency>
<groupid>com.alibaba.boot</groupid>
<artifactid>dubbo-spring-boot-starter</artifactid>
<version>0.2.0</version>
</dependency>

 

二、开发dubbo服务提供者:

1、创建一个springboot项目spring-boot-demo-dubbo-provider并配置好相关的依赖;

pom.xml

<dependencies>
        <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>
        
        
        <!-- 加入springboot与dubbo集成的起步依赖 -->
        <dependency>
            <groupid>com.alibaba.boot</groupid>
            <artifactid>dubbo-spring-boot-starter</artifactid>
            <version>0.2.0</version>
        </dependency>

        <!-- 由于使用了zookeeper作为注册中心,则需要加入zookeeper的客户端jar包: -->
        <dependency>
            <groupid>com.101tec</groupid>
            <artifactid>zkclient</artifactid>
            <version>0.10</version>
        </dependency>
        
        <!-- spring-data-jpa -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-jpa</artifactid>
        </dependency>

        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <scope>runtime</scope>
        </dependency>
        
        <!-- 添加接口服务 -->
        <dependency>
            <groupid>com.example</groupid>
            <artifactid>spring-boot-demo-dubbo-interface</artifactid>
            <version>0.0.1-snapshot</version>
        </dependency>
    </dependencies>

2、在springboot的核心配置文件application.properties中配置dubbo的信息:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?servertimezone=utc&useunicode=true&characterencoding=utf-8&usessl=true
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.mysql5innodbdialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

# 访问端口
server.port=8080
# dubbo配置
dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://192.168.146.128:2181

3、开发编写dubbo的接口实现类:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;

import com.alibaba.dubbo.config.annotation.service;
import com.example.demo.model.user;
import com.example.demo.repository.userrepository;

@component //注册为spring bean
@service // 这注解是dubbo提供的 
public class userserviceimpl implements userservice {
    
    @autowired
    private userrepository userrepository;

    @override
    public void save(user user) {
        userrepository.save(user);
    }

    @override
    public string sayhello(string word) {
        return word;
    }
}

4、入口main程序启动dubbo服务提供者:添加注解 @enabledubbo

package com.example.demo;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

import com.alibaba.dubbo.config.spring.context.annotation.enabledubbo;

@springbootapplication
@enabledubbo
public class springbootdemodubboproviderapplication {

    public static void main(string[] args) {
        springapplication.run(springbootdemodubboproviderapplication.class, args);
    }

}

 启动main ,服务发布到zookeeper 注册中心。

 

三、开发dubbo服务消费者:

1、创建一个springboot项目spring-boot-demo-dubbo-consumer并配置好相关的依赖;

2、加入springboot与dubbo集成的起步依赖:(pom.xml 配置同上)

 注意: 服务提供者 和 消费者都要配置 服务接口依赖 

3、在springboot的核心配置文件application.properties中配置dubbo的信息:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?servertimezone=utc&useunicode=true&characterencoding=utf-8&usessl=true
spring.datasource.username=root
spring.datasource.password=root

# web\u670d\u52a1\u7aef\u53e3
server.port=8081
# dubbo\u914d\u7f6e
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://192.168.146.128:2181 

 

4、编写一个controller类,调用远程的dubbo服务:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

import com.alibaba.dubbo.config.annotation.reference;
import com.example.demo.model.user;
import com.example.demo.service.userservice;

@restcontroller
public class usercontroller {
    
    @reference //该注解是dubbo提供的
    private userservice userservice;
    
    @requestmapping("/say")
    public string sayhello(string name) {
        return userservice.sayhello(name);
    }
    
    @requestmapping("/save")
    public void save() {
        user u = new user();
        u.setage(20);
        u.setpassword("123");
        u.setusername("zheng");
        userservice.save(u);
    }
}

 

 

5、启动类添加 开启dubbo 注解 @enabledubbo

package com.example.demo;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import com.alibaba.dubbo.config.spring.context.annotation.enabledubbo;

@springbootapplication
@enabledubbo
public class springbootdemodubboconsumerapplication {
    public static void main(string[] args) {
        springapplication.run(springbootdemodubboconsumerapplication.class, args);
    }
}

 

  6、启动main方法。

  7、调用远程接口:

http://localhost:8081/say?name=hello

一个简单的springboot基于dubbo的服务接口开发完成。