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

本博客 猫叔的博客,转载请申明出处

学习系列

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

CXF官网: http://cxf.apache.org/

Apache CXF是一个开源的WebService RPC框架,是由Celtix和Codehaus XFire合并而成的。它可以说是一个功能齐全的集合

功能特性:

  • 支持Web Service标准,包括SOAP(1.1、1.2)规范、WSI Basic Profile...等等我也不了解的,这里就不一一举例了。
  • 支持JSR相关规范和标准,包括....同上。
  • 支持多种传输协议和协议绑定(SOAP、REST/HTTP、XML)、数据绑定(JAXB2.X、Aegis、Apache XML Beans)

还是先从案例入手吧

项目源码地址: RPC_Demo,记得是项目里面的 comgithubcxf
  • 1、使用IDEA构建一个maven项目,我选择了maven-archetype-webapp构建基本框架。当然你可能还需要创建一些目录

RPC框架是啥之Apache CXF一款WebService RPC框架入门教程_第1张图片

  • 2、我想是时候先配置好主要的pom文件了。



  4.0.0

  cxf
  comgithubcxf
  1.0-SNAPSHOT
  war

  comgithubcxf Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    3.1.7
    4.0.9.RELEASE
  

  
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-context-support
      ${spring.version}
    
    
      org.apache.cxf
      cxf-rt-frontend-jaxws
      ${cxf.version}
    
    
      org.apache.cxf
      cxf-rt-transports-http
      ${cxf.version}
    
    
      org.apache.cxf
      cxf-rt-transports-http-jetty
      ${cxf.version}
    
    
      junit
      junit
      4.11
      test
    
  

  
    comgithubcxf
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  
  • 3、构建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标明对接的接口实现

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!";
    }
}
  • 4、编写对应的cxf-server.xml文件(核心点),这里我参考了官网的案例




    
    
    

        
  • 5、然后就是我们的web.xml文件了,



  Archetype Created Web Application
  
    contextConfigLocation
    classpath:cxf-server.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    CXFServer
    org.apache.cxf.transport.servlet.CXFServlet
    1
  
  
    CXFServer
    /ws/*
  
  • 6、配置tomcat,由于我是IDEA的环境,所有我就截图给大家看看

RPC框架是啥之Apache CXF一款WebService RPC框架入门教程_第2张图片

然后启动tomcat即可,如果一起正常的话,老干妈保佑!

  • 7、访问测试服务端,这时我们可以访问http://localhost:8080/ws/server?wsdl,如果你看到了一下的画面,就是启动成功!

RPC框架是啥之Apache CXF一款WebService RPC框架入门教程_第3张图片

  • 8、服务端就先让它运行着,接着我们在同一个项目里面创建客户端的,这个比较简单,你可以先准备一个cxf-client.xml文件,配置对应的WebService服务接口,确定访问的地址,注意是HTTP地址哦,WebService就是采用HTTP协议通信的。




    
    
        
        
    

        
  • 9、然后编写一个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"));
    }
}

RPC框架是啥之Apache CXF一款WebService RPC框架入门教程_第4张图片

WebService 是一种跨平台的RPC技术协议。

公众号:Java猫说

学习交流群:728698035

现架构设计(码农)兼创业技术顾问,不羁平庸,热爱开源,杂谈程序人生与不定期干货。

Image Text

你可能感兴趣的:(java,rpc,cxf,webservice)