【dubbo系列】 03-基于注解的配置

03 dubbo 系列 | 基于注解的配置

前文已经介绍了基于spring xml 配置 dubbo,本文将介绍基于注解的方式配置dubbo。要想支持注解的配置,dubbo的版本必须2.6.3版本以上。

provider 的配置

  • 应用共享配置

这是一个properties配置文件,用于配置dubbo的一个基本属性:如application、registry等。

#dubbo-provider.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
  • Service注解暴露服务
package com.joyxj.dubbo.demo.annotation.impl;

import com.joyxj.dubbo.demo.api.DemoService;
import org.apache.dubbo.config.annotation.Service;

/**
 * 基于注解的demo实现类
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */
@Service
public class AnnotationDemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "annotation: hello, " + name;
    }
}

::: tip

这里需要特别的是:Service注解是dubbo提供的注解,而不是spring中的Service注解,千万不要import错误。

:::

  • 配置类

该类主要配置扫描路径

package com.joyxj.dubbo.demo.annotation.config;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 *  提供者配置类
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */

@Configuration
@EnableDubbo(scanBasePackages = "com.joyxj.dubbo.demo.annotation.impl")
@PropertySource("classpath:/dubbo-provider.properties")
public class AnnotationProviderConfig {
}

  • 启动类
package com.joyxj.dubbo.demo.annotation.bootstrap;

import com.joyxj.dubbo.demo.annotation.config.AnnotationProviderConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 *  提供者启动类
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */
public class AnnotationProviderBootstrap {

    public static void main(String[] args) throws Exception {

        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(AnnotationProviderConfig.class);
        context.start();
        System.in.read();

    }
}

consumer 配置

与provider的配置基本上是一致的。

  • 应用共享配置
#dubbo-provider.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
  • Reference 注解引用服务
package com.joyxj.dubbo.demo.annotation.action;

import com.joyxj.dubbo.demo.api.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

/**
 * 【功能描述】
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */
@Component("annotationDemoAction")
public class AnnotationDemoAction {


    @Reference
    private DemoService demoService;

    public String sayHello(String name) {

        return demoService.sayHello(name);
    }
}

  • 配置类

定义扫描类和配置文件

package com.joyxj.dubbo.demo.annotation.config;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 消费者配置
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(value = {"com.joyxj.dubbo.demo.annotation.action"})
@EnableDubbo(scanBasePackages = {"com.joyxj.dubbo.demo.annotation.action"})
public class AnnotationConsumerConfig {
}

这里关键的注解是EnableDubbo。

  • 启动类
package com.joyxj.dubbo.demo.annotation.bootstrap;

import com.joyxj.dubbo.demo.annotation.action.AnnotationDemoAction;
import com.joyxj.dubbo.demo.annotation.config.AnnotationConsumerConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 【功能描述】
 *
 * @author xiaoj
 * @version 1.0
 * @date 2019-04-24
 */
public class AnnotationConsumerBootstrap {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(AnnotationConsumerConfig.class);
        context.start();
        AnnotationDemoAction demoAction
                = (AnnotationDemoAction)context.getBean("annotationDemoAction");
        System.out.println(demoAction.sayHello("xiaojun"));
    }
}


   转载规则


《【dubbo系列】 03-基于注解的配置》 孤独如梦 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
代码的坏味道 代码的坏味道
前言代码的坏味道出自马丁.福勒《重构-改善既有代码的设计》一书,说明你的代码不够好,需要重构才能让代码变成干净的代码。下文主要介绍常见的一些代码坏味道,而这些坏味道,不限定于某种语言。也就是说任何一种语言都可能出现下列的代码坏味道。 神秘命
2019-06-02
下一篇 
【dubbo系列】 04-dubbo 架构 【dubbo系列】 04-dubbo 架构
04 dubbo系列 | dubbo 架构首先来看一张dubbo官网提供的一张架构图: 各个节点角色的说明(架构图中的方框): Provider: 暴露服务的服务提供方 Consumer: 调用远程服务的服务消费方 Registry:
2019-05-31
  目录