Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of Dubbo

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the functions of Dubbo". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the functions of Dubbo"?

Dubbo is very functional, and most of the time we don't need to repeat the wheels. Here are some features that you may not know, but are easy to use.

Directly connected Provider

In the development and testing environment, it may be necessary to bypass the registry and only test the specified service provider. at this time, it may be necessary to use point-to-point direct connection and point-to-point direct connection mode, which will be based on the service interface. ignore the list of providers in the registry, and the An interface is configured point-to-point, which does not affect interface B to obtain the list from the registry (note: officially only recommend that the development & test environment use this function), the usage is as follows The address specified by url is the directly connected address:

Copy multiple versions of the code

When an API is implemented and an incompatible upgrade occurs, you can use the version number to transition. Services with different version numbers do not reference each other. The usage is as follows:

Copy the code

Using this feature of dubbo, we can realize the grayscale release of some functions. The steps are as follows:

The old implementation of the interface defines version= "1.0.0", and the new implementation of the interface version= "2.0.0"

Consumer side defines version= "*"

When Provider and Consumer are defined in this way, the new and old interface implementations each bear 50% of the traffic.

With this feature of dubbo, you can also complete the migration of incompatible versions:

During the low pressure period, upgrade half of the Provider to the new version first.

Then upgrade all consumers to the new version

Then upgrade the remaining half of the providers to the new version.

Echo test

The echo test is used to check whether the service is available. The echo test is performed according to the normal request flow, which can test whether the whole call is smooth and can be used for monitoring.

All services automatically implement the EchoService interface, which can be used by forcibly transforming any service reference into EchoService, the mode of use (demoService is the bean managed by spring)

Copy the code

EchoService echoService = (EchoService) demoService

System.out.println (echoService.$echo ("hello"))

Implicit parameter

You can pass parameters implicitly between Consumer and Provider through RpcContext's setAttachment () and getAttachment (). For example, the Controller layer intercepts the login token, and passing the memberId obtained according to token to the dubbo service can be passed by implicit parameters. The KV pair set by setAttachment () will be emptied after completing a remote call, that is, multiple remote calls will be set multiple times. Mode of use:

1. Server set:

RpcContext.getContext () .setAttachment ("CRT_MEMBER_ID", "13828886888"); copy the code

two。 Client get:

RpcContext.getContext (). GetAttachment ("CRT_MEMBER_ID") copies the code context

What is stored in the context is the environment information required during the current call. All configuration information will be converted to parameters of URL

RpcContext is a temporary status recorder for ThreadLocal. When a RPC request is received, or a RPC request is initiated, the status of the RpcContext

Will change. For example, if An adjusts B to B and then to C, then on the B machine, before B to C, RpcContext records the information of A to B, and B to C

After that, RpcContext records the information of B to C. Mode of use:

Boolean isConsumerSide = RpcContext.getContext () .isConsumerSide (); copy code

Here I recommend my Java advanced communication group: 606187239, there are (distributed architecture, high scalability, high performance, high concurrency, performance optimization, Spring boot, Redis, ActiveMQ, and other learning resources) to every Java partner free of charge, whether you change your profession or want to improve your ability at work, you are welcome to join the group for in-depth exchange and learning!

Local camouflage

Local masquerade is usually used for service degradation, such as an authentication service. When the service provider is all dead, the client does not throw an exception, but uses Mock data

Failed to return authorization. It is used as follows. The implementation class specified by mock executes when Provider throws a RpcException exception (must throw a RpcException exception), instead of returning the result remotely:

Copy the code

DemoServiceMock implementation source code:

Copy the code

Public class DemoServiceMock implements DemoService {

Public String sayHello (String name) {

Return "mock-value"

}

}

Generalization call

The generalized interface invocation mode is mainly used when the client has no API interface and model class elements. All POJO in the parameters and return values are represented by Map, which is usually used for framework integration, for example, to implement a general service testing framework, which can be implemented by invoking all services through GenericService. Mode of use:

Copy the code

Call source code:

Copy the code

/ * *

* @ author afei

* @ version 1.0.0

* @ since 22 / 11 / 2017

, /

Public class Main {

Public static void main (String [] args) {

/ / refer to the remote service. This instance encapsulates all connections to registrations and service providers. Please cache

ReferenceConfig reference = new ReferenceConfig ()

/ / weakly typed with a nickname

Reference.setInterface ("com.alibaba.dubbo.demo.DemoService")

Reference.setVersion ("1.0.0")

/ / declared to be a generic connection

Reference.setGeneric (true)

/ / "com.alibaba.dubbo.rpc.service.GenericService can replace all interface references"

GenericService genericService = reference.get ()

/ / basic types and Date,List,Map do not need to be converted, and can be called directly.

Object result = genericService.$invoke ("sayYes", new String [] {"java.lang.String"}, new Object [] {"afei"})

System.out.println ("result->" + result)

/ / "Map indicates the POJO parameter. If the returned value is POJO, it will also be automatically converted to Map.

Map teacher = new HashMap ()

Teacher.put ("id", "1")

Teacher.put ("name", "admin")

Teacher.put ("age", "18")

Teacher.put ("level", "3")

Teacher.put ("remark", "test")

/ / if POJO is returned, it will be automatically converted to Map.

Result = genericService.$invoke ("justTest", new String []

{"com.alibaba.dubbo.demo.bean.HighTeacher"}, new Object [] {teacher})

System.out.println ("result->" + result)

}

}

Access log

If you want to record the information of each request, you can open the access log, which is similar to the access log of Ngnix. Note: this log is relatively large, please pay attention to the disk capacity. How to use it (if you configure it locally, the global access log will become invalid):

Configure the global:

Copy the code

Configure part:

Copy the code

Log format style:

[2017-11-22 10:23:20] 172.18.1.205 com.alibaba.dubbo.demo.DemoService:1.0.0 sayHello-com.alibaba.dubbo.demo.DemoService:1.0.0 sayHello (java.lang.String) ["afei"] replication code delayed exposure

If the service needs warm-up time, such as initializing the local cache and waiting for related resources to be put in place, you can use delay for delayed exposure. How many milliseconds delay the Dubbo before exposing the service after the Spring container is initialized. Mode of use:

Copy the code

Or:

Copy the code thank you for reading, these are the contents of "what are the functions of Dubbo?" after the study of this article, I believe you have a deeper understanding of what the functions of Dubbo have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report