分佈式服務框架Dubbo入門案例和項目源碼

  


Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,html

是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點。
   官方網站:  http://dubbo.io/

    本項目代碼,根據官方提供的dubbo-ws-demo-master例子,改造而來。
    官網例子源碼: https://github.com/dubbo/dubbo-ws-demo

    官方的例子,都放在1個項目中,接口、實現類、Java應用測試例子。
   
    本身給改造了下,方便在項目中直接使用。
    雖然說是HelloWorld,也仍是要向實際狀況靠攏。


   3個項目java




1.web-service接口項目, 定義接口和供調用放引入的dubbo配置。git


 接口HelloService 
 
  package com.dubbo.demo;
public interface HelloService {
    String hello(String name);
}



接口定義的dubbo配置
  
spring-dubbo.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/beanshttp://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="ws-demo" />
	<dubbo:registry address="N/A" />
	<dubbo:reference id="helloService" interface="com.dubbo.demo.HelloService" version="1.0.0"
				   url="webservice://127.0.0.1:9000/com.dubbo.demo.HelloService"/>
</beans>


maven配置
 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.shop</groupId>
	<version>1.0.0-SNAPSHOT</version>
	<name>web-service</name>
	<url>http://maven.apache.org</url>;
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.4.10</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-simple</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>2.6.1</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>web-service</finalName>

		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

		</plugins>
	</build>
	<artifactId>web-service</artifactId>
</project>


2.web-service-impl接口實現項目

  接口實現類 HelloServiceImpl 
package com.dubbo.demo.impl;

import com.dubbo.demo.HelloService;

public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello, " + name + "!";
    }
}

 
接口實現dubbo配置
spring-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/beanshttp://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="ws-demo" />

	<dubbo:registry address="N/A" />
	
	<dubbo:protocol name="webservice" port="9000" server="servlet" />

	<bean id="helloService" class="com.dubbo.demo.impl.HelloServiceImpl"/>
	
	<dubbo:service interface="com.dubbo.demo.HelloService" version="1.0.0"
				   protocol="webservice" ref="helloService"/>
</beans>


Maven配置
<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.shop</groupId>
	<artifactId>web-service-impl</artifactId>
	<packaging>war</packaging>
	<version>1.0.0-SNAPSHOT</version>
	<name>dubbo-ws Maven Webapp</name>
	<url>http://maven.apache.org</url>;
	<dependencies>
	
		<dependency>
			<groupId>com.shop</groupId>
			<artifactId>web-service</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>
		
	</dependencies>
	<build>
		<finalName>web-service-impl</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		
		</plugins>
	</build>
</project>

 
   web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<display-name>web-service</display-name>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:spring-context.xml
		</param-value>
	</context-param>

	<servlet>
		<servlet-name>dubbo</servlet-name>
		<servlet-class>
			com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dubbo</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

 

3.接口測試(調用方)項目github


接口調用代碼
import com.dubbo.demo.HelloService;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-dubbo.xml");
        classPathXmlApplicationContext.start();

        HelloService helloService = (HelloService) classPathXmlApplicationContext.getBean("helloService");
        String world = helloService.hello("World");

        System.out.println("=====================================");
        System.out.println(world);
        System.out.println("=====================================");        
    }
} 



maven配置
  相似上面的

Web程序訪問
  
ConsumerMain是經過應用程序的方式,訪問Dubbo包裝的服務。
 而 

  
@Controller
@RequestMapping("")
public class HelloWorldController {

	@Autowired
	private HelloService helloService;
	
	@ResponseBody
	@RequestMapping("hello")
	public String hello(){
		return helloService.hello("hi dubbo");
	}
	
}



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<display-name>web-service-test</display-name>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:spring-context.xml
		</param-value>
	</context-param>

	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>




4.項目測試 
   a.啓動服務項目
     web-service-imp
     
   b.啓動Java應用程序
ConsumerMain,打印結果。
      
   c.啓動Web應用程序web-service-test,訪問http://localhost:9080/hello
   

相關文章
相關標籤/搜索