4. Bootstrapping
Now let's try out the basic features of Dubbo.
This is a minimally invasive framework, and lots of its features depend on external configurations or annotations.
It's officially suggested that we should use xml configuration file because it depends on a Spring container (currently Spring 4.3.10).
We'll demonstrate most of its features using xml configuration.
- 4.1. Multicast Registry - Service Provider
- As a quick start, we'll only need a service provider, a consumer and an"invisible" registry. The registry is invisible because we are using a multicast network.
- In the following example, the provider only says "hi" to its consumers:
- public interface GreetingsService {
- String sayHi(String name);
- }
- public class GreetingsServiceImpl implements GreetingsService {
- @Override
- public String sayHi(String name) {
- return "hi," + name;
- }
- }
- 4.2. Multicast Registry - Service Registration
- Let's now register GreetingsService to the registry. A very convenient way is to use a multicast registry if both providers and consumers are on the same local network provider-App.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
- <dubbo:application name="demo-provider" version="1.0"/>
- <dubbo:registry address="multicast://224.1.1.1:9090"/>
- <dubbo:protocol name="dubbo" port="20880"/>
- <bean id="greetingsService" class="dubbo.test.app.GreetingsServiceImpl"/>
- <dubbo:service interface="dubbo.test.app.GreetingsService"
- ref="greetingsService"/>
- </beans>
- With the beans configuration above, we have just exposed our GreetingsService to an url under dubbo://127.0.0.1:20880 and registered the service to a multicast address specified in <dubbo:registry />.
- In the provider's configuration, we also declared our application metadata, the interface to publish and its implementation respectively by <dubbo:application />, <dubbo:service />and <beans />.
- The dubbo protocol is one of many protocols the framework supports. It is built on top of the Java NIO non-blocking feature and it's the default protocol used.
We'll discuss it in more detail later in this article.
- 4.3. Multicast Registry - Service Consumer
- Generally, the consumer needs to specify the interface to invoke and the address of remote service, and that's exactly what's needed for a consumer(consumer-App.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
- <dubbo:application name="demo-consumer" version="1.0"/>
- <dubbo:registry address="multicast://224.1.1.1:9090"/>
- <dubbo:reference interface="dubbo.test.app.GreetingsService"
- id="greetingsService"/>
- </beans>
- Now everything's set up, let's see how they work in action:
- public class MulticastRegistryTest
- {
- @Before
- public void initRemote()
- {
- ClassPathXmlApplicationContext remoteContext = new ClassPathXmlApplicationContext(
- "resources/provider-app.xml");
- remoteContext.start();
- }
- @Test
- public void givenProvider_whenConsumerSaysHi_thenGotResponse()
- {
- ClassPathXmlApplicationContext localContext = new ClassPathXmlApplicationContext(
- "resources/consumer-app.xml");
- localContext.start();
- GreetingsService greetingsService = (GreetingsService) localContext
- .getBean("greetingsService");
- String hiMessage = greetingsService.sayHi("baeldung");
- assertNotNull(hiMessage);
- assertEquals("hi, baeldung", hiMessage);
- }
- }
- When the provider's remoteContext starts, Dubbo will automatically load GreetingsServiceand register it to a given registry. In this case, it's a multicast registry.
The consumer subscribes to the multicast registry and creates a proxy of GreetingsService in the context. When our local client invokes the sayHi method, it's transparently invoking a remote service.
- We mentioned that the registry is optional, meaning that the consumer could connect directly to the provider, via the exposed port:
- <dubbo:reference interface="com.baeldung.dubbo.remote.GreetingsService"
- id="greetingsService" url="dubbo://127.0.0.1:20880"/>
Basically, the procedure is similar to traditional web service, but Dubbo just makes it plain, simple and lightweight.
来源: http://www.bubuko.com/infodetail-3115598.html