博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SDN 期末作业验收
阅读量:6921 次
发布时间:2019-06-27

本文共 8501 字,大约阅读时间需要 28 分钟。

前言

  • SDN 期末作业验收我们是采用的参考场景一,我们在此场景的基础上来做负载均衡,下面是我们搭建的拓扑图
    885895-20180125160701615-985090526.png

演示视频

负载均衡程序

相关的关键代码

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);                                //这里我们去判断S2交换机通过包的增长数量来选择路径,实现负载均衡            if(received-temp<10000){                //下负载均衡流表                httpRequest(url24,"PUT",8);                httpRequest(url14,"PUT",9);            }else if(received-temp>10000 ){                httpRequest(url21,"PUT",2);                httpRequest(url12,"PUT",4);            }            temp = received;        }        //      jsonObject =  httpRequest(url1,"PUT");                            }}

期末分工

  • 我在本次期末作业中,主要是搭建场景,以及设计负载均衡,和最后的视频录制,关于代码实现这部分我参与的比较少。

    课程总结

  • 虽然在本次课程前我是有接触过一些SDN的相关知识,但在一些课程中也有我之前从未接触过的知识,比例ODL的使用以及开发。也同时在课程中重新复习了SDN的知识,也得到了新的知识。这次的负载均衡也是之前理论上知道,但实际并未去实现。通过这次的SDN课程可以说的是收获很多了。

转载于:https://www.cnblogs.com/deepYY/p/8351831.html

你可能感兴趣的文章
MFC中的DC、CDC、HDC、句柄、设备上下文的不同意思,适合初学者参考
查看>>
mysql复制
查看>>
Spring中配置事务的几种方式
查看>>
php 新漏洞
查看>>
asp.net5 的应用启动
查看>>
HTMLDocument和XMLDocument 的children
查看>>
线性判别分析(LDA)算法总结
查看>>
LINUX/CENTOS 修改mysql的root用户密码
查看>>
linux/centos 搭建本地yum服务器
查看>>
RH436 UNIT 1 CLUSTERS AND STORAGE
查看>>
CentOS 6.5 启动级别
查看>>
SQLite中的SELECT子句使用表达式
查看>>
Android动画曲线库AndroidEasingFunctions
查看>>
判断回文数、回文字符串(从左边读和从右边读一样)
查看>>
Java的SimpleDateFormat类
查看>>
Oracle系列:(33)JDBC访问Oracle的存储过程和存储函数
查看>>
企业实战(3)-主从实现基于Keepalived高可用集群网站架构
查看>>
一键安装lamp脚本--进阶版
查看>>
ngnix安装与配置
查看>>
maven 管理项目实践指南
查看>>