DUBBO framework construction

Since this picture is troublesome, if you need a graphic version, please add the q group 216396734 to     
download the source code https://github.com/liuchunxue/dubbo
DUBBO framework construction
1 . Dubbo introduction
     Dubbo is a distributed service framework, dedicated to providing high-performance and transparent RPC remote service invocation scheme, is the core framework of Alibaba SOA service governance scheme
II . Dubbo's construction of
Dubbo framework construction mainly includes the following components:
   1) Registration center installation, here I use zookeeper as the registration center (Registry)
       2) Dubbo monitoring center deployment
       3) Service provider development of exposure service (Provider)
4 ) Service running container development (Container)
5) Service consumption and development

1) Zookeeper installation
   Note: jdk must be installed before installing zookeeper, jdk is best to use version 1.7, otherwise there will be problems in the later monitoring center
   . Download zookeeper from the official website http: //www.apache.org/dist/zookeeper/
   Here I choose version 3.4.9, which is the latest stable version released so far.

Extract it to the /opt/ directory
tar -zxvf zookeeper-3.4.9.tar.gz -C /opt/

switch to the conf directory under this directory

Because the zoo.cfg file under conf will be loaded by default when zk starts, we need to copy the zoo_sample.cfg
cp zoo_sample.cfg zoo.cfg

If you do not modify the dataDir=/tmp/zookeeper directory, this file will default to OK,
switch to the bin directory to start the service

At this point, the zookeeper installation is complete

2) Deploy dubbo-admin
Tomcat to install it by itself.
Download the dubbo-admin-2.4.1.war package and deploy it on Linux tomcat. First put dubbo-admin-2.4.1 under webapps/ROOT of tomcat, and then decompress it:
  #jar -xvf dubbo-admin-2.4. 1. After the war is decompressed, there is a dubbo.properties
file in the webapps/ROOT/WEB-INF directory. Edit the file. It is
mainly the zookeeper address that was just installed.

Start tomcat and access the service. The

account is root and the password is also root.

We can


see

3) The service provider development (Provider) of the exposed service has been connected to
create the dubbo-api project and write the code

package com.feng;

public interface DemoService {
	String sayHello(String name) throws Exception;
}



The application is released as a jar for server and consumer use

4) Service running container development (Container)

DemoServiceImpl.java
package com.feng.impl;
import com.feng.DemoService;
public class DemoServiceImpl implements DemoService {
	public String sayHello(String name) throws Exception {
		if("Excetion".equals(name)){
			throw new Exception("myException");
		}
		return "Hello " + name;
	}
}


service.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false">
	<!-- provider application name information, this is equivalent to a name, our dubbo management page is more clear which application is exposed -->
	<dubbo:application name="dubbo_provider"></dubbo:application>
	<!-- Use the zookeeper registry to expose the service address -->
	<dubbo:registry address="zookeeper://192.168.137.129:2181"
		check="false" subscribe="false" register=""></dubbo:registry>
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- Service interface to be exposed -->
	<dubbo:service interface="com.feng.DemoService" ref="demoService" />
	<bean id="demoService" class="com.feng.impl.DemoServiceImpl" />
</beans>


Write a starting point test program
Provider.java
package com.feng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "service.xml" });
		System.out.println("Published successfully");
		context.start();
		System.in.read(); // Press any key to exit} }
	}
}


Check dubbo-admin

so the service has been registered successfully.

5) Consumer development

Create dubbo-customer application


</beans>
customer.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >
   <dubbo:application name="dubbo_consumer"></dubbo:application>
   <!-- Use the zookeeper registry to expose the service address -->  
   <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>  
     <!-- Service to be referenced -->  
   <dubbo:reference interface="com.feng.DemoService" id="demoService"></dubbo:reference>


Write a startup test program and it
runs successfully
. There is one more consumer on bubbo-admin
Custom.java and run
package com.feng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Custom{
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "customer.xml" });
		DemoService demoService=(DemoService)context.getBean("demoService");
		String xx=null;
		
		try{
		 xx=demoService.sayHello("feng");
		}catch(Exception e){
			e.printStackTrace ();
		}
		System.out.println(xx);
		 context.start();
		 System.in.read();
	}}

So far. . . . . Dubbo is all built successfully. For simplicity all services do not use a web server.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646599&siteId=291194637