Java小白一文讲清Java中集合相关的知识点(四)

LinkedList底层结构

  • LinkedList底层实现了双向链表和双向队列特点
  • 可以添加任意元素,包括null,元素可以重复
  • 线程不安全,没有实现同步
LinkedList底层操作机制
  • LinkedList底层维护了一个双向链表
  • LinkedList中维护了两个属性first和last分别指向首结点和尾结点
  • 每个结点(Node 对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个结点,最终实现双向链表
  • 所以LinkedList的元素的增加和删除,不是通过数组完成的,相对来说效率较高
  • 模拟一个简单的双向链表
@SuppressWarnings({"all"})
public class Journey {
    public static void main(String[] args) {
        //模拟一个简单的双向链表
        Node life = new Node("life");
        Node is = new Node("is");
        Node journey = new Node("a journey");

        //各个结点相连,形成双向链表life is a journey
        life.next = is;
        is.next = journey;

        journey.pre = is;
        is.pre = life;

        //让first引用指向jack,其就是双向链表的头结点
        Node first = life;
        //让last引用指向journey,其就是双向链表的尾结点
        Node last = journey;

        //演示,从头到尾遍历链表
        while(true){
            if(first==null){
                break;
            }
            //输出first,
            System.out.println(first);
            first = first.next;
        }
        /**
         * 输出
         * Node name=life
         * Node name=is
         * Node name=a journey
         */
        System.out.println("==========演示链表添加数据==========");
        //演示下链表添加数据/对象是多么方便
        //要求:在 life 和  is 之间添加个 definitely
        //life definitely is a journey
        Node definitely = new Node("definitely");
        definitely.next = is;
        definitely.pre = life;
        life.next = definitely;
        is.pre = definitely;
        //让first指针重新指向头结点life
        first = life;
        while (true){
            if(first==null){
                break;
            }
            System.out.println(first);
            first = first.next;
        }
    }
}
//定义一个Node 类,表示双向链表的一个结点
class Node{
    public Object item;
    public Node next;
    public Node pre;
    public Node(Object name){
        this.item = name;
    }
    public String toString(){
        return "Node name="+item;
    }
}

在这里插入图片描述

LinkedList的增删改查

@SuppressWarnings({"all"})
public class Journey {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        //添加结点,加了0 1 2
        for (int i = 0; i <= 2; i++) {
            list.add(i);
        }

        //演示删除结点
        System.out.println(list);//[0, 1, 2]
        list.remove();//删除了0
        //此时默认删除第一个元素0,输出此时的list空间状态
        System.out.println(list);//[1, 2]
        list.add("kerwin");
        System.out.println(list);//[1, 2, kerwin]
        list.remove(1);//删除1号索引元素,即2
        System.out.println(list);//[1, kerwin]

        //修改某个结点
        list.set(1,"fun");//修改1索引处的元素为fun
        System.out.println(list);//[1, fun]

        //得到某个结点对象
        list.add("Dopamine");
        System.out.println(list.get(2));//Dopamine
        System.out.println(list.getFirst());//1
        System.out.println(list.getLast());//Dopamine

        System.out.println("==================================================");
        //实现遍历--迭代器
        //因为LinkedList是实现了List的接口的,所以也可以使用迭代器遍历
        Iterator iterator = list.iterator();
        //要想起来快捷生成的方式哟, itit
        while(iterator.hasNext()){//这里要联想到迭代器的结构哈,先判断有没有下个元素
            Object obj = iterator.next();
            System.out.println(obj);

        }
        /**
         * 输出
         * 1
         * fun
         * Dopamine
         */

        //实现遍历--增强for
        for(Object o: list){
            System.out.println("els->"+o);
        }
        /**
         * 输出
         * els->1
         * els->fun
         * els->Dopamine
         */

        //实现遍历--最normal的for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println("normal-For->"+list.get(i));
        }
        /**
         *输出
         * normal-For->1
         * normal-For->fun
         * normal-For->Dopamine
         */


    }
}
对应的各个方法的源码剖析
//增方法的源码剖析   add
    
//深入LinkedList的add方法,可知
//传入一个数进去后,先自动装箱,然后,执行add方法,源码如下:
        public boolean add(E e) {
        linkLast(e);
        return true;
    }
