Strategy

这里写图片描述


public interface Comparable {
    int compareTo(Object o);
}
----------


public interface Comparator {
    int compare(Object o1,Object o2);
}


----------
public class Arrays {
    public static void sort(Comparable[] c){
        Comparable temp;
        for(int i=0;i<c.length;i++){
            for(int j=i+1;j<c.length;j++){
                if(c[i].compareTo(c[j])>0){
                    temp = c[i];
                    c[i] = c[j];
                    c[j] = temp;
                }
            }

        }       
    }
}


----------
public class Cat implements Comparable{
    private int weight;
    private int height;


    public Cat(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    /*未使用Comparator接口
     * @Override
    public int compareTo(Object o) {
        if(o instanceof Cat ){
            Cat c = (Cat)o;
            if(this.height > c.getHeight())
                return 1;
            if(this.height == c.getHeight())
                return 0;
            if(this.height < c.getHeight())
                return -1;
        }
        return -10;
    }*/
    //使用Comparator接口
    @Override
    public int compareTo(Object o) {
        return new HeightComparator().compare(this, o);
    }
    @Override
    public String toString() {
        return "("+this.height+"|"+this.weight+")";
    }
}


----------
class HeightComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Cat c1=(Cat)o1;
        Cat c2=(Cat)o2;
        if(c1.getHeight() > c2.getHeight())
            return 1;
        if(c1.getHeight() == c2.getHeight())
            return 0;
        if(c1.getHeight() < c2.getHeight())
            return -1;
        return -10;
    }
}


----------
import org.junit.Test;
public class ArraysTest {
 
    @Test
    public void testSort() {
        Cat[] cats = {new Cat(1,1),new Cat(3,3),new Cat(2,2)};
        Arrays.sort(cats);
        for(int i=0;i<cats.length;i++){          
            System.out.print(cats[i]);
        }
    }
}
### 如何配置 UpdateStrategy 在 Kubernetes 中 在 Kubernetes 的 `StatefulSet` 或者 `DaemonSet` 资源中可以指定 `updateStrategy` 来控制更新的方式。对于 `Deployment` 类型,则使用 `strategy` 字段。 #### Deployment 更新策略 对于 `Deployment`,有两种主要类型的更新策略: - **Recreate**: 这种方式会在新版本的 Pod 创建之前终止所有的现有 Pod 实例[^1]。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest strategy: type: Recreate ``` - **RollingUpdate**: 默认情况下使用的滚动更新模式会逐步替换老版本的 Pods,而不是一次性全部销毁再重新创建。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 25% maxSurge: 25% ``` 上述例子展示了如何通过 YAML 文件来设定不同的更新策略。其中 `maxUnavailable` 和 `maxSurge` 参数用于更细粒度地调整滚动更新的行为,前者指定了允许的最大不可用实例比例,后者则设定了额外可超出期望副本数目的最大数量。 #### StatefulSet 和 DaemonSet 的更新策略 除了 `Deployment` 外,`StatefulSet` 和 `DaemonSet` 同样支持类似的更新机制,但是具体行为有所不同。例如,在 `StatefulSet` 上可以通过设置 `.spec.updateStrategy.type=OnDelete` 让用户手动删除旧版 Pod 才触发更新;而`.spec.updateStrategy.type=RollingUpdate` 则实现了自动化的滚动升级逻辑。 ```yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 updateStrategy: type: RollingUpdate ... ``` 同样适用于 `DaemonSet`: ```yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: fluentd-elasticsearch spec: updateStrategy: type: RollingUpdate ... ``` 这些配置使得管理员能够灵活应对不同场景下应用程序和服务的需求变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值