Plusaber's Blog

  • Home

  • Tags

  • Categories

  • Archives

Useful Materials

Posted on 2014-12-10 | In System design | Comments:

Useful Materials

Scalable Web Architecture and Distributed Systems
An Unorthodox Approach To Database Design : The Coming Of The Shard
可扩展Web架构与分布式系统
可扩展性设计之数据切分
可扩展性设计之Cache与Search的利用
可扩展架构设计的三个维度
可伸缩性/可扩展性(Scalable/scalability)
可伸缩性最佳实战

internet笔记
hadoop笔记本
TCP/IP笔记本

MySQL Replication 常用架构

System Design总结

Posted on 2014-12-10 | In System design | Comments:

System Design总结[from mitbbs]

最近在准备系统设计方面的内容,找到一篇不错的文章,转自我的System Design总结,结构有少许调整。

入门

System Design和OO Design不同 System Design在FLAG以及很多大公司中主要
是design scalable distributed systems 这里只讨论如何准备这种题目。

对于0基础的同学们 下面的资料可以按顺序开始看:

  1. http://www.hiredintech.com/app#system-design
    这是一个专门准备面试的网站 你只用关心system design部分 有很多的link后面会重
    复提到 建议看完至少一遍

  2. https://www.youtube.com/watch?v=-W9F__D3oY4
    非常非常好的入门资料 建议看3遍以上!
    这是1里面提到的资料 是Harvard web app课的最后一节 讲scalability 里面会讲到很
    多基础概念比如Vertical scaling, Horizontal scaling, Caching, Load balancing,
    Database replication, Database partitioning 还会提到很多基本思想比如avoid
    single point of failure
    再强调一遍 非常好的资料!

  3. http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones
    前面课程里提到的 Scalability for Dummies 还算不错 可以看一遍 知道基本思想

结束语:当你结束这一部分的学习的时候 你已经比50%的candidate知道的多了(因为很
多人都不准备 或者不知道怎么准备system design) 恭喜:)

进阶

这一部分的资料更加零散 每个看的可能不一样 但是你每多看一篇文章或者一个视频
你就比别人强一点
这部分你会遇到很多新名词 我的建议是每当你遇到一个不懂的概念时 多google一下
看看这个概念或者技术是什么意思 优点和缺点各是什么 什么时候用 这些你都知道以
后 你就可以把他运用到面试中 让面试官刮目相看了

  1. http://highscalability.com/blog/2009/8/6/an-unorthodox-approach-to-database-design-the-coming-of-the.html
    Database Sharding是一个很重要的概念 建议看一看

  2. http://highscalability.com/all-time-favorites/
    这个里面会讲到很多非常流行的网站架构是如何实现的 比如Twitter, Youtube,
    Pinterest, Google等等 我的建议是看5-6个 然后你应该已经建立起了一些基本的意识
    还有知道了某些技术和产品的作用和mapping 比如说到cache你会想到memcached和
    Redis 说到
    load balancer你会想到 Amazon ELB, F5一类的

  3. http://www.infoq.com/
    5里面很多的文章都会有链接 其中有很多会指向这个网站 这里面有很多的tech talk
    很不错 可以看看

  4. https://www.facebook.com/Engineering/notes
    Facebook非常好的技术日志 会讲很多facebook的feature怎么实现的 比如facebook
    message:https://www.facebook.com/notes/facebook-engineering/the-underlying-
    technology-of-messages/454991608919 建议看看 尤其是准备面facebook的同学
    这有一个facebook talk讲storage的https://www.youtube.com/watch?v=5RfFhMwRAic

  5. 一些国内网站上的资料
    http://blog.csdn.net/sigh1988/article/details/9790337
    http://blog.csdn.net/v_july_v/article/details/6279498

  6. 最后一些概念很有用 都是我再看这些资料的时候发现的 如果你没有遇到或者查过
    建议查查
    Distributed Hash Table
    Eventual Consistency vs Strong Consistency
    Read Heavy vs Write Heavy
    Consistent Hashing
    Sticky Sessions
    Structured Data(uses DynamoDB) vs Unstructured Data(uses S3)
    http://smartdatacollective.com/michelenemschoff/206391/quick-guide-structured-and-unstructured-data
    http://stackoverflow.com/questions/18678315/amazon-s3-or-dynamodb

  7. 给有兴趣深入研究的人看的
    Mining Massive Datasets —讲很多big data和data mining的东西
    Big Data: Principles and best practices of scalable realtime data systems —
    twitter的前员工讲述如何处理实时数据

  8. 凌乱的资料 随便看看吧
    http://highscalability.com/blog/2013/10/28/design-decisions-for

小结

