1. 程式人生 > >【學習筆記】服務治理工具Dubbo的配置整理

【學習筆記】服務治理工具Dubbo的配置整理

dubbo的結構可分為:服務生產者Provider、服務執行容器Container、服務消費者Consumer、註冊中心Register、監控中心Monitor

作用是,暴露介面,提供服務,其他工廠需要依賴這個介面,像呼叫本地API一樣呼叫遠端API。可以提高程式碼的複用。

公共介面(打包為jar):

package cn.xing.service;

public interface UserService {
	
	void findUser(int id);
}

服務提供者:

package cn.xing.service.Impl;

import org.springframework.stereotype.Service;

import cn.xing.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Override
	public void findUser(int id) {
		// TODO Auto-generated method stub
		System.out.println("服務提供者:服務被呼叫了。。。。");
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


	<context:component-scan base-package="cn.xing.service"/>

	<!-- 使用dubbo釋出服務 -->
	<!-- 提供方應用資訊,用於計算依賴關係 -->
	<dubbo:application name="test-dubbo-provide-1" />
	<dubbo:registry protocol="zookeeper" address="192.168.159.130:2181" />
	<!-- 用dubbo協議在20880埠暴露服務 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 宣告需要暴露的服務介面 -->
	<dubbo:service interface="cn.xing.service.UserService" ref="userServiceImpl" />

</beans>

服務消費者:

package cn.xing.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xing.service.UserService;

@Controller
public class controller {

	@Autowired
	private UserService us;

	@RequestMapping("/dubbo")
	public String test() {
		us.findUser(1);
		return "ok";
	}

	@RequestMapping("/{page}")
	public String show(@PathVariable String page) {
		return page;
	}
}
<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd">


	<context:component-scan base-package="cn.xing.controller" />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 引用dubbo服務 -->
	<dubbo:application name="test-dubbo-consumer-1" />
	<dubbo:registry protocol="zookeeper" address="192.168.159.130:2181" />
	<!-- 引用服務,規範就是這個介面 -->
	<dubbo:reference interface="cn.xing.service.UserService" id="userService" timeout="600000" />
	
</beans>