RPC框架是啥之Apache CXF一款WebService RPC框架入門教程

本博客 貓叔的博客,轉載請申明出處php

學習系列

Apache CXF一款WebService RPC框架入門教程

CXF官網:http://cxf.apache.org/html

Apache CXF是一個開源的WebService RPC框架,是由Celtix和Codehaus XFire合併而成的。它能夠說是一個功能齊全的集合java

功能特性:git

  • 支持Web Service標準,包括SOAP(1.一、1.2)規範、WSI Basic Profile...等等我也不瞭解的,這裏就不一一舉例了。
  • 支持JSR相關規範和標準,包括....同上。
  • 支持多種傳輸協議和協議綁定(SOAP、REST/HTTP、XML)、數據綁定(JAXB2.X、Aegis、Apache XML Beans)

仍是先從案例入手吧

項目源碼地址:RPC_Demo,記得是項目裏面的comgithubcxfgithub

  • 一、使用IDEA構建一個maven項目,我選擇了maven-archetype-webapp構建基本框架。固然你可能還須要建立一些目錄

iamge

  • 二、我想是時候先配置好主要的pom文件了。
<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cxf</groupId>
  <artifactId>comgithubcxf</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>comgithubcxf Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <cxf.version>3.1.7</cxf.version>
    <spring.version>4.0.9.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>comgithubcxf</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
複製代碼
  • 三、構建Server端還有其服務實現,接口使用@WebService註解,標明是一個WebService遠程服務接口
package com.github.cxf.server;

import javax.jws.WebService;

/** * Create by UncleCatMySelf in 21:57 2019\4\23 0023 */
@WebService
public interface CxfService {

    String say(String someOne);

}
複製代碼

在實現類上也一樣加上,並經過endpointInterface標明對接的接口實現web

package com.github.cxf.server;

import javax.jws.WebService;

/** * Create by UncleCatMySelf in 21:57 2019\4\23 0023 */
@WebService(endpointInterface = "com.github.cxf.server.CxfService")
public class CxfServiceImpl implements CxfService {
    @Override
    public String say(String someOne) {
        return someOne + ",Welcome to Study!";
    }
}
複製代碼
  • 四、編寫對應的cxf-server.xml文件(核心點),這裏我參考了官網的案例
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <jaxws:endpoint id="helloWorld" implementor="com.github.cxf.server.CxfServiceImpl" address="/server"/>
</beans>
        <!-- END SNIPPET: beans -->
複製代碼
  • 五、而後就是咱們的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>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:cxf-server.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>CXFServer</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServer</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>
複製代碼
  • 六、配置tomcat,因爲我是IDEA的環境,全部我就截圖給你們看看

iamge
iamge

而後啓動tomcat便可,若是一塊兒正常的話,老乾媽保佑!spring

  • 七、訪問測試服務端,這時咱們能夠訪問http://localhost:8080/ws/server?wsdl,若是你看到了一下的畫面,就是啓動成功!

iamge

  • 八、服務端就先讓它運行着,接着咱們在同一個項目裏面建立客戶端的,這個比較簡單,你能夠先準備一個cxf-client.xml文件,配置對應的WebService服務接口,肯定訪問的地址,注意是HTTP地址哦,WebService就是採用HTTP協議通訊的。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <bean id="client" class="com.github.cxf.server.CxfService" factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.github.cxf.server.CxfService"/>
        <property name="address" value="http://localhost:8080/ws/server"/>
    </bean>
</beans>
        <!-- END SNIPPET: beans -->
複製代碼
  • 九、而後編寫一個client的啓動程序,並運行,我想你會成功的!由於我看到了下圖!
package com.github.cxf.client;

import com.github.cxf.server.CxfService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/** * Create by UncleCatMySelf in 21:56 2019\4\23 0023 */
public class CxfClient {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:cxf-client.xml");
        CxfService client = (CxfService)context.getBean("client");
        System.out.println(client.say("MySelf"));
    }
}
複製代碼

iamge

WebService 是一種跨平臺的RPC技術協議。express

公衆號:Java貓說

學習交流羣:728698035apache

現架構設計(碼農)兼創業技術顧問,不羈平庸,熱愛開源,雜談程序人生與不按期乾貨。tomcat

Image Text
相關文章
相關標籤/搜索