Generic Call

Generic Call

1. Dubbo-go Generic Call Java Server

Using Triple protocol + hessian2 serialization scheme

1.1 Java Server Startup

  1. Transport Structure Definition
  1. package org.apache.dubbo;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class User implements Serializable {
  5. private String id;
  6. private String name;
  7. private int age;
  8. private Date time = new Date();
  9. }
  1. Interface Definition
  1. package org.apache.dubbo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. //import org.apache.dubbo.rpc.filter.GenericFilter;
  6. public interface UserProvider {
  7. User GetUser1(String userId);
  8. }

1.2 Go Client Generic Call

This section shows how to construct a generic interface reference in the form of an API

  1. // Initialize Reference Configuration
  2. refConf := config.NewReferenceConfigBuilder().
  3. SetInterface("org.apache.dubbo.UserProvider").
  4. SetRegistryIDs("zk").
  5. SetProtocol(tripleConst.TRIPLE).
  6. SetGeneric(true).
  7. SetSerialization("hessian2").
  8. Build()
  9. // Construct Root Configuration, importing registry module
  10. rootConfig := config.NewRootConfigBuilder().
  11. AddRegistry("zk", config.NewRegistryConfigWithProtocolDefaultPort("zookeeper")).
  12. Build()
  13. // Reference Configuration initialization, as service discovery needs to pass in the configured rootConfig
  14. if err := refConf.Init(rootConfig); err != nil{
  15. panic(err)
  16. }
  17. // Generic call loading, service discovery
  18. refConf.GenericLoad(appName)
  19. time.Sleep(time.Second)
  20. // Initiating a generic call
  21. resp, err := refConf.GetRPCService().(*generic.GenericService).Invoke(
  22. context.TODO(),
  23. "getUser1",
  24. []string{"java.lang.String"},
  25. []hessian.Object{"A003"},
  26. )
  27. if err != nil {
  28. panic(err)
  29. }
  30. logger.Infof("GetUser1(userId string) res: %+v", resp)

The Invoke method of GenericService includes three parameters: context.Context, []string, []hessian.Object,

where the second parameter is the corresponding Java class name, such as java.lang.String, org.apache.dubbo.User, and the third parameter is the parameter list, where hessian.Object is the interface. The second and third parameters should match the method signature and correspond in order.

Obtain a map structure return result

  1. INFO cmd/client.go:89 GetUser1(userId string) res: map[age:48 class:org.apache.dubbo.User id:A003 name:Joe sex:MAN time:2021-10-04 14:03:03.37 +0800 CST]

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)