//继续深入linkLast方法,可得知其内部具体实现,先了解下Node类的具体内容,再往下看linkLast函数
    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

    void linkLast(E e) {
        final Node<E> l = last;//起初last为null
        final Node<E> newNode = new Node<>(l, e, null);//创建一个新结点,
        //prev为null,element就是此时的元素,next也置为null
        last = newNode;//laast指向新建结点
        if (l == null)
            first = newNode;//如果首结点为空的话,first就指向新节点,使之为首结点
        else
            l.next = newNode;//否则首结点之后才紧跟新建结点
        size++;
        modCount++;
    }


在这里插入图片描述

这时候,你会发现,加了两个元素之后,first指向第一个结点,而last指向第二个结点,这不正合心意??

//删方法的源码剖析   remove
    public E remove() {
        return removeFirst();
    }

    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)//如果为空,还删啥?抛异常lou
            throw new NoSuchElementException();
        return unlinkFirst(f);//不为空,则开始操作
    }

    private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;//将第一个元素置空
        f.next = null; // help GC
        first = next;//first移动到原先头结点的next,也就是移动到了原先的第二个结点处
        if (next == null)//假如原先只有一个结点,那么next==null,
            //也就谈不上断掉下一个结点指向自己的prev了
            last = null;
        else
            next.prev = null;//倘若原先的结点不止一个,
        //那么就需要把第二个结点指向自己的prev也断开
        size--;
        modCount++;
        return element;
    }


在这里插入图片描述

ArrayList和LinkedList比较

在这里插入图片描述

如何选择ArrayList和LinkedList?

  • 如果我们改查的操作多,选择ArrayList
  • 如果我们增删的操作多,选择LinkedList
  • 一般来说,程序中,大部分是查询,因此大多数情况下,会选择ArrayList
  • 在一个项目中,根据业务灵活选择,也可能这样,有可能一个模块用的是ArrayList,另一个模块使用的是LinkedList
