当前位置:网站首页>It doesn't make sense without a distributed gateway
It doesn't make sense without a distributed gateway
2022-07-06 17:48:00 【InfoQ】
Common way
nginx+lua
- nginx It's a high performance HTTP Reverse proxy . In our previous projects, we used nginx There are two main uses : Direction agent + Static resource server management
- Click me to download
- Once the download is complete nginx It is also very convenient to start , What the author demonstrates here is windows Version of . You can either click on the application directly or start it in command line mode
./nginxperhapsnginx. See the specific system

The service broker
- Under normal circumstances, agents use hair HTTP Under the Server To configure the . For example, we need a simple proxy for one of our services
http://192.168.44.131:9200.. The effect we want is through local localhost You can access the interface provided by this service .
server {
listen 9200;
server_name localhost;
location / {
proxy_pass http://192.168.44.131:9200;
#root html;
#index index.html index.htm;
}
}
- Of course, after the configuration, we need to
nginx -s reloadTo restart nginx To reload the configuration . At this time we visitlocalhost:9200/zxhtom/startIn fact, it is represented tohttp://192.168.44.131:9200/zxhtom/startOn this interface .
Static resource agents
- As we mentioned above, in addition to service agents .nginx It can also be used as a static resource server .
server{
listen 90;
server_name localhost;
location / {
root D:\study\PageOffice_4.6.0.3_Java;
index index.html;
}
}
- It also uses HTTP Under the SERVER . This code means that it will be local
D:\study\PageOffice_4.6.0.3_JavaThis folder acts as a local 90 Port entry . We go throughlocalhost:90/index.htmlYou can access localD:\study\PageOffice_4.6.0.3_Java\index.htmlThe file .
Load balancing
upstream redis {
hash $remote_addr consistent;
# $binary_remote_addr;
server 192.168.44.131:6379 weight=5 max_fails=3 fail_timeout=30s;
server 192.168.44.132:6379 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 6379;#redis Server listening port
proxy_connect_timeout 10s;
proxy_timeout 300s;# Set the timeout between the client and the proxy service , If 5 If there is no operation within minutes, it will be disconnected automatically .
proxy_pass redis;
}
- Sometimes our services are not just single node Services . If it is distributed, we need to carry out load balancing strategy for services . At this time, we can't just agent services . We need to add load policy . We go through
upstreamTo define services , And define the service policy . Finally, we server Used directly inupstreamThe service name is ok . Among them weight It is to set the weight of service polling in the cluster .
Common commands
zuul
- zuul yes netflix Open source project , About zuul In simple terms, what I analyzed in the previous article should be very comprehensive .Let me see zuul chapter
- adopt zuul We can realize dynamic routing 、 Certificate authority 、 Dynamic filters 、 The final thing is that it can be customized or combined hystix Carry out service
- zuul It can also realize gray-scale publishing, which can solve our downtime publishing problem . Think about if your system is 24 It can't stop for hours zuul To achieve gray Publishing
Grayscale expansion

- Above, we realize the forwarding of the route through the parameters specified in the request. The implementation principle is with the help of eureka Of metadata Parameter attribute of the route .
- But we should have encountered some software that treat different regions differently .
- For example, Alipay ant forest has different strategies in different cities
- For example, a software invites you to participate in the use of the inner version
- All of the above are collectively referred to as grayscale publishing , The principle is also very simple. In fact, there are many examples ,zuul Forward to different instances according to the characteristics of the request . Different cities route by Region , Inside the invitation is to route through personal user information . Their implementation is inseparable from what we described above
Ribbon.Predicate. If we want to expand grayscale publishing, we cannot do withoutPredicate
Predicate
- The function of this class is to make assertions , yes google The idea put forward . Specific about googleTap me gently
- stay ribbon Special topic , We simply read the source code to understand ribbon How to carry out load balancing and internal load balancing strategies . If you are interested, you can click the homepage to find .
- Today we'll look at it ribbon How to filter the list of services before load balancing .
// Return assertions based on input true or false
@GwtCompatible
public interface Predicate<T> {
// Assert against the input , This method has and not only has the following requirements : 1、 Will not cause any data pollution 2、 stay T Of equals Equal in apply Is the same effect
boolean apply(@Nullable T input);
// Return to two Predicate Are they the same? . General situation Predicate The implementation does not need to be rewritten equals Of . If the implementation can show according to their own needs predicate Are they the same? . What is the same is two predicate object apply The same result means the same object
@Override
boolean equals(@Nullable Object object);
}
- So let's go through Predicate To achieve simple data filtering . Of course, some people will say why not java8 stream What about filtering . This is just for Ribbon Medium service filtration paving . as for ribbon Why not use stream operation ? Personal role google Of Predicate It is more convenient in decoupling .
@Test
public void pt() {
List<User> userList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
userList.add(new User(Long.valueOf(i+1), " Zhang San "+(i+1)));
}
Predicate<User> predicate = new Predicate<User>() {
@Override
public boolean apply(User user) {
return user.getId() % 2 == 0;
}
};
ArrayList<User> users = Lists.newArrayList(Iterables.filter(userList, predicate));
System.out.println(users);
}
AbstractServerPredicate
Premise recall
- As can be seen from our class structure diagram above
AbstractServerPredicateyesPredicateImplementation class of . This class is also Ribbon The key role in getting the list of services . Because the function expansion is based on this class .

