本文介绍注解方式配置dubbo的例子。
服务提供Provider
1. 定义接口Animal和实现类Cat
Animal:
public interface Animal {/*** 描述: 返回动物的叫声*/String sound();
}
Cat:
package com.yt.provider;import com.alibaba.dubbo.config.annotation.Service;@Service(timeout = 5000)
public class Cat implements Animal {public String sound() {return "喵";}
}
@Service: 注解配置,服务提供者的service
2. Provider配置类
package com.yt.provider;import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author: yt* @date: 2021/5/13 21:40* @description:*/
@Configuration
@EnableDubbo(scanBasePackages = "com.yt.provider")
public class ProviderConfiguration {/*** dubbo应用配置*/@Beanpublic ApplicationConfig applicationConfig() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName("myApp");applicationConfig.setOwner("yt");return applicationConfig;}/*** 配置服务提供者*/@Beanpublic ProviderConfig providerConfig() {ProviderConfig providerConfig = new ProviderConfig();providerConfig.setTimeout(1000);return providerConfig;}/*** 注册中心配置 zookeeper*/@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setProtocol("zookeeper");registryConfig.setAddress("localhost:2181");registryConfig.setCheck(false);return registryConfig;}/*** 协议配置 dubbo:20880*/@Beanpublic ProtocolConfig protocolConfig() {ProtocolConfig protocolConfig = new ProtocolConfig();protocolConfig.setName("dubbo");protocolConfig.setPort(20880);return protocolConfig;}
}
@Configuration: 注解标明配置类,@EnableDubbo 指定扫描的包路径。
配置的内容主要有:
- 应用配置 :类似 <dubbo:application 标签
- 服务提供者配置:类似<dubbo:provider‘标签
- 注册中心配置:类似<dubbo:registry 标签
- 协议配置:类似 <dubbo:protocol 标签
3. 启动服务
public class Provider {public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);context.start();System.in.read();}
}
指定配置类之后,就可以启动。
服务消费Consumer
1. 创建服务消费service
package com.yt.consumer;import com.alibaba.dubbo.config.annotation.Reference;
import com.yt.provider.Animal;
import org.springframework.stereotype.Component;@Component
public class ConsumerService {@Referenceprivate Animal animal;public String sound() {return animal.sound();}
}
新建一个service类,加上@Component注解,将对象交给spring管理,之后加上@Reference注解注入Animal接口。
2. 服务消费配置类
package com.yt.consumer;import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;/*** @author: yt* @date: 2021/5/13 21:52* @description:*/
@Configuration
@EnableDubbo(scanBasePackages = "com.yt.consumer")
@ComponentScan(value = {"com.yt.consumer"})
public class ConsumerConfiguration {/*** 应用配置*/@Beanpublic ApplicationConfig applicationConfig() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName("myApp");applicationConfig.setOwner("yt");return applicationConfig;}/*** 消费者配置*/@Beanpublic ConsumerConfig consumerConfig() {ConsumerConfig consumerConfig = new ConsumerConfig();consumerConfig.setTimeout(3000);return consumerConfig;}/*** 注册中心配置*/@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setProtocol("zookeeper");registryConfig.setAddress("localhost:2181");return registryConfig;}
}
跟服务提供者的配置类似。
@ComponentScan(value = {"com.yt.consumer"}) 指定要扫描component的包路径,扫描ConsumerService。
3. 启动服务
public class Consumer {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);context.start();// 从spring容器中取得beanConsumerService consumerService = context.getBean(ConsumerService.class);System.out.println("consumer:" + consumerService.sound());}
}
服务成功启动,并输出结果(记得启动zookeeper):
consumer:喵