In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 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 easy-to-use functions of dubbo". The content in the article 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 good functions of dubbo"?
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:
Multiple editions
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:
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, as long as any service reference is forcibly transformed to EchoServiceEchoService echoService = (EchoService) demoService
System.out.println (echoService.$echo ("hello"))
Implicit parameter
Can be passed through
Server set:
RpcContext.getContext () .setAttachment ("CRT_MEMBER_ID", "13828886888")
Client get:
RpcContext.getContext () .getAttachment (CRT_MEMBER_ID)
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 ()
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:
DemoServiceMock implementation source 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:
Call source 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:
Configure part:
Log format style:
[2017-11-22 10:23:20] 172.18.1.205 com.alibaba.dubbo.demo.DemoService:1.0.0 sayHello 56144-> 172.18.1.205 purl 20886-com.alibaba.dubbo.demo.DemoService:1.0.0 sayHello (java.lang.String) ["afei"]
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. It's really useful to make Dubbo delay the number of milliseconds before exposing services after the Spring container is initialized. Mode of use:
Or:
Thank you for your reading, the above is the content of "what are the easy-to-use features of dubbo"? after the study of this article, I believe you have a deeper understanding of what the easy-to-use features 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.