Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/v5tech/dubbo-example

dubbo example
https://github.com/v5tech/dubbo-example

dubbo dubbo-example

Last synced: 3 months ago
JSON representation

dubbo example

Awesome Lists containing this project

README

        

# dubbo-example

dubbo 分布式服务配置案例

升级到dubbox 2.8.4

dubbox 2.8.4编译安装

```
https://github.com/dangdangdotcom/dubbox/archive/dubbox-2.8.4.zip
修改根pom中curator_version版本为2.6.0
mvn install -Dmaven.test.skip=true
```

### 1. 项目结构介绍

dubbo-service 公共接口服务

dubbo-provider 公共接口服务实现(dubbo provider) 服务提供者

dubbo-consumer (dubbo consumer) dubbo服务消费者

### 2. 具体描述

* dubbo-service 为公共服务接口,在该模块中只声明对外提供的接口,在dubbo provider和 dubbo consumer均有引用

* dubbo-provider 公共接口服务实现,服务提供者.为dubbo consumer提供服务。

* 示例代码如下

```java
import net.aimeizi.dubbo.entity.User;
import net.aimeizi.dubbo.service.UserService;

import com.alibaba.dubbo.config.annotation.Service;

@Service
public class UserServiceImpl implements UserService {

@Override
public User save(User user) {
user.setUserId(++UserIdGenerator.id);
return user;
}

}
```
* dubbo provider 核心配置

```xml

```
注:``配置会扫描该包下的@Service(com.alibaba.dubbo.config.annotation.Service)注解. 这里的服务注入使用dubbo @Service注解

* maven依赖

```xml

net.aimeizi
dubbo-service
1.0-SNAPSHOT

org.springframework
spring-context
${spring.version}

com.alibaba
dubbo
2.8.4


spring
org.springframework

org.apache.zookeeper
zookeeper
3.4.6


com.sun.jmx
jmxri


com.sun.jdmk
jmxtools


javax.jms
jms

com.github.sgroschupf
zkclient
0.1

```

* dubbo-consumer 消费者.这里只依赖公共服务接口,不需要直接依赖dubbo provider

* 示例代码如下

```java
import com.alibaba.dubbo.config.annotation.Reference;
import net.aimeizi.dubbo.service.DemoService;
import net.aimeizi.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
*
* dubbo 消费者
*
* @Reference 注解需要在 dubbo consumer中做如下配置
*
*
*
*
*
*
* 若要使用@Autowired或@Resource注解需要显式声明bean
*
* 使用@Autowired或@Resource注解时需要使用dubbo:reference来声明
*
*
*
* 以上的配置均需要在spring mvc的DispatcherServlet配置中显式配置dubbo consumer的配置.如/WEB-INF/applicationContext-dubbo-consumer.xml 否则在Controller中服务报NullPointException
*
* mvc-dispatcher
* org.springframework.web.servlet.DispatcherServlet
*
* contextConfigLocation
* /WEB-INF/applicationContext*.xml,/WEB-INF/mvc-dispatcher-servlet.xml
*
* 1
*
*
*/
@Controller
public class HelloController {

@Reference
//@Autowired
//@Resource
private DemoService demoService;

@Reference
//@Autowired
//@Resource
private UserService userService;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
}
```

注意:

① @Reference 注解需要在 dubbo consumer配置文件中做如下配置
```xml

```

② 若要使用@Autowired或@Resource注解需要显式声明bean

```xml

```

③ 以上的配置均需要在spring mvc的DispatcherServlet配置中显式配置dubbo consumer的配置.如/WEB-INF/applicationContext-dubbo-consumer.xml 否则在Controller中服务报NullPointException

```xml

mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/applicationContext*.xml,/WEB-INF/mvc-dispatcher-servlet.xml

1

```

### 3. 演示

完整演示

![](Screenshots/1.gif)

错误演示Controller中服务报NullPointException

![](Screenshots/2.gif)

使用@Autowired或@Resource注解

![](Screenshots/3.gif)

dubbo管理控制台演示

![](Screenshots/dubbo.gif)

# 与我联系

* QQ:*184675420*

* Email:*sxyx2008#gmail.com*(#替换为@)

* HomePage:*[aimeizi.net](http://aimeizi.net)*

* Weibo:*[http://weibo.com/qq184675420](http://weibo.com/qq184675420)*(荧星诉语)

* Twitter:*[https://twitter.com/sxyx2008](https://twitter.com/sxyx2008)*

# License

MIT

Copyright (c) 2015 雪山飞鹄