该章节将讲述基于Java配置方式配置Dubbo及使用注解方式开发
要阅读该章节,请先阅读SpringBoot整合Dubbo小测

首先我们先来熟悉几个注解

@Reference

包路径为:com.alibaba.dubbo.config.annotation.Reference
你可以近似的理解为Spring IOC容器的@Resource注解,不过它是Dubbo的一部分。
对应的Dubbo相关配置文件为

<dubbo:reference id="demoService" interface="com.noesblog.demo.dubbo.service.DemoService" />

在前一章节我们是使用@Autowired注入DemoService到控制器中,在该章节,将@Autowired替换为@Reference即可

@DubboComponentScan、

Dubbo扫描注解类的注解,简单的来说就是类似于@MapperScan注解的效果。
该注解请加在SpringBoot启动类上,例如:

@DubboComponentScan(basePackages = "com.noesblog.demo.dubbo")
@SpringBootApplication
public class ConsumerApplication { 
//····
}

需要注意的是,该注解需要同时在服务提供方与服务消费方的启动类注解

@Service

注解该类为一个Dubbo服务类,以用于服务消费方使用。
包路径:com.alibaba.dubbo.config.annotation.Service

开始配置

在provider及consumer Module下各创建一个DubboConfiguration类。

provider.DubboConfiguration

package com.noesblog.demo.dubbo.core;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DubboConfiguration {

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("redis://你的Redis服务IP:6379");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

consumer.DubboConfiguration

package com.noesblog.demo.dubbo.core;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DubboConfiguration {

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer-test");
        return applicationConfig;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("redis://你的RedisIP:6379");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

consumer项目变更

控制器注解变更

修改consumer Module下HelloController下的

    @Autowired
    private DemoService demoService;

变更为

    @Reference
    private DemoService demoService;

consumer启动类修改

package com.noesblog.demo.dubbo;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;

@DubboComponentScan(basePackages = "com.noesblog.demo.dubbo")
@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(ConsumerApplication.class,args);
        System.in.read();
    }
}

provider项目变更

服务类注解变更

package com.noesblog.demo.dubbo.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.noesblog.demo.dubbo.service.DemoService;

@Service
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        System.out.println("消费者1执行操作" + name);
        return "Hello" + name;
    }
}

provider启动类变更

package com.noesblog.demo.dubbo;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
@DubboComponentScan(basePackages = "com.noesblog.demo.dubbo.provider")
public class ProviderApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(ProviderApplication.class, args);
        System.in.read();
    }

}

按照此处进行测试即可->测试

发表评论

电子邮件地址不会被公开。