provider
service notes exposed Services
@Service public class AnnotationServiceImpl implements AnnotationService { @Override public String sayHello(String name) { return "annotation: hello, " + name; } }
PS: @Service very easy to use the wrong notes, there are two packages have @Service notes:
com.alibaba.dubbo.config.annotation.Service : used to label dubbo interface class of foreign exposure.
org.springframework.stereotype.Service : according to the service label for separate blocks of Service implementation class corresponding to the service (such as a plurality of dubbo method may call block service business layer, these service implementation class to use Spring annotations ).
Increase application sharing configuration
# Set the register connected to the central information Manufacturer
# dubbo-provider.properties
dubbo.application.name = Annotation-Provider
dubbo.registry.address = ZooKeeper: //127.0.0.1: 2181
dubbo.protocol.name = Dubbo
dubbo.protocol .port = 20880
Spring specified scan path
@Configuration @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl") @PropertySource("classpath:/spring/dubbo-provider.properties") static public class ProviderConfiguration { }
The PS: @Configuration used to define the configuration class, alternative xml configuration file, the internal class annotated with one or more methods are annotated @Bean,
These methods will be scanned or AnnotationConfigWebApplicationContext AnnotationConfigApplicationContext class, and with
To build bean definition, initialization Spring container.
@EnableDubbo Open Dubbo annotation function, scanBasePackages properties configuration package scanning path for scanning and registration bean
@PropertySource object annotation is to load the specified file attribute, the value stored in the configuration file to the properties of the Environment in Spring, Environment interface provides a method to read the value in the configuration file.
consumer
Reference
Notes referral service
@Component("annotationAction") public class AnnotationAction { @Reference private AnnotationService annotationService; public String doSayHello(String name) { return annotationService.sayHello(name); } }
The PS: @Component (Common pojo instantiated into the spring container, corresponding to the configuration file <bean id = "" class =refers to the various components,
That is when our class does not belong to a variety of classification when (not part of @ Controller, @ Services, etc. when), we can use @Component to label this class.
Example:<context:component-scan base-package=”com.*”>
The above example is an example of the introduction of Component assembly, wherein the base-package represented for all the sub-packets to be scanned.
@Reference and @ Autowired, @ Resource annotation function is similar for dependency injection, injection is generally object distributed remote service, you need to configure the use dubbo
Increase application sharing configuration
# Configure registry information of consumers connected
# dubbo-consumer.properties
dubbo.application.name = Annotation-Consumer
dubbo.registry.address = ZooKeeper: //127.0.0.1: 2181
dubbo.consumer.timeout = 3000
Spring specified scan path
@Configuration @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action") @PropertySource("classpath:/spring/dubbo-consumer.properties") @ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"}) static public class ConsumerConfiguration { }
The PS: @ComponentScan mainly defined scan path to find out the identity of the need to assemble the class is automatically fitted into the spring bean container.
Call service
public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); context.start(); final AnnotationAction annotationAction = (AnnotationAction) context.getBean("annotationAction"); String hello = annotationAction.doSayHello("world"); }