Nginx Configuration of loading balance

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yexianyi/article/details/81252972

Abstract:

This example will show how to config a simple Nginx loading balance with Round-Robin policy, which involves 4 node servers to run web app and 1 nginx server to do load balance. Now, I will demonstrate this case with Docker.

Configuration:

1. Setup 4 app servers

Since I already programmed a simple web app with Springboot and published it as Docker Image, it is quite easy to launch the 4 node servers by calling following commands in 4 times. 

docker run -it -d yexianyi/chukonu-springboot

If we exec to these nodes, we will get their service urls:

http://172.17.0.3:9000
http://172.17.0.4:9000
http://172.17.0.5:9000
http://172.17.0.6:9000

2. Setup and config Nginx

1) The installation process will be ignored and you may refer to my pervious blog for details.

2) config /etc/nginx/nginx.conf

2.1) create upstream under http section:

upstream tomcat-pool {
      server 172.17.0.3:9000;
      server 172.17.0.4:9000;
      server 172.17.0.5:9000;
      server 172.17.0.6:9000;
}

Note that underline "_" is not supported, so we use dash "-" instead.

2.2) Config Server Section

server {
        listen  8080;
        server_name localhost;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_pass http://tomcat-pool;
        }

    #Other config are ignored
    #...

}

listen: the port number will be used for access service.

server_name: the server address will be used for access service.

proxy_pass: the upstream name we create in step 2.1.

2.3) Save above update

2.4) Refresh config by invoking following command

/usr/sbin/nginx -s reload

3. Testing

Now, there is a rest service published on endpoint http://<server_ip>:<port>/function/local_ip, which will display the underlying server address. For example, if we call http://localhost:8080/function/local_ip several times, the result should be populated from 172.17.0.3 to 172.17.0.6 in a RR way, such as

{"address":"172.17.0.3"}
{"address":"172.17.0.4"}
{"address":"172.17.0.5"}
{"address":"172.17.0.6"}
{"address":"172.17.0.3"}
{"address":"172.17.0.4"}
{"address":"172.17.0.5"}
{"address":"172.17.0.6"}
{"address":"172.17.0.3"}

Appendix:

My webapp docker image: yexianyi/chukonu-springboot

展开阅读全文

Balance

08-19

Problem DescriptionnAn investor invests a certain percentage of his assets into NINSTRUMENTS financial instruments. After each term, these instruments deduct a certain fixed administrative cost, followed by a fee that is a percentage of the amount that was invested at the beginning of the term, and then add a return, which is a (positive or negative) percentage of the amount invested at the beginning of the term. If any account drops to zero or below after such a transaction, it is considered closed (no fees are charged against it, and is treated as simply zero) until a rebalancing occurs. Rebalancing occurs after every NREBALANCE terms, where the total assets of the investor are redistributed according to the original ratios for the instruments. Without rebalancing, the investor's assets would become dominated by the higher return instruments, which would expose them to more risk compared to a balanced investment plan. Note that it is possible that all instruments drop to zero, in which case they all remain closed for the remaining terms. You are to model the value of such an investment strategy and report the ending value in each instrument (before rebalancing, if it happens to land on a term when a rebalance is due). Compute your results using double precision (do not round intermediate values to pennies), but round your final answers to pennies.n nnInputnThe first line of the input contains the three positive integers:nNINSTRUMENTS NTERMS NREBALANCEnThere are no more than 10 instruments, and the number of terms is at most 20. This is followed by 3 lines of floating-point numbers separated by spaces, in the following format:nFIXED_FEE(1) .. FIXED_FEE(NINSTRUMENTS)nPERCENTAGE_FEE(1) .. PERCENTAGE_FEE(NINSTRUMENTS)nPRINCIPAL_START(1) .. PRINCIPAL_START(NINSTRUMENTS)nFinally, there are NTERMS lines each containing NINSTRUMENTS floating-point numbers indicating the percentage return of each instrument in each term:nRETURN(1,1) .. RETURN(1,NINSTRUMENTS)nRETURN(2,1) .. RETURN(2,NINSTRUMENTS)n.n.nRETURN(NTERMS,1) .. RETURN(NTERMS,NINSTRUMENTS)nAll percentages (PERCENTAGE_FEE and RETURN) are given as ratios, up to 4 decimal places. For example, a fee of 0.0002 means 0.02% of the investment in this instrument is deducted as a fee each term. FIXED_FEE and PRINCIPAL_START are non-negative floating-point numbers that are specified to 2 decimal places. At least one of the PRINCIPAL_START values is positive.n nnOutputnWrite on a single line the principal of each investment (separated by a space) at the end of NTERMS terms. Round each principal to the nearest penny.nPRINCIPAL_END(1) .. PRINCIPAL_END(NINSTRUMENTS)n nnSample Inputn4 10 5n5.00 10.00 20.00 50.00n0.002 0.001 0.0008 0.0005n150000.00 100000.00 75000.00 50000.00n0.10 0.05 -0.05 -0.85n0.10 0.05 -0.10 -0.85n0.10 0.05 -0.20 -0.85n0.10 0.05 -0.40 -0.85n0.10 0.05 -0.80 -0.85n0.10 0.05 -0.05 -0.90n0.10 0.05 -0.05 -0.90n0.10 0.05 -0.05 -0.90n0.10 0.05 -0.05 -0.85n0.10 0.05 -0.05 -0.85n nnSample Outputn237698.69 126086.01 57298.74 0.00 问答

