目录
注解配置
服务提供方
Service
注解暴露服务
-
import com.alibaba.dubbo.config.annotation.Service; @Service(timeout = 5000) public class AnnotateServiceImpl implements AnnotateService { // ... }
javaconfig形式配置公共模块
@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("zookeeper://127.0.0.1:2181"); registryConfig.setClient("curator"); return registryConfig; } }
指定dubbo扫描路径
@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service.impl")
public class ProviderTestApp {
// ...
}
服务消费方
Reference
注解引用服务
public class AnnotationConsumeService { @com.alibaba.dubbo.config.annotation.Reference public AnnotateService annotateService; // ... }
javaconfig形式配置公共模块
@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("zookeeper://127.0.0.1:2181"); registryConfig.setClient("curator"); return registryConfig; } }
指定dubbo扫描路径
@SpringBootApplication @DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service") public class ConsumerTestApp { // ... }