SDN期末作业---负载均衡
一.设计场景
我们采用参考场景一,实现负载均衡,拓扑图及端口示意如下:
二.实现代码
package loadBalance;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import net.sf.json.*;
public class Main {
public static JSONObject jsonObject = null;
public static JSONObject[] jsonArray = new JSONObject[100];
static String url24= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0";
static String url14= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1";
static String url21= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0";
static String url12= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1";
public static JSONObject httpRequest(String requestUrl, String requestMethod,int index) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
// http协议传输
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
String userPassword = "admin" + ":" + "admin";
String encoding = Base64.getEncoder().encodeToString((userPassword).getBytes());
httpUrlConn.setRequestProperty("Authorization", "Basic " + encoding);
httpUrlConn.setRequestProperty("Connection", "Keep-Alive"); // 设置维持长连接
httpUrlConn.setRequestProperty("Charset", "UTF-8");// 设置文件字符集:
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
{
httpUrlConn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
// System.out.println(buffer.toString());
}else if("PUT".equalsIgnoreCase(requestMethod)){
byte[] data = (jsonArray[index].toString()).getBytes();//转换为字节数组
httpUrlConn.setRequestProperty("Content-Length", String.valueOf(data.length));// 设置文件长度
httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
// 开始连接请求
httpUrlConn.connect();
OutputStream out = httpUrlConn.getOutputStream();
// 写入请求的字符串
out.write((jsonArray[index].toString()).getBytes());
out.flush();
out.close();
if (httpUrlConn.getResponseCode() == 200) {
System.out.println("发送成功");
}
}else if("DELETE".equalsIgnoreCase(requestMethod)){
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static void init() throws IOException{
String s = null;
int i = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("DATA.txt"),"UTF-8"));
while((s = br.readLine())!=null){
jsonArray[i] = JSONObject.fromObject(s);
i++;
}
String url31= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/0";
String url32= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/1";
String url11= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/0";
String url22= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/1";
String url13= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/2";
String url23= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/2";
String url33= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/2";
httpRequest(url31,"PUT",0);
httpRequest(url32,"PUT",1);
httpRequest(url21,"PUT",2);
httpRequest(url11,"PUT",3);
httpRequest(url12,"PUT",4);
httpRequest(url22,"PUT",5);
httpRequest(url13,"PUT",6);
httpRequest(url23,"PUT",7);
httpRequest(url33,"PUT",10);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int getReceived(String url2){
jsonObject = httpRequest(url2,"GET",0);
JSONArray j1 = (JSONArray) jsonObject.get("node-connector");
JSONObject j2 = (JSONObject) j1.get(0);
JSONObject j3 = (JSONObject) j2.get("opendaylight-port-statistics:flow-capable-node-connector-statistics");
JSONObject j4 = (JSONObject)j3.get("bytes");
int received = (int) j4.get("received");
return received;
}
public static void main(String[] args) {
System.out.println("-------------------------------------------------");
try {
init();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s = "";
String url = "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/1";
String url1= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0";
String url2 = "http://172.17.172.244:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:2/node-connector/openflow:2:2";
// jsonObject = httpRequest(url2,"GET",0);
// System.out.println(jsonObject.toString());
int received ;
int temp = 0;
while(true){
received = getReceived(url2);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(received-temp);
if(received-temp<10000){
//下负载均衡流表
// System.out.println(received-temp);
httpRequest(url24,"PUT",8);
httpRequest(url14,"PUT",9);
}else if(received-temp>10000 ){
// System.out.println(received-temp);
httpRequest(url21,"PUT",2);
httpRequest(url12,"PUT",4);
}
temp = received;
}
// jsonObject = httpRequest(url1,"PUT");
}
}
三.演示视频
四.团队分工
负责查阅资料,辅助代码编写,参与小组讨论
五.课程总结
SDN是一种新型的网络架构,它的设计理念是将网络的控制平面与数据转发平面进行分离,从而通过集中的控制器中的软件平台去实现可编程化控制底层硬件,实现对网络资源灵活的按需调配。在SDN网络中,网络设备只负责单纯的数据转发,可以采用通用的硬件;而原来负责控制的操作系统将提炼为独立的网络操作系统,负责对不同业务特性进行适配,而且网络操作系统和业务特性以及硬件设备之间的通信都可以通过编程实现。
与传统网络相比,SDN的基本特征有3点:
- 控制与转发分离。转发平面由受控转发的设备组成,转发方式以及业务逻辑由运行在分离出去的控制面上的控制应用所控制。
- 控制平面与转发平面之间的开放接口。SDN 为控制平面提供开放可编程接口。通过这种方式,控制应用只需要关注自身逻辑,而不需要关注底层更多的实现细节。
- 逻辑上的集中控制。逻辑上集中的控制平面可以控制多个转发面设备,也就是控制整个物理网络,因而可以获得全局的网络状态视图,并根据该全局网络状态视图实现对网络的优化控制。
总结下来,软件定义网络(SDN)就是通过它的核心技术OpenFlow将网络设备控制面与数据面分离开来,从而实现了网络流量的灵活控制,为核心网络及应用的创新提供了良好的平台。
六.个人收获
通过本学期软件定义网络这门课程的学习,让我收获了许多。在还没接触这门课程之前,我对这方面的知识很陌生,可以说基本没怎么接触过,后面通过老师上课的讲解以及课上的实验操作,让我慢慢了解了SDN的相关知识,从搭建一个网络拓扑到下发流表,以及最后的期末作业实现负载均衡,让我学到了很多。但同时实验中也遇到了很多困难,说明我的理论知识还是不够完善,尽管这门课程结束了,但这门课程的学习不能停止,今后的日子还需要进一步的学习。