看多了以后 你的最终目标应该是心里有了一个大框架 一个基本的distributed system
是怎么搭起来的 然后心里有很多if condition 如果要是满足这个条件 我应该用什么
技术 比如如果read heavy那么用cache会提升performance之类的 同时知道应该避免什
么东西 比如避免single point of failure 再比如时间和空间的tradeoff在read
heavy的时候应该倾向于时间 Write heavy的时候倾向于空间等等

你总结出来的和我总结出来的大框架和if conditions肯定不完全一样 但因为system
design本来就是一个open ended question 所以不用害怕 能够自圆其说 就不会有问题。

Tomcat on Mac

Posted on 2014-11-22 | In Developing | Comments:

Tomcat在mac上的安装

  • 安装java并配置好JAVA_HOME
  • 下载Tomcat并验证md5
  • 解压到/Library/下(其他目录也可以),命名为Tomcat
  • 修改文件执行权限,sudo chmod 777 Tomcat
  • 启动Tomcat,执行sh Tomcat/bin/startup.sh
  • 打开http://localhost:8080确认服务是否正常启动。

上面的配置完成后,我们便可以执行Tomcat安装目录下bin目录中的命令进行启动和停止,但是有些麻烦,我们可以配置一个脚本文件用于控制Tomcat的启动停止。

在/usr/local/bin目录(任何PATH下的目录都可以)下创建文件tomcat,写入如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
case $1 in
start)
sh /Library/Tomcat/bin/startup.sh
;;
stop)
sh /Library/Tomcat/bin/shutdown.sh
;;
restart)
sh /Library/Tomcat/bin/shutdown.sh
sh /Library/Tomcat/bin/startup.sh
;;
*)
echo “Usage: start|stop|restart”
;;
esac
exit 0

修改执行权限chmod 777 tomcat。

新建一个concole,我们可以通过下面命令控制Tomcat的运行,如果需要更高级的控制,在tomcat脚本文件添加相关内容即可:

1
2
3
tomcat start  # 启动
tomcat stop # 停止
tomcat restart # 重启

Tomcat的配置

Tomcat vs Apache

Apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展。Apache和Tomcat都可以做为独立的web服务器来运行,但是Apache不能直接解释java程序(jsp,serverlet,php)。需要通过插件才能解释。Apache是普通服务器,本身只支持html即普通网页。不过可以通过插件支持php,还可以与Tomcat连通(单向Apache连接Tomcat,就是说通过Apache可以访问Tomcat资源。反之不然)两者都是一种容器,只不过发布的东西不同:Apache是html容器,功能像IIS一样;Tomcat是jsp/servlet容器,用于发布jsp及java的,类似的有IBM的webshere、EBA的Weblogic,sun的JRun等等。Apache和Tomcat是独立的,在通一台服务器上可以集成。打个比方:Apache是一辆卡车,上面可以装一些东西如html等。但是不能装水,要装水必须要有容器(桶),Tomcat就是一个桶(装像Java这样的水),而这个桶也可以不放在卡车上。Apache只支持静态网页,但像asp,php,cgi,jsp等动态网页就需要Tomcat来处理。Apache和Tomcat整合使用:如果客户端请求的是静态页面,则只需要Apache服务器响应请求;如果客户端请求动态页面,则是Tomcat服务器响应请求;因为jsp是服务器端解释代码的,这样整合就可以减少Tomcat的服务开销 。

Decorator pattern

Posted on 2014-11-22 | In Design Pattern | Comments:

假设我们要设计一个咖啡厅的订单系统,咖啡厅有四种类型的咖啡(饮料类型,浓度等区分,与配料无关,每种咖啡可以自由加入任何数量任何种类的配料),以及多种配料。我们需要实现的是根据客户订单,计算具体咖啡的价钱。

Several Method

使用继承

Design_pattern_decorator_1

Design_pattern_decorator_2

当对于每种具体的咖啡建立一个类时,类数量会迅速增长。配料价格的变化将会需要改变所有这些具体类。

改进的一种方法是,不再对于每个具体咖啡建立类,而是设置为配料变量,这样对于具体咖啡可以通过设置具体的变量实现。仍然存在的问题是,当调料的价格变化时,出现新的调料时等情况,我们仍然需要修改现有代码。

装饰器模式

在策略模式中,我们其实已经尝试实现这个重要的原则:开闭原则。
类应该对扩展开放,对修改关闭。

需要注意的是,我们并不需要也不可能将每个部分实现为满足开闭原则,而只是对设计中最有可能改变的地方应用开闭原则。

上面的例子已经显式,利用继承无法完全解决问题:类数量爆炸、设计死板,以及基类加入的新功能并不适用于所有的子类。

