1.修改配置文件
conf/nginx.conf
2.配置文件中增加服务调用
upstream NET6RC2{
server 127.0.0.1:5726;
server 127.0.0.1:5727;
server 127.0.0.1:5728;
server 127.0.0.1:5729;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://NET6RC2; #转发给上面upstream中的地址
}
}
3.启动Nginx服务
start nginx
重启:nginx -s reload
4.前端调用
#region 负载均衡Nginx
string url = null;
url = "http://localhost:8080/api/users/all"; //访问nginx地址 + api函数方法
string content = this.InvokeApi(url);
base.ViewBag.Users = JsonConvert.DeserializeObject<IEnumerable<User>>(content);
Console.WriteLine($"This is {url} Invoke");
endregion
5.封装的Invoke请求API方法
private string InvokeApi(string url)
{
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage message = new HttpRequestMessage();
message.Method = HttpMethod.Get;
message.RequestUri = new Uri(url);
var result = httpClient.SendAsync(message).Result;
string content = result.Content.ReadAsStringAsync().Result;
return content;
}
}
标签:负载,string,url,0.1,server,content,Nginx,message,搭建 From: https://www.cnblogs.com/jiangyuhu/p/17726353.html