SpringBoot整合Dubbo案例

使用框架:

  1. jdk 1.8
  2. springboot-2.1.3
  3. dubbo-2.6
  4. 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的服务接口开发完成。

原文地址:https://blog.51cto.com/14230003/2406570

时间: 2024-08-30 01:05:06

SpringBoot整合Dubbo案例的相关文章

Springboot整合Dubbo和Zookeeper

Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这使得其与Spring Boot项目整合变得更为简单方便.而Zookeeper在这里充当的是服务注册中心的角色,我们将各个微服务提供的服务通过Dubbo注册到Zookeeper中,然后服务消费者通过Dubbo从Zookeeper中获取相应服务并消费.本文案例的架构图可以简单用下图表示: 本文案例最终项

SpringBoot整合dubbo(yml格式配置)

yml文件 如果只作为服务的消费者不用暴露端口号,扫描的包名根据自己service改 dubbo: application: name: springboot-dubbo-demo #应用名 registry: address: zookeeper://127.0.0.1:2181 #zookeeper地址 # port: 2181 #提供注册的端口 protocol: name: dubbo port: "20889" #dubbo服务暴露的端口 scan: com.bw.ssm.s

Springboot 整合 Dubbo/ZooKeeper

本文提纲 一.为啥整合 Dubbo 实现 SOA 二.运行 springboot-dubbo-server 和 springboot-dubbo-client 工程 三.springboot-dubbo-server 和 springboot-dubbo-client 工程配置详解 一.为啥整合 Dubbo 实现 SOA Dubbo 不单单只是高性能的 RPC 调用框架,更是 SOA 服务治理的一种方案. 核心: 1. 远程通信,向本地调用一样调用远程方法. 2. 集群容错 3. 服务自动发现和

SpringBoot整合dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 以上介绍来源于百度百科,具体dubbo相关可以自行查找资料,本文只是介绍SpringBoot简单整合dubbo. 1.安装zookeeper 1.1 去官网下载,本文以3.4.12 版本为例子http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/ 1.2 下载之后解压ZooKeepe

SpringBoot 整合MyBatis案例详解

SpringBoot约定大于配置的特点,让框架的配置非常简洁,与传统的SSM框架相比,简化了大量的XML配置,使得程序员可以从繁琐的配置中脱离出来,将精力集中在业务代码层面,提高了开发效率,并且后期的维护成本也大大降低. 从源码中可以看到,每一个springboot集成的jar包也是一个maven的子module,springboot已经将相关依赖的jar包自动添加到工程中,不需要开发人员手动去添加了,这也是springboot能够简化配置的一个重要原因. 下面开始说明springboot是如何

springBoot 整合 dubbo 遇到的坑

一.注意springBoot 和 dubbo 之间版本的问题 <?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:/

spring-boot整合dubbo:Spring-boot-dubbo-starter

为什么要写这个小工具 如果你用过Spring-boot来提供dubbo服务,相信使用中有很多"不爽"的地方.既然使用spring boot,那么能用注解的地方绝不用xml配置,这才是spring-boot-style.开个玩笑,真正意思是,spring-boot适合一些简单的.独立的服务,一个大的系统是不适合使用spring-boot来开发.相反,spring-boot适合那些简单服务的搭建. 网上大多数的方法还是使用xml配置,通过@Import注解来引入xml配置. 怎么使用 对于

dubbo学习(三) springboot整合dubbo mybatis mysql

dubbo-admin查看服务和服务提供方配置 服务消费方配置 和web 整合相对复杂一些,常出现的问题是Controller中 有@Reference配置的属性  没注入进来而调用时报null 异常 原文地址:https://www.cnblogs.com/LDDXFS/p/9941203.html

springboot整合dubbo注解方式

工程结构: 主pom <?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/PO