您的位置:首页 > 运维架构 > 网站架构

原来dubbo发布服务如此简单

2017-09-26 20:48 543 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息