1. 编程
1.1 Java
1.1.1 线程池
线程池 7大配置
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler){
}
- corePoolSise: 要保留在池中的线程数,即使它们处于空闲状态,除非 allowCoreThreadTimeOut 已设置
- maximumPoolSize 池中允许的最大线程数
- keepAliveTime 当线程数大于核心数时,这是多余的空闲线程在终止之前等待新任务的最长时间。
- unit 参数的时间 keepAliveTime 单位
- workQueue 用于在执行任务之前保留任务的队列。此队列将仅 Runnable 保存该方法提交 execute 的任务
- threadFactory 执行器创建新线程时使用的工厂
- handler 由于达到线程边界和队列容量而被阻止执行时要使用的处理程序
线程池执行的流程
- 线程池创建,准备好core数量的核心,准备接受任务
- 新的任务进来,用core准备好的空闲线程执行
1)core满了,就将再进来的任务放入到阻塞队列中,空闲的core就会自己去阻塞队列获取任务并执行
2)阻塞队列满了,就直接开新线程执行,最大只能开到指定的数量
3)max 都执行好了 max-core数量空闲的线程会在keepAliveTime指定的时间后自动销毁。最终保持到core大小
3)如果线程数开到了max的数量,还有新任务进来,就会使用reject指定的拒绝策略进行处理 - 所有线程穿件都是由指定的factory创建的
常见的线程池
- newCachedThreadPool: 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则创建线程
- newFixedThreadPool: 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待
- newScheduledThreadPool: 创建一个定长线程池,支持定时及周期性任务执行
- newSingleThreadExecutor: 创建一个单线程的线程池
1.1.2 CompletableFuture
- 创建异步对象
CompletableFuture 提供了四个静态方法来创建一个异步操作
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable,
Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
Executor executor)
备注
- runXxx是没有返回结果的,supply都是可以获取返回结果的
- 可以传入自定义的线程池,否则就用默认的线程池
1.1.3 实际应用
将List分片处理
2. 分布式
2.1 Spring Cloud Alibaba以及feignClient
@FeignCliet
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
/**
* The name of the service with optional protocol prefix. Synonym for {@link #name()
* name}. A name must be specified for all clients, whether or not a url is provided.
* Can be specified as property key, eg: ${propertyKey}.
* @return the name of the service with optional protocol prefix
*/
@AliasFor("name")
String value() default "";
/**
* The service id with optional protocol prefix. Synonym for {@link #value() value}.
* @deprecated use {@link #name() name} instead
* @return the service id with optional protocol prefix
*/
@Deprecated
String serviceId() default "";
/**
* This will be used as the bean name instead of name if present, but will not be used
* as a service id.
* @return bean name instead of name if present
*/
String contextId() default "";
/**
* @return The service id with optional protocol prefix. Synonym for {@link #value()
* value}.
*/
@AliasFor("value")
String name() default "";
/**
* @return the <code>@Qualifier</code> value for the feign client.
*/
String qualifier() default "";
/**
* @return an absolute URL or resolvable hostname (the protocol is optional).
*/
String url() default "";
/**
* @return whether 404s should be decoded instead of throwing FeignExceptions
*/
boolean decode404() default false;
/**
* A custom <code>@Configuration</code> for the feign client. Can contain override
* <code>@Bean</code> definition for the pieces that make up the client, for instance
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
*
* @see FeignClientsConfiguration for the defaults
* @return list of configurations for feign client
*/
Class<?>[] configuration() default {};
/**
* Fallback class for the specified Feign client interface. The fallback class must
* implement the interface annotated by this annotation and be a valid spring bean.
* @return fallback class for the specified Feign client interface
*/
Class<?> fallback() default void.class;
/**
* Define a fallback factory for the specified Feign client interface. The fallback
* factory must produce instances of fallback classes that implement the interface
* annotated by {@link FeignClient}. The fallback factory must be a valid spring bean.
*
* @see feign.hystrix.FallbackFactory for details.
* @return fallback factory for the specified Feign client interface
*/
Class<?> fallbackFactory() default void.class;
/**
* @return path prefix to be used by all method-level mappings. Can be used with or
* without <code>@RibbonClient</code>.
*/
String path() default "";
/**
* @return whether to mark the feign proxy as a primary bean. Defaults to true.
*/
boolean primary() default true;
}
其中path、contextId、name、value是常见的配置项
path: 目的服务的context-path
context-Id 唯一性标识,当name一样 比如全部指向网关的时候 指定context-id feiClient才不会冲突
name: 目的服务,可以直接指向网关
使用FeignClient加上Spring Cloud Gateway的方式,相当于是外部请求走网关,内部请求直接走向服务本身
2.2 社交登录流程(以微博登录举例)
- 申请web社交登录api
- 填写重定向url