jmnarloch First time to know
- This is my in RIbbon The content in . We can know Ribbon In the end
BaseLoadBalancerLoad balancing in . Inside rule The default isnew RoundRobinRule(), Because we introducedio-jmnarloch. First look at the internal class structure

io-jmnarlochThe interior is not very complicated , At least Ribbon、feign Compared with these, he is really simple . There are four inside package
Register load Rule

- We can see in the support In bag
RibbonDiscoveryRuleAutoConfigurationConfiguration of the rule Package defined Ribbon Load balancing class Rule.

- The power failure hit
BaseLoadBalancerWe can see that rule It's usrule.MetadataAwareRuleThis class . Here and ribbon There seems to be a discrepancy in the chapters , We are ribbon The chapter says you need to customize rule I need to be in@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)This way, .
- It's actually configuring
DiscoveryEnabledRuleWhen registering, there is@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)Represents the scope
MetadataAwareRule How to filter Services

- adopt
MetadataAwareRuleCombined with the code, we can know that the final result is PredicateBaseRule#choose Select the service list


- This predicate It's us choose in getPredicate() Method acquired . So in ribbon Before selecting services, you will pass
MetadataAwarePredicateFilter Services .
- After getting the filter object , We will execute
chooseRoundRibbinAfterFiltering.
go back to AbstractServerPredicate

- As mentioned above, it will finally pass Predicate To carry out chooseRoundRobinAfterFiltering. Remember the beginning predicate Have you got the structure diagram of .
MetadataAwarePredicateFinally inheritAbstractServerPredicate. andAbstractServerPredicate#chooseRoundRobinAfterFilteringIt's dependence getEligibleServers` To get the appropriate list of services .
- stay
AbstractServerPredicateAchieved a lot chooseXXX Methods . because ribbon The default is polling, so in BaseLoadBalance Middle is choice Round Corresponding method . We can modify these methods by ourselves . I won't repeat it here
EligibleTranslate into appropriate .getEligibleServers Get the right list of services .

- We can obviously see that the final filtering logic falls in apply On the way .

- This is what we passed above
metadata-map:lancherConfigure our service information .
- Here is
AbstractServerPredicateAfter simplification . MainlygetEligibleServersThis method .

Subclass
- on top
AbstractServerPredicateIn the structure diagram, we can see that exceptDiscoveryEnabledPredicateOutside this subclass , There are four subclasses .
边栏推荐
- 自动化运维利器ansible基础
- FlutterWeb瀏覽器刷新後無法回退的解决方案
- Unity粒子特效系列-闪星星的宝箱
- PyTorch 提取中间层特征?
- Interpretation of Flink source code (II): Interpretation of jobgraph source code
- Xin'an Second Edition: Chapter 26 big data security demand analysis and security protection engineering learning notes
- sql语句优化,order by desc速度优化
- 02 personal developed products and promotion - SMS platform
- The art of Engineering (1): try to package things that do not need to be exposed
- 中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
猜你喜欢

虚拟机启动提示Probing EDD (edd=off to disable)错误

基于STM32+华为云IOT设计的智能路灯

The problem of "syntax error" when uipath executes insert statement is solved

网络分层概念及基本知识

Interpretation of Flink source code (II): Interpretation of jobgraph source code

Spark accumulator and broadcast variables and beginners of sparksql

【MySQL入门】第一话 · 初入“数据库”大陆
![[ASM] introduction and use of bytecode operation classwriter class](/img/0b/87c9851e577df8dcf8198a272b81bd.png)
[ASM] introduction and use of bytecode operation classwriter class

【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符

Zen integration nails, bugs, needs, etc. are reminded by nails
随机推荐
分布式不来点网关都说不过去
connection reset by peer
EasyCVR授权到期页面无法登录,该如何解决?
Grafana 9 正式发布,更易用,更酷炫了!
Based on infragistics Document. Excel export table class
遠程代碼執行滲透測試——B模塊測試
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
[translation] principle analysis of X Window Manager (I)
Solr appears write Lock, solrexception: could not get leader props in the log
TCP连接不止用TCP协议沟通
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
TCP connection is more than communicating with TCP protocol
Unity粒子特效系列-闪星星的宝箱
Guidelines for preparing for the 2022 soft exam information security engineer exam
Single responsibility principle
C WinForm series button easy to use
学 SQL 必须了解的 10 个高级概念
Unity particle special effects series - treasure chest of shining stars
Concept and basic knowledge of network layering
Flink parsing (VII): time window