spring httpinvoker 實踐

spring httpinvoker是spring框架的功能,適用於spring框架項目調用另外一個spring框架項目。java

服務器端:ssh項目mysql

客戶端:springboot項目web

服務器端配置:redis

public interface IHelloService {
    public String testHttpInvoker();

}spring

public class HelloServiceImpl implements IHelloService{

    @Override
    public String testHttpInvoker() {
        return "http invoker success";
    }

}sql


springbean.xml:json

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans           
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
     <mvc:annotation-driven/>
    
     <bean id="helloService"
        class="com.service.HelloServiceImpl">
     </bean>
     <!-- 服務器端httpinvoker 配置 -->api

<!--spring-mvc

服務聲明:springboot

在Spring配置文件中聲明一個HttpInvokerServiceExporter類的bean,共三部分:

--服務名稱(如helloExporter)

--服務類型(如com.service.IHelloService)

--服務實現類 helloService-->


    <bean name="/helloExporter"
        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="helloService"></property>
        <property name="serviceInterface" value="com.service.IHelloService">
        </property>
     </bean>
   
     
      </beans>

 

web.xml:

<!-- 服務器端httpinvoker 配置 -->
    <servlet>
        <servlet-name>helloExporter</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springbean.xml</param-value>
      </init-param>
      <load-on-startup>3</load-on-startup>
     </servlet>
     <servlet-mapping>
            <servlet-name>helloExporter</servlet-name>
            <url-pattern>/remoting/*</url-pattern>
      </servlet-mapping>

客戶端配置:

package com.contorller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.service.IHelloService;
import com.util.SpringUtil;

@RestController
public class HelloController {
    
    @GetMapping("/testHttpinvoker")
    public String testHttpinvoker(){
        IHelloService iHelloService = SpringUtil.getBean("remoteHelloService",IHelloService.class);
        return iHelloService.testHttpInvoker();
    }
}

public interface IHelloService {
    public String testHttpInvoker();

}

package com.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class SpringUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
 
    }
 
    //獲取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    //經過name獲取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }
 
    //經過class獲取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
 
    //經過name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
 
}

application-bean.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans           
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="remoteHelloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl" value="http://localhost:8088/sshAndRedis/remoting/helloExporter" />

<!--路徑是關鍵,不能出錯。對應服務器端servlet url-pattern,/helloExporter對應HttpInvokerServiceExporter的id-->
        <property name="serviceInterface" value="com.service.IHelloService" />
    </bean>
    
    
</beans>

@SpringBootApplication
@ImportResource(locations= {"classpath:application-bean.xml"})
public class RunApplication {
    public static void main(String[] args){
        
        SpringApplication.run(RunApplication.class, args);
    }
    
}

pom文件:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/>
    </parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

 

<properties>        <!-- 統一源碼的編碼方式 -->        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!-- spring4.3.2版本兼容jackson2.7.5版本,版本不兼容會報錯找不到jackson的類 -->        <spring.version>4.3.2.RELEASE</spring.version>        <!-- 3.6.5  -->        <hibernate.version>4.3.1.Final</hibernate.version>    </properties>  <dependencies> <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-webmvc</artifactId>       <version>${spring.version}</version>   </dependency>    <!-- Spring 核心依賴 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- Spring web依賴 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- Spring整合ORM框架依賴 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- Hibernate 核心依賴 -->        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-core</artifactId>            <version>${hibernate.version}</version>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-c3p0</artifactId>            <version>${hibernate.version}</version>        </dependency>        <!-- <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-ehcache</artifactId>            <version>${hibernate.version}</version>        </dependency> -->        <!-- <dependency>          <groupId>net.sf.ehcache</groupId>          <artifactId>ehcache-core</artifactId>          <version>2.2.0</version>        </dependency> -->   <!-- 解析jsp文件須要的依賴 -->   <!-- <dependency>              <groupId>jstl</groupId>              <artifactId>jstl</artifactId>              <version>1.2</version>     </dependency> -->   <!-- jdbcTemplate須要的依賴 -->   <!-- <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jdbc</artifactId>    <version>3.2.8.RELEASE</version>    </dependency> -->   <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.25</version>   </dependency>   <!-- C3P0 依賴 -->        <dependency>            <groupId>com.mchange</groupId>            <artifactId>c3p0</artifactId>            <version>0.9.5</version>            </dependency>          <!-- jedis依賴 -->        <!-- <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>2.7.1</version>        </dependency>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>1.6.2.RELEASE</version>        </dependency> -->        <!-- 經常使用有三種json解析jackson、fastjson、gson。 -->        <!-- <dependency>              <groupId>com.alibaba</groupId>              <artifactId>fastjson</artifactId>              <version>1.1.41</version>          </dependency> -->        <!-- 2.9.3 -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <version>2.7.5</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-core</artifactId>            <version>2.7.5</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-annotations</artifactId>            <version>2.7.5</version>        </dependency>                <dependency>          <groupId>javax.servlet</groupId>          <artifactId>servlet-api</artifactId>          <version>2.5</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>4.3.0.RELEASE</version>        </dependency>

相關文章
相關標籤/搜索