文章目錄
JS跨域請求解決方案-CORS
1.JS跨域請求
這裏說的js跨域是指通過js在不同的域之間進行數據傳輸或通信,比如用ajax向一個不同的域請求數據,或者通過js獲取頁面中不同域的框架中(iframe)的數據。只要協議、域名、端口有任何一個不同,都被當作是不同的域。
測試JavaScript的跨域請求(失敗)
- 出現了以下錯誤
2. 跨域解決方案CORS
CORS是一個W3C標準,全稱是"跨域資源共享"(Cross-origin resource sharing)。CORS需要瀏覽器和服務器同時支持。目前,所有瀏覽器都支持該功能,IE瀏覽器不能低於IE10。
它允許瀏覽器向跨源服務器,發出XMLHttpRequest請求,從而克服了AJAX只能同源使用的限制。整個CORS通信過程,都是瀏覽器自動完成,不需要用戶參與。對於開發者來說,CORS通信與同源的AJAX通信沒有差別,代碼完全一樣。瀏覽器一旦發現AJAX請求跨源,就會自動添加一些附加的頭信息,有時還會多出一次附加的請求,但用戶不會有感覺。因此,實現CORS通信的關鍵是服務器。只要服務器實現了CORS接口,就可以跨源通信。
普通實現方法
- 在請求的服務端的方法中加兩個頭信息
response.setHeader("Access-Control-Allow-Origin", "http://localhost:9105");
response.setHeader("Access-Control-Allow-Credentials", "true");
- 客戶端
springMVC的跨域請求註解
-
服務端
-
客戶端:與普通方法一致
3.分佈式Id解決方案
概述
當在互聯網應用中某一個表太大時,會採用分佈式數據庫進行存儲,在物理上是對各數據庫多張表,在邏輯上是一個數據庫,一張表,但是如果數據庫的主鍵Id使用自增的方式,可能會長生重複的Id,這樣在使用數據庫時就會報錯
- UUID(不用他的理由:太長,無法進行排序)
- redis(redis會產生一個自增的序號,作爲數據庫中的主鍵:主鍵的生成需要訪問redis,對Redis有依賴)
- Oracle(數據庫對象-序列(與表無關):只有數據庫用Oracle才能用)
- 程序自己來寫算法
分佈式ID生成器
我們採用的是開源的twitter( 非官方中文慣稱:推特.是國外的一個網站,是一個社交網絡及微博客服務) 的snowflake(雪花)算法。
可以在一秒鐘內生成26萬不重複Id
- 代碼
package util;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
* <p>名稱:IdWorker.java</p>
* <p>描述:分佈式自增長ID</p>
* <pre>
* Twitter的 Snowflake JAVA實現方案
* </pre>
* 核心代碼爲其IdWorker這個類實現,其原理結構如下,我分別用一個0表示一位,用—分割開部分的作用:
* 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
* 在上面的字符串中,第一位爲未使用(實際上也可作爲long的符號位),接下來的41位爲毫秒級時間,
* 然後5位datacenter標識位,5位機器ID(並不算標識符,實際是爲線程標識),
* 然後12位該毫秒內的當前毫秒內的計數,加起來剛好64位,爲一個Long型。
* 這樣的好處是,整體上按照時間自增排序,並且整個分佈式系統內不會產生ID碰撞(由datacenter和機器ID作區分),
* 並且效率較高,經測試,snowflake每秒能夠產生26萬ID左右,完全滿足需要。
* <p>
* 64位ID (42(毫秒)+5(機器ID)+5(業務編碼)+12(重複累加))
*
* @author Polim
*/
public class IdWorker {
// 時間起始標記點,作爲基準,一般取系統的最近時間(一旦確定不能變動)
private final static long twepoch = 1288834974657L;
// 機器標識位數
private final static long workerIdBits = 5L;
// 數據中心標識位數
private final static long datacenterIdBits = 5L;
// 機器ID最大值
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
// 數據中心ID最大值
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
// 毫秒內自增位
private final static long sequenceBits = 12L;
// 機器ID偏左移12位
private final static long workerIdShift = sequenceBits;
// 數據中心ID左移17位
private final static long datacenterIdShift = sequenceBits + workerIdBits;
// 時間毫秒左移22位
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
/* 上次生產id時間戳 */
private static long lastTimestamp = -1L;
// 0,併發控制
private long sequence = 0L;
private final long workerId;
// 數據標識id部分
private final long datacenterId;
public IdWorker(){
this.datacenterId = getDatacenterId(maxDatacenterId);
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
}
/**
* @param workerId
* 工作機器ID
* @param datacenterId
* 序列號
*/
public IdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* 獲取下一個ID
*
* @return
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
// 當前毫秒內,則+1
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 當前毫秒內計數滿了,則等待下一秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
// ID偏移組合生成最終的ID,並返回ID
long nextId = ((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;
return nextId;
}
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
/**
* <p>
* 獲取 maxWorkerId
* </p>
*/
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuffer mpid = new StringBuffer();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (!name.isEmpty()) {
/*
* GET jvmPid
*/
mpid.append(name.split("@")[0]);
}
/*
* MAC + PID 的 hashcode 獲取16個低位
*/
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
}
/**
* <p>
* 數據標識id部分
* </p>
*/
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
System.out.println(" getDatacenterId: " + e.getMessage());
}
return id;
}
}
- . 在spring中配置上,注入兩個值
- 在了中直接注入實用就行