1. 程式人生 > >dubbo入門例子

dubbo入門例子

有問題歡迎評論

 

1.服務提供方


1.1新建maven工程(war)

略........

1.2 寫配置檔案 applicationContextService.xml

<dubbo:application name="" > 設定當前應用名稱

<dubbo:registry address=""> 設定註冊中心地址

<dubbo:annotation package = "" > 掃描服務(掃描service註解)

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

  
	<dubbo:application name="dubbo-demo"/>

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<dubbo:annotation package="com.alibaba.service.impl"/>   
</beans>

1.3 新建服務

新建一個介面

public interface UserService {
	
	public String getName();
}

實現類 @Service註解使用alibaba的

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Override
	public String getName() {
		return "我的眼睛望著窗外,不知如何對你表白";
	}
}

web.xml

載入spring容器

<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_2_5.xsd"
	version="2.5">	

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

2.服務消費方


2.1 新建maven工程(war)

略...

2.2 寫配置檔案 springmvc.xml

<dubbo:annotation package="com.alibaba.controller" /> 這裡掃描contorller類

        <dubbo:application name="dubbo-web"/>

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<dubbo:annotation package="com.alibaba.controller" />

web.xml

<servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定載入的配置檔案 ,通過引數contextConfigLocation載入-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>

2.3 新建類

把上面的service介面複製過來(不是移動)

public interface UserService {
	
	public String getName();
}
@RestController
public class UserController {

	//遠端注入
	@Reference
	private UserService userService;
	
	@RequestMapping("/getName")
	public String getName(){
		return userService.getName();
	}
}

3.測試


工程程式碼

連結: https://pan.baidu.com/s/1B1vMruGPFEqhxNEiHMJdag 提取碼: 4vjs