原来dubbo发布服务如此简单 springboot+dubbo

非常感谢http://blog.csdn.net/hulei19900322/article/details/78106718

微服务技术学习 https://www.itkc8.com

Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分业务的架构,使用这种方式可以使各个业务之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。关于注册中心、协议支持、服务监控等内容,详见后面描述。spring-boot是近几年越来越来越流行的库,Spring Boot 可以大大提升使用 Spring 框架时的开发效率。spring-boot-starter-dubbo则是借助spring-boot高效,整合dubbo的实现,让dubbo的使用变得平民化。

快速入门

1.在maven管理的spring-boot项目中引入依赖,(建议使用spring-boot版本1.5以上,1.5以下未测试过)

    <dependency>
        <groupId>com.gitee.reger</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>${spring-boot-starter-dubbo.version}</version>
    </dependency>

2.在spring-boot项目的配置文件’application.yml’中增加dubbo的配置项

服务发布者增加

spring:
  dubbo: 
    application:
      name: demo-provider
    base-package: com.test.dubbo.provider  # dubbo服务发布者所在的包
    registry:
      address: 127.0.0.1                   # zookeeper注册中心的地址
      port: 2181                           # zookeeper注册中心的端口
    protocol:
      name: dubbo
      serialization: hessian2
    provider:
      retries: 0                           # 服务调用重试次数,服务发布者不给重试,让服务调用者自己重试

服务调用者增加

spring:
  dubbo: 
    application:
      name: demo-consumer
    base-package: com.test.dubbo.consumer  # dubbo服务调用者所在的包  
    registry:
      address: 127.0.0.1                   # zookeeper注册中心的地址
      port: 2181                           # zookeeper注册中心的端口
    consumer:
      timeout: 1000 
      check: true                          # 服务启动时检查被调用服务是否可用
      retries: 2                           # 服务调用重试次数 

3. 定义服务接口,

在api项目中增加接口

package com.test.dubbo.service;

public interface DemoService {
    Integer add(Integer a,Integer b);
}

4. 服务提供者

服务提供者项目中增加业务类

package com.test.dubbo.provider;
import com.test.dubbo.service.DemoService;
import com.alibaba.dubbo.config.annotation.Service;

@Service
public class DemoServiceImpl implements DemoService{

    public Integer add(Integer a,Integer b){
        System.err.printf("方法add被调用 %s+%s", a, b);
        System.err.println();
        if(a==null||b==null){
            return 0;
        }
        return a+b;
    }
}

5. 服务调用者

服务调用者项目中增加业务类

package com.test.dubbo.consumer;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.reger.dubbo.annotation.Inject;

import com.test.dubbo.service.DemoService;

@Component
public class DemoConsumer implements CommandLineRunner {

    // 使用兼容注入,可以使用dubbo原生注解@Reference注入
    @Inject DemoService service; 

    @Override
    public void run(String... args){  
        int a=1;
        int b =2;
        System.err.printf("%s+%s=%s", a, b, service.add(a,b));
        System.err.println(); 
    }
}

6.启动服务提供者,启动服务调用者。

服务提供者spring-boot的main方法的示例

package com.test.dubbo.main;

import java.util.concurrent.TimeUnit;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class SpringDubboConfigApplication implements CommandLineRunner {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(SpringDubboConfigApplication.class, args);
        TimeUnit.MINUTES.sleep(10); //提供者main线程暂停10分钟等待被调用
        System.err.println("服务提供者------>>服务关闭");
    }

    @Override
    public void run(String... args) throws Exception {
        System.err.println("服务提供者------>>启动完毕");
    } 
}

服务调用者spring-boot的main方法的类示例

package com.test.dubbo.main;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class SpringDubboConfigApplication implements CommandLineRunner {

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

    @Override
    public void run(String... args) throws Exception {
        System.err.println("服务调用者------>>启动完毕");
    }
}

相关资源

spring-boot-starter-dubbo-example码云地址 
spring-boot-starter-dubbo 码云地址 
dubbo官网 
spring-boot

项目推荐

使用了后端通过jar包发布的rpc协议库,然后与前端app h5 微信交互使用restful api,你或许很有必要使用这个restful文档插件spring-boot-starter-swagger

微服务技术学习 https://www.itkc8.com

 

 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值