这里的问题我们可以使用装饰器模式:以饮料为主体,然后在运行时以调料在装饰饮料。比如说如果顾客想要摩卡加牛奶深焙咖啡,需要做的是:

  • 建立一个深焙咖啡对象
  • 以摩卡对象装饰上一个对象,建立摩卡对象
  • 以牛奶(Whip)装饰上一个对象
  • 调用cost()方法,并依赖委托上一个对象将前面的价格加上去。

Design_pattern_decorator_3

Design_pattern_decorator_4

Design_pattern_decorator_5

装饰器模式

装饰器模式动态的将责任附加到对象上。如果要扩展功能,装饰者提供了比继承更有弹性的替代方案。装饰者可以在被委托的被装饰者的行为之前/之后,加上自己行为,以达到特定的目的。

另外,需要说明的几点是:

  • 装饰者和被装饰对象有相同的超类型。
  • 可以使用一个或多个装饰者包装一个对象。
  • 装饰者和被装饰对象具有相同的父类,也就是装饰者可以进一步被其他装饰者包装,并且这一过程不限数量。

Design_pattern_decorator_6

使用装饰器实现咖啡订单系统:

Design_pattern_decorator_7

1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class Beverage {
String description = "Unknown Beverage";

public String getDescription() {
return description;
}

public abstract double cost();
}

public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Espresso extends Beverage {

public Espresso() {
description = "Espresso";
}

public double cost() {
return 1.99;
}
}

public class Decaf extends Beverage {
public Decaf() {
description = "Decaf Coffee";
}

public double cost() {
return 1.05;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Mocha extends CondimentDecorator {
Beverage beverage;

public Mocha(Beverage beverage) {
this.beverage = beverage;
}

public String getDescription() {
return beverage.getDescription() + ", Mocha";
}

public double cost() {
return .20 + beverage.cost();
}
}

public class Milk extends CondimentDecorator {
Beverage beverage;

public Milk(Beverage beverage) {
this.beverage = beverage;
}

public String getDescription() {
return beverage.getDescription() + ", Milk";
}

public double cost() {
return .10 + beverage.cost();
}
}

使用装饰器模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class StarbuzzCoffee {

public static void main(String args[]) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription()
+ " $" + beverage.cost());

Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
System.out.println(beverage2.getDescription()
+ " $" + beverage2.cost());

Beverage beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
System.out.println(beverage3.getDescription()
+ " $" + beverage3.cost());
}
}

Java I/O中很多类即采用了装饰器模型,将功能结合起来,实现更强大的读取数据或其他操作。

Design_pattern_decorator_8

Maven(2)_创建第一个Maven项目

Posted on 2014-11-05 | In java | Comments:

参考Maven Tutorial,Maven - 创建项目

Installing Maven

To install Maven on your own system (computer), go to the Maven download page and follow the instructions there. In summary, what you need to do is:

  1. Set the JAVA_HOME environment variable to point to a valid Java SDK (e.g. Java 8).
  2. Download and unzip Maven.
  3. Set the M2_HOME environment variable to point to the directory you unzipped Maven to.
  4. Set the M2 environment variable to point to M2_HOME/bin (%M2_HOME%\bin on Windows, $M2_HOME/bin on unix).
  5. Add M2 to the PATH environment variable (%M2% on Windows, $M2 on unix).
  6. Open a command prompt and type ‘mvn -version’ (without quotes) and press enter.

Manual project creation

创建项目目录和pom文件

创建pom.xml文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>com.jenkov</groupId>

<artifactId>hello-world</artifactId>

<version>1.0.0</version>

</project>

