Dubbo Learning Service Provider, dubbo learning provider
1. Introduction
Here we mainly record how to build a spring framework and provide a dubbo service, including detailed steps.
2. Detailed steps
2.1 project directory structure
2.2 create a maven Project
New --> Web Project, select Add maven support, and Add maven support. The details are as follows:
After creating a maven project, you can deploy it to tomcat to test whether the project can be accessed.
2.3 add maven dependency
Modify pom. xml and load the basic spring jar package and dubbo jar package as follows:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-version>4.1.6.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2-SNAPSHOT</version> </dependency> </dependencies>
2.4 modify the applicationContext-base.xml configuration file and add the dubbo configuration 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: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jee = "http://www.springframework.org/schema/jee" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: d = "http://code.alibabatech.com/schema/dubbo" xsi: schemaLocat Ion = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework. Org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <! -- Provider application information for dependency calculation --> <d: application name = "dubbo_provider"/> <! -- Expose services on port 20880 using dubbo protocol --> <d: protocol name = "dubbo" port = "20880"/> <! -- Use the zookeeper cluster registration center to expose the service address -->
<! -- The pseudo cluster method is used here. For details, refer to the "zookeeper initial *********" blog section -->
<D: registry address = "zookeeper: // 192.168.1.102: 2181? Backup = 192.168.1.102: 2182,192.168 .1.102: 2183 "default =" true "/> <! -- Declare the service interface to be exposed --> <d: service interface = "com. wei. interfaces. DemoService" ref = "demoService"/> <! -- Implement services like local services --> <bean id = "demoService" class = "com. wei. services. DemoServiceImpl"/> </beans>
2.5 modify web. xml as follows:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*.xml</param-value> </context-param>
2.6 compile the dubbo interface released externally, as follows:
Package com. wei. interfaces;/*** pack separately * @ author wed **/public interface DemoService {public String sayHello (String name );}
2.7 compile implementation classes for the dubbo interface released externally, as shown below:
package com.wei.services;import com.wei.interfaces.DemoService;public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "hello "+ name; }}
2.8 start the service and view the dubbo console as follows:
You can see that there is already ip information on the provider tab page. Click in to view the corresponding service information.
2.8 notes:
(1) Implement an interfaces jar package separately, which will be used by consumers later.
(2) The zookeeper address used in this step is the zookeeper pseudo cluster mode configured in the previous step. If multiple nodes are not configured, you can also configure it as a single node or a multicast registration center.