Home  >  Article  >  Java  >  Case in which Java is called by Node

Case in which Java is called by Node

黄舟
黄舟Original
2017-09-21 09:36:281904browse

This article mainly introduces the sample code of Node calling Java. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look.

The Java side serves as a service provider, implements services based on Dubbo and exposes services through Dubbo Hessian extension; the Node side acts as a service consumer, calling Java side services through node-hessian.

Java side

Service interface


package com.yuanxin.paas.ssb;
 
public interface TestService {
 
  /**
   * 测试:无参,无返回值。
   */
  void test();
   
  /**
   * 测试:原生类型参数与返回值。
   * 
   * @param i
   * @return 
   */
  String test0(int i);
   
  /**
   * 测试:无参,自定义类型返回值。
   * 
   * @return 
   */
  Result test1();
   
  /**
   * 测试:自定义类型参数,无返回值。
   * 
   * @param arg 
   */
  void test2(Arg arg);
   
  /**
   * 测试:自定义参数,自定义返回值。
   * 
   * @param arg
   * @return 
   */
  Result test3(final Arg arg);
}

Custom parameter class


package com.yuanxin.paas.ssb;
 
import java.io.Serializable;
 
public class Arg implements Serializable {
 
  private int i;
 
  public int getI() {
    return i;
  }
 
  public void setI(int i) {
    this.i = i;
  }
 
}

Custom return value class


package com.yuanxin.paas.ssb;
 
import java.io.Serializable;
 
public class Result implements Serializable {
 
  private int i;
 
  private String string;
 
  public int getI() {
    return i;
  }
 
  public void setI(int i) {
    this.i = i;
  }
 
  public String getString() {
    return string;
  }
 
  public void setString(String string) {
    this.string = string;
  }
 
}

Dubbo configuration is omitted.

Node side

node-hessian

Install node-hessian:


npm install hessian-proxy

Calling service interface


var Proxy = require('hessian-proxy').Proxy;
 
var proxy = new Proxy('http://127.0.0.1:9098/test-provider/provider/com.yuanxin.paas.ssb.TestService', '', '', proxy);
 
proxy.invoke('test', null, function (err, reply) {
  console.log('test: ' + reply);
});
 
proxy.invoke('test0', [25], function (err, reply) {
  console.log('test0: ' + JSON.stringify(reply));
})
 
proxy.invoke('test1', null, function (err, reply) {
  if (err) {
    console.log('test1: ' + err);
  }
 
  console.log('test1: ' + JSON.stringify(reply));
})
 
var argForTest2 = {
  i: 2
};
 
argForTest2.__type__ = 'com.yuanxin.paas.ssb.Arg';
 
proxy.invoke('test2', [argForTest2], function (err, reply) {
  if (err) {
    console.log('test2: ' + err);
  }
 
  console.log('test2: ' + JSON.stringify(reply));
})
 
 
var argForTest3 = {
  i: 3
};
 
argForTest3.__type__ = 'com.yuanxin.paas.ssb.Arg';
 
proxy.invoke('test3', [argForTest3], function (err, reply) {
  if (err) {
    console.log('test3: ' + err);
  }
 
  console.log('test3: ' + JSON.stringify(reply));
})

Run result

Java side

Node side

Summary

Dubbo is very good and provides support for the Hessian native protocol; Node is also very simple to call, but there are still a few points that need to be noted:

  • Interface methods cannot be overloaded (methods in the interface must use different method names)

  • Custom type namespace server and consumer To be consistent (com.yuanxin.paas.ssb.Arg)

  • The server-side custom type needs to implement the serialization interface (java.io.Serializable)

  • Try to use simple custom type structures (avoid nesting)

The above is the detailed content of Case in which Java is called by Node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn