In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Spring Cloud developers to solve service conflicts and instances of sample analysis, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.
I. Background
When we develop a microservice architecture system, although each microservice is isolated and can be developed separately, it is actually not the case. To debug and test your service, you need not only your microservice to run, but also its context services and dependent basic services. Is there any way to improve development efficiency?
As shown in the above figure, can we deploy all the services with the server, and then develop only the services we are responsible for running locally, because we need to rely on other services, so locally started services also need to be registered in the public registry;
In this example, there are 3 instances of business service B registered in the registry
Respectively: service, development A and development B own start-up
However, doing so will cause a new problem: services will conflict, which means that when developer A debugs its own business service B, the request may jump to someone else's instance (server, developer B).
II. Solutions
An elegant way to solve this service chaos problem is to customize Load Balancer rules to achieve the following goals:
When an ordinary user accesses a page on the server, all routes requested call only instances on the server
When Developer A accesses, all routes requested will call instances started locally by Developer A first, if not, instances on the server will be called.
When Development B accesses, the same as above. All the routes requested will call the instance started by Development B first. If there is no instance, the instance on the server will be called.
III. Concrete realization
There are two key problems that need to be solved to achieve the above goals
Differentiate service instances for different users
Implement custom Load Balancer rules
3.1. Differentiate service instances for different users
You can use the metadata of the registry directly to distinguish
Mainstream registries have metadata management
Take Nacos for example, just add it under the configuration file
spring: cloud: nacos: discovery: server-addr: localhost:8848 metadata: version: zlt
The version under metadata is the metadata I added. Key is version, value is zlt.
After starting the service, metadata will be registered, as shown in the following figure
After metadata differentiation, the current situation is as follows: the instance version of the server is empty, and the instance version started locally by the developer is the unique identifier (own name).
3.2. Custom Load Balancer Rules
Load Balancer of instances in the Spring Cloud microservices framework is first handled by the Ribbon.
CustomIsolationRule class
public class CustomIsolationRule extends RoundRobinRule {/** * Take instance first according to version number */@Overridepublic Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null; } String version = LbIsolationContextHolder.getVersion(); List targetList = null; List upList = lb.getReachableServers();if (StrUtil.isNotEmpty(version)) {//Take an instance of the specified version number targetList = upList.stream ().filter( server -> version.equals( ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION) ) ).collect(Collectors.toList()); }if (CollUtil.isEmpty(targetList)) {//only take instances without version number targetList = upList.stream ().filter( server -> { String metadataVersion = ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION);return StrUtil.isEmpty(metadataVersion); } ).collect(Collectors.toList()); }if (CollUtil.isNotEmpty(targetList)) {return getServer(targetList); }return super.choose(lb, key); }/** * Take a random instance */private Server getServer(List upList) {int nextInt = RandomUtil.randomInt(upList.size());return upList.get(nextInt); }}
Integrated polling rule RoundRobinRule to implement, the main logic is
According to the version number input upstream, if there is a value, the instance with the same version value in the service meta information is taken.
If the version number of the upstream has no value or does not match any service, only the version value in the service meta information is empty.
And through the configuration switch control whether to open custom load rules
@Configuration@ConditionalOnProperty(value = "zlt.ribbon.isolation.enabled", havingValue = "true")@RibbonClients(defaultConfiguration = {RuleConfigure.class})public class LbIsolationConfig {}
Related source code
https://gitee.com/zlt2000/microservices-platform/blob/master/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/rule/CustomIsolationRule.java
IV. Summary
Differentiated service instances and custom load rules mentioned above are the core points of the whole solution idea. What remains to be done is how to specify the upstream version. Here are two ideas
Developers start the front-end project by themselves, and pass the version uniformly in the front-end project through configuration parameters.
Add the header parameter when calling the interface via postman
Thank you for reading this article carefully. I hope that the article "Spring Cloud developers solve service conflicts and example chaos" shared by Xiaobian will help everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
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.