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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people are at a loss about how to implement RPC calls with zero cost in Spring Cloud Alibaba+Nacos+Dubbo. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Dubbo's rpc framework has the advantages of high learning cost, strong code invasion, incomplete ecology, and needs to integrate multiple external components, so it chooses the Spring Cloud family bucket with relatively weak performance.
Until the advent of Spring Cloud Alibaba, Nacos was used for service discovery and registration, and was compatible with both http calls using Feign and rpc calls using dubbo.
Why does Spring Cloud need RPC?
In the micro-service system built by Spring Cloud, most developers use the officially provided Feign components for internal service communication. This declarative HTTP client is very simple, convenient and elegant, and has nothing to do with the development platform and language, but usually, HTTP does not turn on the KeepAlive function, that is, the current connection is a short connection. The disadvantage of short connection is that every request needs to establish a TCP connection. This makes it quite inefficient.
It is a very good thing to provide REST API services externally, but if the internal invocation also uses HTTP invocation, the performance will appear to be low. The Feign component used by Spring Cloud by default is called using the HTTP protocol. At this time, if the internal service uses RPC invocation and external use REST API, it will be a very good choice.
Quote to: perfect combination of Dubbo and Spring Cloud
Using Dubbo Spring Cloud to invoke the transformation using the internal RPC protocol is almost zero cost.
I. system structure
Cloud-gateway is the gateway of cloud cluster, external routing forwarding uses http protocol, and internal service invocation uses dubbo protocol.
Remote calls between cloud-user and cloud-mq use the dubbo protocol
Services that use Nacos as the service registration and discovery and configuration center
Using Sentinel as a flow control service for inter-service http and dubbo invocation
Directory structure
├── cloud-admin # Service Monitoring ├── cloud-gateway # Service Gateway ├── cloud-mq # mq Service ├── cloud-provider # Service Interface └── cloud-user # user Service 2, Service Interface provider implementation 1. Service interface definition public interface UserProvider {UserDTO checkUser (String userName, String password); UserDTO findByUserName (String userName);} @ Data@Builder@NoArgsConstructor@AllArgsConstructorpublic class UserDTO implements Serializable {String userName; String realName; String password;}
A service interface is a contract between the service provider and the consumer, which contains the method transfer object DTO of the service. Due to the introduction of multiple application services, it is best to separate them into Module
DTO objects must implement the Serializable interface
two。 Introduction of dubbo package
POM
Com.alibaba.cloud spring-cloud-starter-dubbo org.springframework.boot spring-boot-starter-actuator
Spring-boot-starter-actuator is also necessary.
3. The service interface implements import org.apache.dubbo.config.annotation.Service;@Servicepublic class UserProviderImpl implements UserProvider {@ Autowired private UserService userService; @ Override public UserDTO checkUser (String userName, String password) {User user = userService.checkUser (userName, password); return UserConvertor.toDTO (user);}}
@ Service must be org.apache.dubbo.config.annotation.Service
4. Information related to configuring dubbo services spring: main: allow-bean-definition-overriding: true dubbo: scan: base-packages: fun.barryhome.cloud.provider # specifies the protocol name of the scan base package protocols: dubbo: name: dubbo # Dubbo of the Dubbo service implementation class port:-1 # port is the protocol port (- 1 indicates self-increment port From 20880) registry: address: spring-cloud://localhost # mounts to the Spring Cloud registry
Connection failure may occur after startup, which does not affect the use of
Java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect (Native Method) ~ [na:1.8.0_111] at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:350) ~ [na:1.8.0_111] at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206) ~ [na:1.8.0_111] 3. Service caller implementation 1. Introduce dependency package fun.barryhome cloud-provider 0.0.1-SNAPSHOT compile com.alibaba.cloud spring-cloud-starter-dubbo org.springframework.boot spring-boot-starter-actuator2. Call the service interface import org.apache.dubbo.config.annotation.Reference;public class UserController {@ Reference private UserProvider userProvider; @ GetMapping (value = "/ sessionUser") public UserDTO sessionUser (HttpServletRequest request) {String userName = request.getHeader ("X-User-Name"); if (Strings.isEmpty (userName)) {throw new RuntimeException ("user not found");} return userProvider.findByUserName (userName) }} 3. Information related to configuring dubbo services dubbo: cloud: subscribed-services: cloud-user # Service name of the provider consumer: check: false loadbalance: "leastactive" # minimum active load balancer registry: # Mount to Spring Cloud registry address: spring-cloud://localhost
Dubbo.consumer.check: used to check whether the service provider is running properly during startup. If it is abnormal, the caller cannot be started.
Dubbo.consumer.loadbalance: load balancing strategy
RandomLoadBalance: random, set random probability by weight
ConsistentHashLoadBalance: consistent hash algorithm
LeastActiveLoadBalance: minimum active load balancer
RoundRobinLoadBalance: training in rotation according to weight
IV. Summary
A long connection is used between services. In normal operation, it will take a period of time to switch after a node of the provider is cut off. You can use sentinel to control the fast switching of available nodes.
Using dubbo to make remote calls is provided in terms of internal call performance and relatively simple in call mode.
In cooperation with sentinel, rational use of load strategy can achieve more functions, such as grayscale release, version control, etc.
The performance improvement makes it possible to increase the call chain, enabling smaller granularity of micro-service splitting and composition
After reading the above, have you mastered how Spring Cloud Alibaba+Nacos+Dubbo can implement RPC calls at zero cost? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.