[ 33.140132] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [ 33.146123] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [ 33.176267] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [ 33.190118] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [ 34.102442] bond1: (slave enp2s0f0): making interface the new active one [ 34.109200] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [ 34.117858] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [ 34.923468] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [3133671.754531] bond1: (slave enp2s0f1): link status definitely down, disabling slave [3134010.024926] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [5219813.104545] bond1: (slave enp2s0f1): link status definitely down, disabling slave [5220149.604908] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [10618633.524565] bond1: (slave enp2s0f0): link status definitely down, disabling slave [10618633.538863] bond1: (slave enp2s0f1): making interface the new active one [10618968.644957] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [11432487.344557] bond1: (slave enp2s0f1): link status definitely down, disabling slave [11432487.352258] bond1: (slave enp2s0f0): making interface the new active one [11432825.544936] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [18160576.837131] bond1: (slave enp2s0f1): link status definitely down, disabling slave [18160914.354960] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [18160915.804527] bond1: (slave enp2s0f1): link status definitely down, disabling slave [18160915.915000] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [19218278.814549] bond1: (slave enp2s0f0): link status definitely down, disabling slave [19218278.828826] bond1: (slave enp2s0f1): making interface the new active one [19218615.414926] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [19625319.094759] bond1: (slave enp2s0f0): Releasing backup interface [19625319.100915] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [19625320.188941] bond1: (slave enp2s0f1): Releasing backup interface [19625321.284874] bond1 (unregistering): Released all slaves [19625323.031464] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [19625323.037743] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [19625323.045740] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [19625323.062059] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [19625324.467280] bond1: (slave enp2s0f0): making interface the new active one [19625324.474256] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [19625324.483105] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [19625325.398244] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [19735782.404529] bond1: (slave enp2s0f1): link status definitely down, disabling slave [19736120.064946] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [19736121.934529] bond1: (slave enp2s0f1): link status definitely down, disabling slave [19736122.045010] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [21214349.664526] bond1: (slave enp2s0f1): link status definitely down, disabling slave [21214689.274912] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [22809995.026848] bond1: (slave enp2s0f0): link status definitely down, disabling slave [22809995.041127] bond1: (slave enp2s0f1): making interface the new active one [22810331.510866] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [23750455.475825] bond1: (slave enp2s0f0): link status definitely down, disabling slave [23750792.509848] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [31117814.788858] bond1: (slave enp2s0f0): Releasing backup interface [31117814.795014] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [31117815.890500] bond1: (slave enp2s0f1): Releasing backup interface [31117816.975552] bond1 (unregistering): Released all slaves [31117818.682309] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [31117818.688622] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [31117818.696243] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [31117818.747836] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [31117820.181488] bond1: (slave enp2s0f0): making interface the new active one [31117820.188466] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [31117820.197285] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [31117821.091895] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [31360907.052937] bond1: (slave enp2s0f0): link status definitely down, disabling slave [31360907.067209] bond1: (slave enp2s0f1): making interface the new active one [31361242.827164] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [34215503.795582] bond1: (slave enp2s0f1): link status definitely down, disabling slave [34215503.803291] bond1: (slave enp2s0f0): making interface the new active one [34215841.419769] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [36438431.210973] bond1: (slave enp2s0f1): link status definitely down, disabling slave [36438767.134862] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [45043447.917485] bond1: (slave enp2s0f1): link status definitely down, disabling slave [45043786.701375] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [47047816.659353] bond1: (slave enp2s0f0): link status definitely down, disabling slave [47047816.673637] bond1: (slave enp2s0f1): making interface the new active one [47048154.192799] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [47083991.336403] bond1: (slave enp2s0f0): Releasing backup interface [47083991.342559] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [47083992.380982] bond1: (slave enp2s0f1): Releasing backup interface [47083993.468728] bond1 (unregistering): Released all slaves [47083995.141388] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [47083995.147648] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [47083995.155146] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [47083995.205530] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [47083996.659897] bond1: (slave enp2s0f0): making interface the new active one [47083996.666881] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [47083996.677002] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [47083997.599581] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [51333094.205883] bond1: (slave enp2s0f0): link status definitely down, disabling slave [51333094.220160] bond1: (slave enp2s0f1): making interface the new active one [51333429.949095] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [51333432.038595] bond1: (slave enp2s0f0): link status definitely down, disabling slave [51333432.149166] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [52351551.783364] bond1: (slave enp2s0f0): Releasing backup interface [52351551.789520] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [52351552.834873] bond1: (slave enp2s0f1): Releasing backup interface [52351553.918634] bond1 (unregistering): Released all slaves [52351555.580538] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52351555.586814] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52351555.594586] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52351555.608958] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52351557.015366] bond1: (slave enp2s0f0): making interface the new active one [52351557.022342] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [52351557.032532] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [52351557.986705] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [52662182.373856] bond1: (slave enp2s0f0): link status definitely down, disabling slave [52662182.388126] bond1: (slave enp2s0f1): making interface the new active one [52662518.567239] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [52696895.339928] bond1: (slave enp2s0f0): Releasing backup interface [52696895.346084] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [52696896.430979] bond1: (slave enp2s0f1): Releasing backup interface [52696897.527648] bond1 (unregistering): Released all slaves [52696899.230093] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52696899.236354] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52696899.243999] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52696899.258726] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [52696900.712360] bond1: (slave enp2s0f0): making interface the new active one [52696900.719338] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [52696900.728122] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [52696901.643658] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [55959906.885164] bond1: (slave enp2s0f1): link status definitely down, disabling slave [55960244.497872] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [58177716.229197] bond1: (slave enp2s0f0): link status definitely down, disabling slave [58177716.243481] bond1: (slave enp2s0f1): making interface the new active one [58178053.872688] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [58178055.212218] bond1: (slave enp2s0f0): link status definitely down, disabling slave [58178055.322624] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [58659303.733177] bond1: (slave enp2s0f0): Releasing backup interface [58659303.739333] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [58659304.773714] bond1: (slave enp2s0f1): Releasing backup interface [58659305.848994] bond1 (unregistering): Released all slaves [58659307.547241] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [58659307.553522] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [58659307.561147] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [58659307.590712] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [58659309.015706] bond1: (slave enp2s0f0): making interface the new active one [58659309.022688] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [58659309.032812] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [58659309.927476] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [60355234.023173] bond1: (slave enp2s0f1): link status definitely down, disabling slave [60355571.537030] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [60700816.664795] bond1: (slave enp2s0f1): link status definitely down, disabling slave [60701154.448311] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [65658954.578896] bond1: (slave enp2s0f0): link status definitely down, disabling slave [65658954.593173] bond1: (slave enp2s0f1): making interface the new active one [65659290.502356] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [66348713.394669] bond1: (slave enp2s0f0): Releasing backup interface [66348713.400825] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [66348714.420380] bond1: (slave enp2s0f1): Releasing backup interface [66348715.550502] bond1 (unregistering): Released all slaves [66348717.278447] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [66348717.284720] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [66348717.292478] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [66348717.342606] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [66348718.736752] bond1: (slave enp2s0f0): making interface the new active one [66348718.743730] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [66348718.753928] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready [66348719.658746] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [70676902.917739] bond1: (slave enp2s0f1): link status definitely down, disabling slave [70677242.601056] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [70677243.920526] bond1: (slave enp2s0f1): link status definitely down, disabling slave [70677244.030971] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [73171530.471264] bond1: (slave enp2s0f0): link status definitely down, disabling slave [73171530.485543] bond1: (slave enp2s0f1): making interface the new active one [73171868.154692] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [73257145.596177] bond1: (slave enp2s0f0): Releasing backup interface [73257145.602339] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [73257146.085249] bond1: (slave enp2s0f1): Releasing backup interface [73257147.297788] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [73257147.968258] bond1: (slave enp2s0f0): making interface the new active one [73257147.975229] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [73257148.659556] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link [77550478.226714] bond1: (slave enp2s0f1): link status definitely down, disabling slave [77550814.040174] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [77550816.789716] bond1: (slave enp2s0f1): link status definitely down, disabling slave [77550816.900149] bond1: (slave enp2s0f1): link status definitely up, 10000 Mbps full duplex [80917020.093439] bond1: (slave enp2s0f0): link status definitely down, disabling slave [80917020.107722] bond1: (slave enp2s0f1): making interface the new active one [80917358.096294] bond1: (slave enp2s0f0): link status definitely up, 10000 Mbps full duplex [80943657.922185] bond1: (slave enp2s0f0): Releasing backup interface [80943657.928339] bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts [80943658.406453] bond1: (slave enp2s0f1): Releasing backup interface [80943659.368258] IPv6: ADDRCONF(NETDEV_UP): bond1: link is not ready [80943660.092338] bond1: (slave enp2s0f0): making interface the new active one [80943660.099311] bond1: (slave enp2s0f0): Enslaving as an active interface with an up link [80943660.783585] bond1: (slave enp2s0f1): Enslaving as a backup interface with an up link
最新发布
09-19
<think> 我们正在分析bond1接口的日志,这些日志显示了bonding接口及其从属接口enp2s0f0和enp2s0f1的状态变化。关键问题包括链路频繁断开/恢复以及MAC地址冲突警告。 根据日志,问题主要集中在两点: 1. 从属接口(尤其是enp2s0f0和enp2s0f1)的链路状态频繁变化(link status definitely down/up),导致bonding组切换活动接口。 2. 在多次网络重启(或bonding重新配置)时,出现MAC地址冲突的警告: "bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts" 这表明bond1接口和其从属接口enp2s0f0的MAC地址相同(都是04:e8:92:5a:bb:ec),导致冲突。 因此,问题的主要原因可能是: - 物理链路问题(如网线、交换机端口故障)导致链路频繁中断(表现为link down/up)。 - Bonding配置错误,导致MAC地址冲突,这可能进一步引发网络不稳定。 解决方案: 1. 排除物理链路问题:检查网线、交换机端口状态,以及通过`ethtool`查看网卡错误统计。 2. 修改bonding配置,确保从属接口的MAC地址与bond接口不同。通常,bonding接口会使用其中一个从属接口的MAC地址(默认为第一个从属接口)。如果希望bond接口使用单独的MAC地址,可以在bond接口配置中明确设置MAC地址,或者在从属接口配置中设置不同的MAC地址(注意:修改从属接口的MAC地址可能会影响网络,需谨慎)。 具体配置调整步骤(以Red Hat/CentOS为例): - 编辑bond接口配置文件(如`/etc/sysconfig/network-scripts/ifcfg-bond1`),添加一行: MACADDR=xx:xx:xx:xx:xx:xx # 设置一个与从属接口不同的MAC地址 - 或者,在从属接口的配置文件(如`/etc/sysconfig/network-scripts/ifcfg-enp2s0f0`)中,修改其MAC地址(注意:这需要确保新的MAC地址不会与其他设备冲突): MACADDR=yy:yy:yy:yy:yy:yy - 重启网络服务。 另外,检查bonding的模式(mode)是否适合当前环境。常用的模式有active-backup(主备)和802.3ad(动态链路聚合)。根据日志,这里似乎是active-backup模式(因为有一个active和一个backup)。确认模式配置是否正确。 如何检查当前bonding模式? # cat /sys/class/net/bond1/bonding/mode 建议: 在排除物理链路问题后,重点解决MAC地址冲突问题,因为冲突会导致网络不稳定。同时,确保bonding驱动参数(如miimon、downdelay等)设置合理,以适应网络环境。 排查步骤: 1. 使用`ethtool enp2s0f0`和`ethtool enp2s0f1`检查物理链路状态和错误计数。 2. 检查交换机端口是否有错误计数或端口频繁up/down。 3. 修正bonding配置中的MAC地址冲突。 4. 确认bonding模式和其他参数(如主备接口的优先级)是否合理。 注意:修改配置前备份原始文件。 根据以上分析,提出相关问题。 </think> ### 问题分析与排查建议 根据提供的日志,网络断开的核心问题是 **bonding 接口的 MAC 地址冲突** 和 **物理链路不稳定**。以下是关键发现和排查步骤: #### 1. **核心问题定位** - **MAC 地址冲突(关键故障)**: 日志中多次出现: ``` bond1: (slave enp2s0f0): the permanent HWaddr of slave - 04:e8:92:5a:bb:ec - is still in use by bond ``` 这表明 bond1 接口和其从属接口 enp2s0f0 使用了相同的 MAC 地址(`04:e8:92:5a:bb:ec`),导致地址冲突,触发网络服务重启时链路中断。 - **物理链路不稳定(次要问题)**: 频繁出现 `link status definitely down/up`(如 enp2s0f0 和 enp2s0f1 交替断开),可能是网线松动、交换机端口故障或网卡驱动问题。 #### 2. **排查步骤** ##### (1) 解决 MAC 地址冲突 - **检查 bonding 配置**: ```bash cat /etc/sysconfig/network-scripts/ifcfg-bond1 cat /etc/sysconfig/network-scripts/ifcfg-enp2s0f0 cat /etc/sysconfig/network-scripts/ifcfg-enp2s0f1 ``` **修复方案**: - 在 `ifcfg-bond1` 中显式指定唯一 MAC 地址(例如添加 `MACADDR=00:11:22:33:44:55`)。 - 或在 `ifcfg-enp2s0f0` 中修改从属接口的 MAC 地址(例如添加 `MACADDR=00:11:22:33:44:56`)。 ##### (2) 诊断物理链路问题 - **检查网卡错误计数**: ```bash ethtool -S enp2s0f0 | grep error # 查看发包/收包错误 ethtool enp2s0f0 # 检查 "Link detected" 状态 ``` 若错误计数高(如 `rx_crc_errors`),需更换网线或检查交换机端口。 - **监控链路状态**: ```bash watch -n 1 'cat /proc/net/bonding/bond1' # 实时查看 bonding 状态 ``` 关注 `Slave Interface` 的 `Link Failure Count`。 ##### (3) 检查系统日志 - **过滤关键事件**: ```bash journalctl -k --since "2023-01-01" | grep -e "bond1" -e "enp2s0" ``` 确认 `link down` 事件是否与物理操作(如网线插拔)相关。 #### 3. **解决方案** 1. **紧急恢复**: ```bash ifdown bond1 && ifup bond1 # 重启 bond 接口 ``` 2. **永久修复**: - 修改配置文件解决 MAC 冲突(步骤见上)。 - 若链路问题持续,更换网线或联系交换机管理员检查端口日志。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值