ARTS 第34周:搜索插入位置算法、网关聚合模式、Spring Cloud Gateway与云服务设计模式

#coding#arts
目录

Algorithm

package org.nocoder.leetcode.solution;

/**
 * 35. Search Insert Position
 * <p>
 * Given a sorted array and a target value, return the index if the target is found.
 * <p>
 * If not, return the index where it would be if it were inserted in order.
 * <p>
 * You may assume no duplicates in the array.
 * <p>
 * Example 1:
 * <p>
 * Input: [1,3,5,6], 5
 * Output: 2
 * Example 2:
 * <p>
 * Input: [1,3,5,6], 2
 * Output: 1
 * Example 3:
 * <p>
 * Input: [1,3,5,6], 7
 * Output: 4
 * Example 4:
 * <p>
 * Input: [1,3,5,6], 0
 * Output: 0
 *
 * @author jason
 * @date 2019/3/23.
 */
public class SearchInsertPosition {

    public static void main(String[] args) {
        int[] nums = new int[]{1, 3, 5, 6};
        int t1 = 4;
        System.out.println(searchInsert(nums, t1));
        int t2 = 2;
        System.out.println(searchInsert(nums, t2));
        int t3 = 7;
        System.out.println(searchInsert(nums, t3));
        int t4 = 0;
        System.out.println(searchInsert(nums, t4));
    }

    /**
     * 使用二分法,查找目标数的位置,
     * 如果在数组中找到了目标数,则返回目标数的下标;
     * 如果找不到目标数,则返回最后一次循环结束后的开始位置(即 start 的值)
     * @param nums
     * @param target
     * @return
     */
    public static int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length-1;
        while(start <= end){
            int middle = (start + end ) / 2;
            System.out.println("middle: "+middle);
            if(nums[middle] > target){
                end = middle - 1;
            }else if(nums[middle] < target){
                start = middle + 1;
            }else{
                return middle;
            }
        }
        return start;
    }
}

Review

Gateway Aggregation pattern

Tip

Spring Cloud Gateway 的使用

Spring Cloud Gateway 的功能

package gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@SpringBootApplication
@RestController
@EnableConfigurationProperties(Application.UriConfiguration.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    /**
     * 访问超时后的后备方案fallback
     * @return
     */
    @RequestMapping("/fallback")
    public Mono<String> fallback(){
        System.out.println("fallback...");
        return Mono.just("fallback");
    }

    /**
     * RouteLocator 用于创建路由
     * RouteLocatorBuilder 允许在路由中添加 predicate 和 filter,以便根据特定条件路由
     * UriConfiguration 从配置中获取URL
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder, UriConfiguration uriConfiguration){
        String httpUri = uriConfiguration.getHttpbin();
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                /**
                 * 使用curl 发起请求,header 中包含 Host: *.hystrix.com,否则请求不会被路由
                 * $ curl --dump-header - --header 'Host: www.hystrix.com' http://localhost:8080/delay/3
                 * 注:使用 --dump-header 查看响应的headers
                 * --dump-heaer 后面的 - 是为了告知 cURL 将headers 打印出来
                 */
                .route(p -> p
                        .host("*.hystrix.com")
                        .filters(f -> f.hystrix(config -> config
                                .setName("mycmd")
                                .setFallbackUri("forward:/fallback")))
                        .uri(httpUri))
                .build();
    }

    @ConfigurationProperties
    class UriConfiguration {
        private String httpbin = "http://httpbin.org:80";

        public String getHttpbin() {
            return httpbin;
        }

        public void setHttpbin(String httpbin) {
            this.httpbin = httpbin;
        }
    }
}

Share

Cloud Design Patterns

微软的文章,介绍了34种云服务的设计模式。


评论区