简单SOA框架实现

简介:

14.4. Service-oriented architecture (SOA)

SOA 与 REST很多相同之处,目前SOA主要是基于SOAP实现,也有基于MQ的实现。而REST只限于HTTP POST/GET/PUT/DELETE等等。

我个人比较喜欢机遇TCP的SOA实现,不喜欢SOAP大量XML传输。

14.4.1. SOAP实现

这里提供一个简单的机遇SOAP实现的SOA框架

index.php入口文件

			
<?php
define ('CONFIG_DIR', '../config/');
define ('LIBRARY_DIR', '../library/');
define ('DEBUG', false);
//define ('DEBUG', ture);

require_once(CONFIG_DIR. 'default.php');
$remote_addr = $_SERVER['REMOTE_ADDR'];
if(!in_array($remote_addr, $firewall)) {
	printf("Permission denied: %s", $remote_addr);
	exit(0);
}

$request_uri = $_SERVER['REQUEST_URI'];
$classspath = LIBRARY_DIR.strtolower($request_uri)  . '.class.php';
if( is_file($classspath) ){
	require_once($classspath);
}else{
	die("Cannot loading interface!");
}

$class = ucfirst(substr($request_uri, strrpos($request_uri, '/')+1));
if( DEBUG ){
		printf("%s<br>",$class);
}

if (class_exists($class)) {
    $server = new SoapServer(null, array('uri' => "http://webservice.example.com"));
	$server->setClass($class);
	$server->handle();
}else{
	die('Object isnot exist.');
}
			
			

接口文件

			
<?php
require_once('common.class.php');

class Members extends Common{
	private $dbh = null;
	public function __construct() {
		parent::__construct();
		$this->dbh = new Database('slave');
	}
	public function getAllByUsernameAndMobile($username,$mobile){
		$result = array();
		if(empty($username) or empty($mobile)){
			return($result);
		}
		$sql = "SELECT username, chinese_name, sex FROM members m, members_digest md WHERE m.id = md.id and m.username= :username and md.mobile = md5( :mobile );";
		$stmt = $this->dbh->prepare($sql);
		$stmt->bindValue(':username', $username);
		$stmt->bindValue(':mobile', $mobile);
		$stmt->execute();
		$result = $stmt->fetch(PDO::FETCH_ASSOC);
		return($result);
	}
	public function getAllByLimit($limit,$offset)
	{
		$sql = "SELECT username FROM members limit ".$limit.",".$offset;
		$stmt = $this->dbh->query($sql);
		while ($row = $stmt->fetch()) {
			//printf("%s\r\n", $row['username']);
			$result[] = $row['username'];
		}
		return $result;
	}
	function __destruct() {
       $this->dbh = null;
   }
}
			
			

客户端调用实例

			
<?php

$options = array('uri' => "http://webservice.example.com",
                'location'=>'http://webservice.example.com/members',
				 'compression' => 'SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP',
				'login'=>'neo',
				'password'=>'chen',
                'trace'=>true
				);
$client = new SoapClient(null, $options);

try {

	print_r($client->getAllByUsernameAndMobile('280600086','13113668890'));
	print_r($client->getAllByLimit(20,20));

}
catch (Exception $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
			
			

Nginx 虚拟主机配置文件 /etc/nginx/conf.d/webservice.example.com.conf

			
server {
    listen       80;
    server_name  webservice.example.com;

    charset utf-8;
    access_log  /var/log/nginx/webservice.example.com.access.log  main;
    auth_basic            "Login";
    auth_basic_user_file  htpasswd;

    location / {
        root   /www/example.com/webservice.example.com/htdocs;
        index  index.html index.php;
		if ($request_filename !~ (js|css|images|robots/.txt|.*\.html|index/.php) ) {
	            rewrite ^/(.*)$ /index.php/$1 last;
		    break;
		}
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ /index.php/ {
        root           /www/example.com/webservice.example.com/htdocs;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/example.com/webservice.example.com/htdocs$fastcgi_script_name;
        include        fastcgi_params;
    }

}
			
			

每增加一个功能需求,在library中创建一个 Class 文件即可。

index.php 有IP过滤功能,禁止非法IP访问

客户端采用压缩传输,节省xml传输开销

Nginx 设置了HTTP认证,防止他人探测,另外提示你还可以采用双向SSL认证。

目录
相关文章
|
运维 IDE Java
SOA(面向服务架构)是什么?
SOA(面向服务架构)是什么?
1310 0
SOA(面向服务架构)是什么?
|
3天前
|
存储 监控 API
SOA简介
SOA简介
12 1
|
存储 供应链 监控
做了那么多架构,你真的懂 SOA 了吗?
如何统一看待和区别分层架构、微服务架构、分布式架构等主流架构?什么是 SOA?我们采用 SOA 的目的是什么?什么是服务化的本质?如何设计服务以及服务化架构呢?阿里高级技术专家程彦分享他对面向服务架构的一些看法,并给出相关的步骤和方案,较长,同学们可收藏后再看。
2159 0
做了那么多架构,你真的懂 SOA 了吗?
|
XML 数据格式 网络架构
|
NoSQL Java 数据库
微服务架构与SOA架构模式实现区别|学习笔记
快速学习微服务架构与SOA架构模式实现区别
120 0
|
缓存 监控 负载均衡

热门文章

最新文章