Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How Dubbo generalization refers to

Shulou Source: shulou.com Published: 2022-06-01 12:56:44 10月01日 Update

This article mainly introduces "how to quote Dubbo generalization". In daily operation, I believe many people have doubts about how to quote Dubbo generalization. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to quote Dubbo generalization". Next, please follow the editor to study!

The pan-interface invocation mode is mainly used when the client has no API interface and model class elements. The POJO used in the parameters and return values are expressed by Map, which is usually used for framework integration, such as implementing a general service testing framework, which can be implemented by invoking all services through GenericService.

The implementation code is as follows:

Server-side code:

Package com.dubbo.entity

Import java.io.Serializable

Public class Computer implements Serializable {

Private static final long serialVersionUID = 1L

Private Integer id

Private String name

Public Integer getId () {

Return id

}

Public void setId (Integer id) {

This.id = id

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

}

Package com.dubbo.entity

Import java.io.Serializable

Public class User implements Serializable {

Private static final long serialVersionUID = 1L

Private Integer id

Private String name

Private Computer computer

Public Integer getId () {

Return id

}

Public void setId (Integer id) {

This.id = id

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

Public Computer getComputer () {

Return computer

}

Public void setComputer (Computer computer) {

This.computer = computer

}

}

Package com.dubbo.service

Import com.dubbo.entity.User

Public interface IDubboGenQService {

Public User queryUser (Integer id)

Public void saveUser (User user)

}

Package com.dubbo.service.impl

Import com.dubbo.entity.Computer

Import com.dubbo.entity.User

Import com.dubbo.service.IDubboGenQService

Public class DubboGenQServiceImpl implements IDubboGenQService {

Public User queryUser (Integer id) {

User user=new User ()

User.setId (id)

User.setName ("Zhang San" + id)

Computer computer=new Computer ()

Computer.setId (id)

Computer.setName ("Zhang San's computer")

User.setComputer (computer)

Return user

}

Public void saveUser (User user) {

System.out.println ("user saved")

System.out.println (user.getName ())

System.out.println (user.getComputer () .getName ())

}

Dubbo.xml configuration:

Dubbo provider launch

Public class dubboServerStart {

Public static void main (String [] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("dubbo.xml")

Context.start ()

System.in.read ()

}

}

Client implementation:

Import java.io.IOException

Import java.util.HashMap

Import java.util.Map

Import org.springframework.context.ApplicationContext

Import org.springframework.context.support.ClassPathXmlApplicationContext

Import com.alibaba.dubbo.rpc.service.GenericService

Public class DubboClientStart {

Public static void main (String [] args) throws IOException {

ApplicationContext ctx=new ClassPathXmlApplicationContext ("dubbo.xml")

GenericService genericService= (GenericService) ctx.getBean ("dubboGenQService")

Object resultUser = genericService.$invoke ("queryUser"

New String [] {"java.lang.Integer"}

New Object [] {1})

HashMap userMap= (HashMap) resultUser

For (Map.Entry entry1: userMap.entrySet ()) {

If (entry1.getValue () instanceof HashMap) {

HashMap computerMap= (HashMap) entry1.getValue ()

System.out.println (entry1.getKey ())

For (Map.Entry entry: computerMap.entrySet ()) {

System.out.println ("\ t" + entry.getValue ())

}

} else {

System.out.println (entry1.getKey () + "+ entry1.getValue ())

}

}

/ / demonstrate the encapsulation of user data

Map user = new HashMap ()

User.put ("name", "Zhang San")

HashMap value = new HashMap ()

Value.put ("name", "Zhang San's computer")

User.put ("computer", value)

GenericService.$invoke ("saveUser", new String [] {"com.dubbo.entity.User"})

New Object [] {user})

}

}

Dubbo.xml configuration:

At this point, the study of "how to quote Dubbo generalization" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Zhang San learning service code client client interface more framework user computer help configuration utility next parameters situation used provider data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux MariaDB Shulou Information MySQL