Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to call dubbo programmer dynamically

Shulou Source: shulou.com Published: 2022-06-01 18:50:58 10月03日 Update

This article introduces you how to dynamically call dubbo programming, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Define an interface

Public interface ToDoService {String test (String s);}

Two implementations

@ Service (group = "test1") public class ToDoService1 implements ToDoService {@ Override public String test (String s) {return "1" + s;}} @ Service (group = "test2") public class ToDoService2 implements ToDoService {@ Override public String test (String s) {return "2" + s;}}

Here we use group to distinguish the two implementation classes

Caller:

/ * execute ToDoService * * @ param group * @ return * / public String executeToDoService (String group) {ReferenceConfig referenceConfig = new ReferenceConfig (); referenceConfig.setInterface (ToDoService.class); referenceConfig.setGroup (group); ReferenceConfigCache cache = ReferenceConfigCache.getCache (); ToDoService toDoService1 = cache.get (referenceConfig); try {return toDoService1.test ();} catch (Exception ex) {return ";}}

It should be noted here that the official ReferenceCofing document says it is heavy and needs to be cached. The implication is not to repeat the new ReferenceCofing instance, which will continue to register as a consumer Consumer.

But ReferenceConfigCache is officially provided, so instead of writing a static hashMap to cache, use the tools provided by dubbo developers to get the instances we need to call.

ReferenceConfigCache cache = ReferenceConfigCache.getCache (); ToDoService toDoService1 = cache.get (referenceConfig)

On how to dynamically call the dubbo programmer to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Dynamic program two content official instance more cache help yes implication constant interest guys buddies that is tools developers interfaces documents Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Redmi Apple macOS