可以通过mvn clean测试pom.xml以及环境是否正常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pc-a375-2:maven admin$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.pom (4 KB at 1.9 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/16/spice-parent-16.pom
Downloaded: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/16/spice-parent-16.pom (9 KB at 31.8 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/sonatype/forge/forge-parent/5/forge-parent-5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/sonatype/forge/forge-parent/5/forge-parent-5.pom (9 KB at 31.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.6/maven-plugin-api-2.0.6.jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.6/maven-plugin-api-2.0.6.jar (13 KB at 26.2 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (221 KB at 292.8 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.157 s
[INFO] Finished at: 2016-04-12T17:39:37+09:00
[INFO] Final Memory: 7M/130M
[INFO] ------------------------------------------------------------------------
pc-a375-2:maven admin$

创建源码目录

按照maven的标准目录结构在项目录下创建源码目录:

1
2
3
src
main
java

创建代码文件

创建java类源码文件HelloWorld.java.

1
2
3
4
5
6
7
8
9
10
11
package helloworld;

public class HelloWorld {

public static void main(String args[]){

System.out.println("Hello World, Maven");

}

}

构建项目

运行mvn package, package是默认生命周期的一个阶段。会将编译得到的java class文件打包位一个JAR文件。maven会创建target目录,生成的JAR文件就在里面。JAR包会命名为:
artifactId-version.

1
2
3
4
pc-a375-2:maven admin$ cd target
pc-a375-2:target admin$ ls
classes hello-world-1.0.0.jar maven-archiver maven-status
pc-a375-2:target admin$

项目模板

Archetype 是一个 Maven 插件,它的任务是按照项目模板创建一个项目的目录结构。这里,我们将使用 archetype 插件 quickstart 来创建一个简单的 java 应用。

可以Maven的archetype插件来创建基本的项目,也可以创建高级的项目(根据模板)。要创建一个简单的java应用,我们将使用maven-archetype-quickstart插件。

Maven 将会开始处理,并且要求你选择需要的archetype。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] -------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:generate] (aggregator-style)
[INFO] -------------------------------------------------------------------
[INFO] Preparing archetype:generate
...
600: remote -> org.trailsframework:trails-archetype (-)
601: remote -> org.trailsframework:trails-secure-archetype (-)
602: remote -> org.tynamo:tynamo-archetype (-)
603: remote -> org.wicketstuff.scala:wicket-scala-archetype (-)
604: remote -> org.wicketstuff.scala:wicketstuff-scala-archetype
Basic setup for a project that combines Scala and Wicket,
depending on the Wicket-Scala project.
Includes an example Specs test.)
605: remote -> org.wikbook:wikbook.archetype (-)
606: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-glassfish (-)
607: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-spring (-)
608: remote -> org.xwiki.commons:xwiki-commons-component-archetype
(Make it easy to create a maven project for creating XWiki Components.)
609: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro
(Make it easy to create a maven project for creating XWiki Rendering Macros.)
610: remote -> org.zkoss:zk-archetype-component (The ZK Component archetype)
611: remote -> org.zkoss:zk-archetype-webapp (The ZK wepapp archetype)
612: remote -> ru.circumflex:circumflex-archetype (-)
613: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
614: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
Choose a number or apply filter
(format: [groupId:]artifactId, case sensitive contains): 203:

按回车选择默认的选项(203:maven-archetype-quickstart)。

Maven 将要求提供 archetype 的特定版本。

1
2
3
4
5
6
7
8
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:

按回车选择默认的选项(6:maven-archetype-quickstart:1.1)。

Maven 将要求提供项目详情。要找要求键入项目详情。如果默认值有提供就按回车。你可以键入你自己的值来覆盖这些默认值。

1
2
3
4
5
6
7
8
9
10
11
12
Define value for property 'groupId': : com.companyname.insurance
Define value for property 'artifactId': : health
Define value for property 'version': 1.0-SNAPSHOT:
Define value for property 'package': com.companyname.insurance:
Maven 会要求确认项目详情。按回车或者 Y 。

Confirm properties configuration:
groupId: com.companyname.insurance
artifactId: health
version: 1.0-SNAPSHOT
package: com.companyname.insurance
Y:

现在 Maven 将开始创建项目目录结构并且显示下面的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[INFO] -----------------------------------------------------------------------
[INFO] Using following parameters for creating project
from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] -----------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.companyname.insurance
[INFO] Parameter: packageName, Value: com.companyname.insurance
[INFO] Parameter: package, Value: com.companyname.insurance
[INFO] Parameter: artifactId, Value: health
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\health
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------------------------
[INFO] Total time: 4 minutes 12 seconds
[INFO] Finished at: Fri Jul 13 11:10:12 IST 2012
[INFO] Final Memory: 20M/90M
[INFO] -----------------------------------------------------------------------

Maven使用如下所示的一套标准目录结构:

目录结构 描述
src/main/java 包含包下的Java源代码文件。
src/main/test 包含包下的测试Java源代码文件。
src/main/resources 包含图片和properties文件(在上面的例子中,我们需要手动创建此目录)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Maven 为项目生成了一个 POM.xml 文件,如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.insurance</groupId>
<artifactId>health</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>health</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Maven还创建了一个Java源文件样例和Java测试类文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.companyname.insurance;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

开发者需要按照上面表格提到的要求放置自己的文件,然后Maven会处理构建相关的所有复杂工作。

在 Maven 中使用一条命令,你就可以创建任意种类的项目,并且可以迅速启动你的开发工作。

1…111213…17
Plusaber

Plusaber

Plusaber's Blog
82 posts
12 categories
22 tags
Links
  • LinkedIn
  • Indeed
  • Baito
  • Kaggle
© 2014 – 2019 Plusaber
Powered by Hexo v3.8.0
|
Theme – NexT.Mist v7.1.1