Dubbo is a distributed service framework. Since it is a service, there must be service providers and service invokers.
Next, let's write a service provider. Engineering still uses the project in the introduction to spring 4 in the previous article.
We add dubbo dependencies to pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
Write the following categories:
package com.mm.service; public interface WeatherService { public String getMessage(String city); }
package com.mm.service.impl; import com.mm.service.WeatherService; public class WeatherServiceImpl implements WeatherService{ @Override public String getMessage(String city) { return city+"It's sunny and partially showered."; } }
Create a new spring configuration file (provider.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"> <!-- Providers apply information for computing dependencies --> <dubbo:application name="mm-weather" /> <!-- Use multicast Radio Registry Exposure Service Address --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- use dubbo Protocol Exposure Service at Port 20880 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- Declare service interfaces that need to be exposed --> <dubbo:service interface="com.mm.service.WeatherService" ref="weatherService" /> <!-- And local bean Implementing services as well --> <bean id="weatherService" class="com.mm.service.impl.WeatherServiceImpl" /> </beans>
Create a new console program for service startup
package com.mm.main.dubbo; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProvider { public static void main(String[] args) throws IOException{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"config/provider.xml"}); context.start(); System.in.read(); // press any key to exit } }
Then, we start the service provider and find no error message. The service should be started successfully.
Next, let's write a service caller, officially called a consumer. For convenience, I simply configure maven and tomcat by copying a copy of the code directly into the new workspace. And delete the corresponding implementation class. The code structure is as follows:
The consumer.xml file is as follows:
<?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"> <!-- The consumer application name, used to calculate dependencies, is not a matching condition, not the same as the provider --> <dubbo:application name="mm-weather-consumer" /> <!-- Use multicast Radio Registry Exposure Discovery Service Address --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- Generate remote service proxies that can be local and local bean Same use demoService --> <dubbo:reference id="weatherService" interface="com.mm.service.WeatherService" /> </beans>
Create a new console program for service invocation
package com.mm.main.dubbo; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mm.service.WeatherService; public class DubboConsumer { public static void main(String[] args) throws IOException{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"config/consumer.xml"}); WeatherService demoService = (WeatherService )context.getBean("weatherService"); // Getting a remote service agent String hello = demoService.getMessage("Beijing"); // Executing remote methods System.out.println( hello ); // Display the result of the call } }
The console output is as follows:
At the same time, we will find the following information in the provider's console:
Additional additions:
Errors are reported when dubbo tags are used in spring configuration files. Actually, it should not be affected. It is estimated that it is the problem of eclipse file validation. But it looks a little uncomfortable, so...
Firstly, dubbo-2.5.3.jar is found in maven's local warehouse and xsd file is decompressed as follows:
Open eclipse configuration
Change the key value to http://code.alibabatech.com/schema/dubbo/dubbo.xsd
Then, right-click provider.xml and validate. After a long wait, the error message disappears.