part2 Pingora Tutorial
https://github.com/cloudflare/pingora
负载均衡
复刻part1的负载均衡配置
这里使用http,其中xxxxxxxxx是我的公网ipv4地址
main.rs
use async_trait::async_trait;
use pingora::prelude::*;
use std::sync::Arc;
pub struct LB(Arc<LoadBalancer<RoundRobin>>);
#[async_trait]
impl ProxyHttp for LB {
/// For this small example, we don't need context storage
type CTX = ();
fn new_ctx(&self) -> () {
()
}
async fn upstream_peer(&self, _session: &mut Session, _ctx: &mut ()) -> Result<Box<HttpPeer>> {
let upstream = self
.0
.select(b"", 256) // hash doesn't matter for round robin
.unwrap();
// println!("upstream peer is: {:upstream?}");
// Set SNI to one.one.one.one
let peer = Box::new(HttpPeer::new(
upstream,
false,
"one.one.one.one".to_string(),
)); // the second argument, false for http, true for https
Ok(peer)
}
async fn upstream_request_filter(
&self,
_session: &mut Session,
upstream_request: &mut RequestHeader,
_ctx: &mut Self::CTX,
) -> Result<()> {
upstream_request
.insert_header("Host", "xxxxxxxxxxx")
.unwrap();
Ok(())
}
}
fn main() {
let mut my_server = Server::new(None).unwrap();
my_server.bootstrap();
let upstreams = LoadBalancer::try_from_iter(["127.0.0.1:12777", "127.0.0.1:12778"]).unwrap();
let mut lb = http_proxy_service(&my_server.configuration, LB(Arc::new(upstreams)));
lb.add_tcp("0.0.0.0:12803");
my_server.add_service(lb);
my_server.run_forever();
}
Then I can access http://xxxxxxx:12803/say
to access my assets.
0 评论:
发表评论