前言:
自從互聯網起步,到發展壯大以來,已經深入到了各個領域。用戶的需求體驗也慢慢提高了,對應的互聯網相關開發技術也越來越多。但是我們知道每一門開發語言都是在某種環境下應運而生,有的發展越來越大,越來越成熟穩定。但是有的開發語言則好景不長。這裏就有很多人關注的問題,哪種計算機語言是最好的語言。我還是那句話,每種語言誕生的背景不同,解決的問題不同,因此各有千秋。所以提出兩個問題:
在A語言A平臺不能完成的工作,能不能交給B語言B平臺來實現?
它們之間通過什麼規則的語法來交流數據?
這就是接下來正文中要說明的,webservice(web服務)
本文僅僅實現一個簡單實例,爲大家提供一個思路,拋磚引玉,相互交流學習,有不正確之處還請多多指教。
正文:
一、實例所用環境及工具
1、jdk版本1.8
2、windows下基於eclipse(版本neon.3 支持java ee) 的tomcat(9.0.6)插件發佈java webservice,使用maven管理依賴包,遠程倉庫使用aliyun
3、Linux Centos6.5 操作系統下搭建PHP環境(nginx服務器軟件+php的php-fpm模式)
說明:僅做服務發佈調用調試,因此不涉及數據庫相關,我相信你能自己擴展的
二、發佈java、webservice
1、打開eclipse創建maven工程,並選擇webapp項目
2、項目右鍵選擇properties,將其中有關java jdk版本設置爲1.8
說明:使用原sun公司的JAX-WS開發WebService,步驟:接口編寫-》實現接口類-》發佈(接口中只需要把類註明爲@WebService,把 要暴露給客戶端的方法註明爲@WebMethod即可)
3、創建service接口,綁定註解。編寫實例實現接口(注意其中的註解)
創建接口:
package cn.zgtlz.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public interface HelloService {
/**
* 發佈無參調用方法sayHelloDefault
*/
@WebMethod
public String sayHelloDefault();
/**
* 發佈帶參調用方法
*/
@WebMethod
public String sayHello(String userName);
}
創建實現類:
package cn.zgtlz.service.impl;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import cn.zgtlz.service.HelloService;
@WebService(endpointInterface="cn.zgtlz.service.HelloService")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class HelloServiceImpl implements HelloService {
@Override
public String sayHelloDefault() {
return "hello";
}
@Override
public String sayHello(String userName) {
return "hello"+userName;
}
}
4、發佈服務
發佈方式一:(測試用,不做詳細介紹)
使用Endpoint.publish發佈服務,但是jdk1.6u17?以下編譯器不支持
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/test-webservice/service/hello", new HelloServiceImpl());
}
發佈方式二:發佈到tomcat,(需要編寫sun-jaxws.xml文件並放到WEB-INF下)
sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="HelloService"
implementation="cn.zgtlz.service.impl.HelloServiceImpl"
url-pattern="/service/hello" />
</endpoints>
Web.xml配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>HelloService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloService</servlet-name>
<url-pattern>/service/hello</url-pattern>
</servlet-mapping>
</web-app>
最後部署到Tomcat裏,值得一提的是您可能需要添加以下jar包(因爲Tomcat沒有)
這裏我將配置到pom文件中
Pom.xml
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.13</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>policy</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.external</groupId>
<artifactId>management-api</artifactId>
<version>3.2.1-b002</version>
</dependency>
<dependency>
<groupId>org.glassfish.ha</groupId>
<artifactId>ha-api</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.2.0-b003</version>
</dependency>
</dependencies>
...
最後將項目發佈到tomcat,沒有報錯則發佈成功
最後在地址欄輸入:
http://localhost:8081/tomcatservice/service/hello?wsdl
三、搭建php環境調用java webservice
1、準備php和nginx源碼包
nginx http://nginx.org/download/nginx-1.9.9.tar.gz
php http://distfiles.macports.org/php5/php-5.6.0.tar.xz
2、測試環境 linux centos6.5
3、爲避免編譯php安裝時出錯,先將php所需依賴安裝
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-develncurses ncurses-deve curl curl-devel openssl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel pcre pcre-devel
4、解壓並編譯安裝php
#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-zlib --with-libxml-dir --enable-xml --with-curl --with-jpeg-dir --with-png-dir --enable-fpm --with-gd --with-openssl --with-xmlrpc --enable-zip --with-freetype-dir --with-apxs2=/usr/local/apache2/bin/apxs //此配置可生成libphp5.so
#make && make install
5、配置PHP
#cp /home/software/php-5.6.0/php.ini-production /usr/local/php/etc/php.ini
#cd /usr/local/php/etc
#cp php-fpm.conf.default php-fpm.conf
修改FPM 配置文件php-fpm.conf
pm.max_children = 30
pm.start_servers = 10
pm.min_spare_servers = 3
pm.max_spare_servers = 25
pm.max_requests = 300 //去掉配置前分號
注意:此處start_servers 必須大於min_spare_servers,而小於max_spare_servers
#/usr/local/php/sbin/php-fpm //啓動php-fpm
6、配置nginx
location ~ \.php$ {
root html; #這是你網站的根目錄
fastcgi_pass 127.0.0.1:9000; #這裏指定了fastcgi進程偵聽的端口,nginx就是通過這裏與php交互的
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
7、啓動nginx:
#/usr/local/nginx/sbin/nginx
8、測試php環境
在/usr/local/nginx/html目錄下創建index.php,並加入如下代碼:
<?php
Phpinfo();
最後訪問地址,如下圖結果,表示搭建成功:
最後將index.php文件中代碼改爲:
<?php
header("Content-Type: text/html; charset=utf-8");
try{
$client= new SoapClient(
'http://192.168.2.101:8081/tomcatservice/service/hello?wsdl'
);
echo $client->sayHelloDefault();
}catch(Exception $e){
echo $e->getMessage();
}
注意:
此處php需要soap擴展才能遠程訪問service,因此地址欄輸入
http://192.168.2.102/index.php
如無響應請先確定php是否有該擴展
使用phpize進行重新編譯
配置完成後重啓php-fpm和nginx,並訪問http://192.168.2.102/index.php