美文网首页
Springboot整合Dubbo

Springboot整合Dubbo

作者: 鸡龙 | 来源:发表于2019-07-13 20:51 被阅读0次

前:

为什么我要用Springboot和Dubbo来搭建这个demo,因为springboot自身和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。同时它集成了大量常用的第三方库配置,Spring Boot应用中这些第三方库几乎可以是零配置的开箱即用(out-of-the-box),大部分的 Spring Boot 应用都只需要非常少量的配置代码(基于 Java 的配置),不需要配置提供者和消费者的xml文件,开发者能够更加专注于业务逻辑。并且项目不需要发送到tomcat中使用SpringApplication.run就可以在Idea中充当容器启动服务。

1、Springboot空项目创建

next
next
next
web
创建完成

2、springboot-dubbo-provider模块创建

如创建空项目时在项目下创建模块,创建后模块中的pom.xml中追加dubbo和zookeeper的依赖

       <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>

在项目的resources中application.properties中定义参数

spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.example.springboot.dubbo.provider
server.port=8082

创建demo接口version是重点,不能漏
com.example.springboot.dubbo.api.SayHello

package com.example.springboot.dubbo.api;

public interface SayHello {
    String sayHello(String name);
}

实现接口
con.example.springboot.dubbo.provider.SayHelloImpl

package com.example.springboot.dubbo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.springboot.dubbo.api.SayHello;
@Service(version = "1.0.0")
public class SayHelloImpl implements SayHello {
    @Override
    public String sayHello(String name){
        return "Hello " + name + "!";
    }
}

启动服务提供者

package com.example.springboot.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

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

3、springboot-dubbo-consumer模块创建

与provider模块一样的创建方式,配置pom.xml,追加dubbo依赖和provider

<dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>springboot-dubbo-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

配置resources中application.properties

spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.example.springboot.dubbo.consumer
server.port=8083

创建consumer接口,version是重点。
com.example.springboot.dubbo.api.SayHello

package com.example.springboot.dubbo.api;
public interface SayHello {
    String sayHello(String name);
}

实现接口

package com.example.springboot.dubbo.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.springboot.dubbo.api.*;
import org.springframework.stereotype.Component;

@Component
public class SayService  {
    @Reference
            (version = "1.0.0")
    SayHello sayHello;
    public String say (String name) {
        return sayHello.sayHello(name);
    }
}

启动服务消费者

package com.example.springboot.dubbo.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Client {
    @Autowired
    SayService sayService;
    @RequestMapping("/hello")
    public String say(@RequestParam ("name") String name) {
        return sayService.say(name);
    }
    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context = SpringApplication.run(Client.class, args);
    }
}

启动消费者,查看dubbo admin



调用消费者端口,传入参数,成功回显。


参考文章

1、IDEA构建springboot
2、springboot优点
3、springboot整合dubbo

相关文章

  • io.dubbo.springboot版本不兼容dubbo-2.

    遇到问题:在dubbo整合springboot的时候,使用io.dubbo.springboot的jar包,配合的...

  • SpringBoot 整合 Dubbo错误收集

    SpringBoot 整合 Dubbo CuratorFrameworkFactory找不到 错误信息:java....

  • spring boot 整合dubbo

    #项目介绍 Springboot 整合 Dubbo/ZooKeeper 码云地址:https://gitee.co...

  • SpringBoot整合dubbo

    1、添加依赖 2、生产者配置文件 3、消费者配置文件 4、定义service接口 5、生产者实现service接口...

  • SpringBoot整合dubbo

    首先创建一个多模块Maven项目,再创建一个生产者子项目和一个消费者子项目。父模块Maven POM文件如下: 生...

  • SpringBoot Dubbo 整合

    官方参考文档http://dubbo.apache.org/zh-cn/docs/user/configurati...

  • SpringBoot整合dubbo

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,...

  • SpringBoot整合Dubbo

    前期准备:下载安装zookeeper作为服务注册中心 安装步骤 解压 将zoo_sample.cfg文件名修改为z...

  • springboot整合dubbo

    导入依赖 服务调用者和服务发布者都需要引入该依赖 该依赖不定时更新,可以关注最新版本http://maven.al...

  • Springboot整合dubbo

    要的工具,zookeeper,dubbo首先Pom,yaml配置一下 然后下载zookeeper,dubbo前往g...

网友评论

      本文标题:Springboot整合Dubbo

      本文链接:https://www.haomeiwen.com/subject/tkgmkctx.html