In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what is the use of dubbo-go in java. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Technical background
2020 is the year of our company's development and growth. The entire company team has expanded from hundreds of people to thousands of people now. When it is used intensively, there will basically be thousands of people operating in the operation background at the same time. The original internal background operation system of the company is built with PHP, and the performance and business have gradually failed to meet the company's demand planning. In addition, the development department of our company has already done microservice splitting, and the main external service is Dubbo cluster in java language. The background system needs to seamlessly interface with java's Dubbo service, so PHP has gradually failed to meet the needs of our company.
At that time, I also investigated the PHP Dubbo project, because the project has been basically unupdated and maintained, so I passed it. Later, I was interested in the simple and high-performance go language, and then I paid attention to the Dubbo-go project. After a period of research, I found that Dubbo Go meets our business needs, and the community is very active. Later, I decided to choose Dubbo-go as the PC business framework in the background.
Some students may also ask why not use gRPC with better cross-language support, because many companies 'initial RPC service clusters are built based on the Dubbo ecosystem. If the cost of changing the framework is too large, it will not be considered basically. Although gRPC has better cross-language support, many things need to be built by themselves, such as service registration, service discovery, log monitoring, etc.
At that time, when deciding to choose Dubbo-go, there were also some opposition voices within the development, why not directly switch to java, then there is no problem of cross-language communication, the problem of switching to java is that the entry cost is high, and for the entire company's technology stack, maintain the diversity of languages, in order to more calmly cope with future business changes, Go itself is not weaker than Java high-performance language, very suitable for microservice architecture.
the challenges facing
After determining the framework selection, the first task I received was to build a scaffold that could quickly create business projects, develop RPC proxy services based on HTTP protocol, deploy containerized deployment platforms that needed to be connected to the company, and everything started from scratch. There was basically no information that could be used for reference on the Internet. First of all, we need to plan the architecture of Dubbo-go project and determine the directory structure of the project. After referring to Dubbo-go Demo and other Go projects, we finally determine the directory structure of the project. The following directory structure can be used as reference.
In keeping with Java Services Registry, Dubbo-go selects the following components for project selection:
Using zookeeper as a registry
nacos as a configuration center
Database ORM using gorm
Message queuing using RocketMQ
In order to increase the efficiency of development, we can streamline the configuration before the initialization of the provider service. Only the most basic configuration can be retained. It can be similar to the following. The coding of the provider service can be referenced by Dubbo-go demo.
Here is the main method code for service startup
Design of Dubbo-go RPC Service Gateway
Generally, when using Dubbo, the provider side needs to expose the interface and method, and the consumer side needs to be very clear about the interface definition and method definition used by the service, as well as the information such as the type of input parameter and return parameter, etc. It also needs to be based on the API provided by the provider side before the two sides can communicate normally.
However, the gateway usage scenario does not care about the detailed definition of the interface to be invoked. The gateway only cares about the method to be invoked, the parameters passed, and the ability to receive the returned result. The basis for implementing the gateway proxy is the generalized invocation feature of Dubbo/Dubbo-go.
The following is the demo given by Dubbo-go official. After the generalized service is loaded, it takes 3 seconds to complete the call. However, when it is actually used, it is definitely impossible to load the service in real time to wait 3 seconds, so it is necessary to load the cache when the gateway application starts.
After studying the Dubbo-go generalized invocation demo, it is found that it is feasible to design dubbo-go gateway with this feature. The difficulty lies in that we need to obtain and cache the parameters of each RPC service method that requires gateway proxy and the path of the service. In this way, the generalized invocation service can be initialized before invocation. The configuration of a service is as follows.
Because it is a gateway proxy made in go language, it is impossible to obtain Java RPC service configuration through Java jar package. If it is manually maintained, the workload is too large and error-prone, which is obviously unacceptable. After a period of understanding, Java services can obtain configuration through annotations. Java will send configuration information to MQ when starting services after adding annotations to methods. Gateway consumes these messages to obtain configuration of Java RPC services.
RPC service of Dubbo Go Since go language does not support annotations, I wrote a small tool for scanning code after thinking, adding corresponding comments before each RPC service method, obtaining RPC service configuration by scanning comments, generating RPC service configuration in project directory after obtaining configuration, reading configuration and sending to MQ when starting application.
After the gateway proxy is implemented, more functions can be implemented on the basis of the gateway, such as token verification, white list, current limiting, fuse, and log monitoring functions. The implementation effect of the gateway proxy request is as follows:
containerized deployment
The company's internal containerized deployment environment is Alibaba Cloud's k8s. Deployment to the k8s platform only requires the provision of image files. Since Dubbo-go is a binary file after compilation, it does not require any additional third-party libraries and can run stably in Docker environment. Docker image file as shown below, you can use any linux distribution such as centos as a base image.
LABEL maintainer=""LABEL version="1.0"LABEL description="KKL-GO-NKO-BASE"`ARG envType=stable#Set environment variable ENV envType ${envType}#Compile packaged package ADD ./ target/nko-base-${envType}.tar.gz /app/WORKDIR /appEXPOSE 20000
After the image is written, it is provided to the publishing platform, and the publishing platform machine starts the image and decompresses the packaged file, and executes the Dubbo-Go program.
Container entrypoint set to [bash, -c, tar -zxf nko-base-stable.tar.gz && SERVER_ENV=kubernetes && sh ./ nko-base/bin/load.sh start -group=stable]
Since there are usually multiple deployment environments from development testing to production, we need to change the compilation script in the dubbo-go samples demo [see related link 1] to support multi-environment packaging.
In addition, the IP registered by Dubbo-go by default is the virtual IP of the k8s pod. The network between different k8s clusters cannot be interconnected. Therefore, if you need to call across clusters, you need to modify the default registered IP. Modify the default registered pod IP + port to the IP of the kubernetes physical machine plus the corresponding port. Kubernetes will write the IP of the physical machine plus the corresponding port environment variable in the pod. Application programs can obtain IP plus port of physical machine by reading environment variables. If this function needs to be realized, it is necessary to modify the registration logic of Dubbo-go. For example, taking zookeeper registry as an example, we can modify the registered IP and port by extending the registerTempZookeeper/registry.go registerTempZookeeper Node method. The code is shown below. Dubbo-go officially supports the function of custom registered IP and port in the form of configuration in the later version.
func (r *zkRegistry) registerTempZookeeperNode(root string, node string) error { ... regIp = os.Getenv(constant2.RegistryEnvIP) //ip regPort = os.Getenv(constant2.RegistryEnvPort) //port urlNode, _ := common.NewURL(node) role, _ := strconv.Atoi(urlNode.GetParam(constant.ROLE_KEY, "")) if role == common.PROVIDER && regIp != "" && regPort != "" { urlNode.Ip = regIp urlNode.Port = regPort node = url.QueryEscape(urlNode.String()) } zkPath, err = r.client.RegisterTemp(root, node) ...} About "java dubbo-go what is useful" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
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.