The Balance

10-06

DescriptionnnMs. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. nYou are asked to help her by calculating how many weights are required. nn![](http://poj.org/images/2142_1.jpg)nInputnnThe input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases. nThe end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.nOutputnnThe output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions. nYou can measure dmg using x many amg weights and y many bmg weights. nThe total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition. nThe total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.nnNo extra characters (e.g. extra spaces) should appear in the output.nSample Inputnn700 300 200n500 200 300n500 200 500n275 110 330n275 110 385n648 375 4002n3 1 10000n0 0 0nSample Outputnn1 3n1 1n1 0n0 3n1 1n49 74n3333 1 问答

Station Balance

05-21

Problem DescriptionnThe International Space Station contains many centrifuges in its labs. Each centrifuge will have some number (C) of chambers each of which can contain 0, 1, or 2 specimens. You are to write a program which assigns all S specimens to the chambers such that no chamber contains more than 2 specimens and the following expression for IMBALANCE is minimized. nnnnwhere: nnCMi is the Chamber Mass of chamber i and is computed by summing the masses of the specimens assigned to chamber i. nnAM is the Average Mass of the chambers and is computed by dividing the sum of the masses of all specimens by the number of chambers (C). nn nnInputnInput to this program will be multiple sets of input. The first line of each set will contain two numbers. The first number (1 <= C <= 5) defines the number of chambers in the centrifuge and the second number (1 <= S <= 2C) defines the number of specimens in the input set. The second line of input will contain S integers representing the masses of the specimens in the set. Each specimen mass will be between 1 and 1000 and will be delimited by the beginning or end of the line and/or one or more blanks. n nnOutputnFor each input set, you are to print a line specifying the set number (starting with 1) in the format "Set #X" where "X" is the set number. nnThe next C lines will contain the chamber number in column 1, a colon in column number 2, and then the masses of the specimens your program has assigned to that chamber starting in column 4. The masses in your output should be separated by exactly one blank. nnYour program should then print ``IMBALANCE = X" on a line by itself where X is the computed imbalance of your specimen assignments printed to 5 digits of precision to the right of the decimal. nnThe final line of output for each set should be a blank line. (Follow the sample output format.) nn nnSample Inputn2 3n6 3 8n3 5n51 19 27 14 33n5 9n1 2 3 5 7 11 13 17 19n nnSample OutputnSet #1n 0: 6 3n 1: 8nIMBALANCE = 1.00000nnSet #2n 0: 51n 1: 19 27n 2: 14 33nIMBALANCE = 6.00000nnSet #3n 0: 1 17n 1: 2 13n 2: 3 11n 3: 5 7n 4: 19nIMBALANCE = 11.60000 问答

Best Balance

03-14

FatMouse has just got a baby! In order to control the baby's mouse weight, he bought a balance as well as some weights. By placing his baby on one side of the balance and some of the weights on the other, he could read how heavy his baby is. However, the baby is catching on weight so fast that FatMouse worries he would not be able to do that one day. Thus he has decided to purchase several other weights. He can choose how heavy these weights are. And of course he wants to maximize the smallest weight that this balance can not measure.nAll weights are integers (in pounds). The balance can detect and compensate any fractional weight (less than one pound) difference between two sides. FatMouse never places any weight on the same side of his baby.nnInput DescriptionnnStandard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. T test cases follow, each preceded by a single blank line.nnEach case starts with two integers n (0 <= n <= 20) and k (0 <= k <= 10), where n is the number of the weights that FatMouse currently has, and k is the number of weights that FatMouse is going to purchase. The next n integers ci (0 <= i < n, 1 <= ci < 100,000) give the weights (in pounds) he currently owns. Two consecutive numbers are separated by a single space.nnOutput DescriptionnnResults should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.nnThe output of each test case should be a single integer, which is the maximized smallest weight that he will not be able to measure.nnSample Inputnn4nn3 1 1 2 4nn3 2 1 3 5nn2 1 1 1nn3 0 1 2 5nnSample OutputnnCase 1:n16nnCase 2:n24nnCase 3:n6nnCase 4:n4nn 问答

没有更多推荐了,返回首页