Springboot整合Dubbo

前:

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

1、Springboot空项目创建

14899865-89cbe0dfb98a249e.png

next

 

14899865-314b05e2a1c6b14b.png

next

 

14899865-2cafe92eebea3e20.png

next

 

14899865-1a9f4e00d60cabc9.png

web

 

14899865-7e3eba18c5458ada.png

创建完成

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);}
}

14899865-413d247c20c16754.png

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

 

14899865-cd1976a270defb17.png

 

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

 

14899865-4099ff59e59fe105.png

参考文章

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值