liguofeng29’s blog

個人勉強用ブログだっす。

備忘

git

  • 実行権限追加
git update-index --add --chmod=+x filename.sh
  • 指定ファイルを戻す
git checkout [コミット番号] [ファイルパス]
git checkout HEAD test_file.txt

maven

  • maven wrapper追加 mvn -N io.takari:maven:wrapper -Dmaven=3.3.9

javascript

  • javasriptでJqueryライブラリ追加
if (!window.jQuery) {
    document.body.appendChild((function () {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
        return s;
    })());
}

linux

  • E437: terminal capability “cm” required (vim)
# bashrcに下記を追加して下さい。
export TERM=vt100

java

  • java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 avassistのバージョンを更新する

git - branch,tag

参考

リモートリポジトリ

pull : fetch + merge

fetch

  • リモートの最新履歴取得
  • FETCH_HEAD

push : リモートリポジトリへ変更を反映

ブランチ操作

初期処理

$ echo "リポジトリ初期化"
$ mkdir repo
$ cd repo
$ git init
  
$ echo "ファイル作成&コミット"
$ echo "first commit" > myfile.txt
$ git add myfile
$ git status
On branch master
  
Initial commit
  
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
  
        new file:   myfile.txt
         
$ git commit -m "first commit"
[master (root-commit) 2add8a7] first commit
  
$ git status
On branch master
nothing to commit, working directory clean

ブランチ作成&切り替え

$ echo "ブランチ作成"
$ git branch issue1
  
$ echo "ブランチ確認"
$ git branch
  issue1
* master
  
$ git checkout issue1
Switched to branch 'issue1'
  
$ git branch
* issue1
  master
  
echo "-bでブランチ作成&切り替え"
echo "git checkout -b branch_name"

$ echo "ブランチを変更してコミット"
$ echo "second commit" >> myfile.txt
$ git status
On branch issue1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  
        modified:   myfile.txt
  
no changes added to commit (use "git add" and/or "git commit -a")

$ git add myfile.txt
$ git status
On branch issue1
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   myfile.txt

$ git commit -m "second commit"
[issue1 e91d210] second commit

ブランチマージ

$ echo "masterにissue1をマージする"
$ git checkout master
Switched to branch 'master'
  
$ echo "マージ前の内容である"
$ less myfile.txt
  
$ git merge issue1
Updating 2add8a7..e91d210
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)
  
$ echo "マージ誤の内容である"
$ less myfile.txt

$ echo "マージはfast^forward(早送り)"

ブランチ削除

$ git branch
  issue1
* master
  
$ echo "ブランチ削除は-d branch_name"
$ git branch -d issue1
Deleted branch issue1 (was e91d210).
  
$ git branch
* master

複数ブランチ作業&競合解消

$ echo "複数ブランチ作成"
$ git branch issue2
$ git branch issue3
$ git branch
  issue2
  issue3
* master
  
$ echo "issue2ブランチでファイル修正&コミット"
$ git checkout issue2
Switched to branch 'issue2'
$ echo "add line by issue2" >> myfile.txt
$ git add myfile.txt
$ git commit -m "add line by issue2"
  
$ echo "issue3ブランチでファイル修正&コミット"
$ git checkout issue3
$ echo "add line by issue3" >> myfile.txt
$ git add myfile.txt
$ git commit -m "add line by issue3"

$ echo "masterブランチへissue2, issue3をマージする"
$ git checkout master
Switched to branch 'master'
$ git merge issue2
Updating e91d210..3a5d7a2
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)
  
$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
  
$ echo "競合を手動で解消する"
$ vi myfile.txt
$ git add myfile.txt
$ git commit -m "merge issue3"
[master 0b82438] merge issue3

コミット履歴イメージ:
http://www.backlog.jp/git-guide/img/post/stepup/capture_stepup2_7_2.png

rebaseでマージする

$ echo "masterへのissue3マージを取り消す (issue2をマージした状態)"
$ git reset --hard HEAD~
HEAD is now at 3a5d7a2 add line by issue2
  
$ git checkout issue3
Switched to branch 'issue3'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add line by issue3
Using index info to reconstruct a base tree...
M       myfile.txt
Falling back to patching base and 3-way merge...
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
error: Failed to merge in the changes.
Patch failed at 0001 add line by issue3
The copy of the patch that failed is found in: .git/rebase-apply/patch
  
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
  
$ echo "競合を解消する"
$ vi myfile.txt
  
$ git add myfile.txt
$ git rebase --continue
  
$ echo "masterへissue3をマージする(fast-forword)"
$ git checkout master
Switched to branch 'master'
$ git merge issue3
Updating 3a5d7a2..9038b2e
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)

コミット履歴イメージ:
http://www.backlog.jp/git-guide/img/post/stepup/capture_stepup2_8_2.png

タグ : タグとは、コミットを参照しやすくするために、わかりやすい名前を付けるものです。

  • 軽量タグ
  • 注釈タグ
$ echo "リポジトリ初期化&コミット"
$ git init
$ echo "first line" >> myfile.txt
$ git add myfile.txt
$ git commit -m "first commit"
  
$ echo "軽量タグ作成"
$ git tag first-tag
$ git tag
  
$ echo "タグ情報を含めlog"
$ git log --decorate
  
$ echo "注釈タグ作成"
$ git tag -am "注釈付きタグ追加" commet-tag
$ git tag -n

Docker&DockerHubチュートリアル

参考

イメージ

f:id:liguofeng29:20170801102902p:plain

重要な用語

コンテナ

Dockerイメージから作られ、実行される仮想環境。

Dockerイメージ

コンテナーのファイルシステム、設定をまとめて保存したファイル。 複数のコンテナーで利用可能

Dcoker Hub

Dockerイメージを保存するレジストリ。githubのようなもの? DockerHub的なものが、AwsにはECR(Amazon EC2 Container Registry)がある。

Dockerfile

Dockerイメージの構築方法をコードとして記述したファイル

FROM java:8

ADD spring-boot-sample.jar /opt/spring-boot-sample/
EXPOSE 8080
WORKDIR /opt/spring-boot-sample/
CMD ["java", "-Xms512m", "-Xmx1g", "-jar", "spring-boot-sample.jar"]

チュートリアル

前提:

  1. docker install
  2. java8
  3. maven
  4. git

処理イメージ

  1. DockerfileからDokcerイメージする作成する
$ echo "イメージ一覧"
$ docker images  
  
$ echo "コンテナー一覧"
$ docker ps -a
  
$ echo "spring-bootのアプリをclone"
$ git clone https://github.com/liguofeng29/sample.git
$ cd sample
$ mvn clean package
  
$ echo "DockerfileからDokcerイメージ作成する"
$ sudo docker build -t spring-boot-docker-demo:first .
  
$ echo "イメージ一覧"
$ docker images

REPOSITORY                TAG                 IMAGE ID            CREATED              SIZE
spring-boot-docker-demo   first               9e6a5927b020        About a minute ago   656 MB
java                      8                   d23bdf5b1b1b        6 months ago         643 MB
  1. Dockerイメージからコンテナーを起動する
$ echo "ホスト9090をコンテナー8080へ転送で起動"
$ docker run -p 9090:8080 -t spring-boot-demo:first

$ echo "curlアプリ起動確認"
$ curl http://localhost:9090
  1. イメージをDockerHubへpushする
$ echo "DockerHubへログイン"
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: liguofeng29
Password:
Login Succeeded
  
$ echo "tagging"
$ docker tag 9e6a5927b020 liguofeng29/spring-boot-demo:first
$ docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
liguofeng29/spring-boot-demo   first               9e6a5927b020        16 minutes ago      656 MB
spring-boot-demo               first               9e6a5927b020        16 minutes ago      656 MB
java                           8                   d23bdf5b1b1b        6 months ago        643 MB

$ echo "push"
$ docker push liguofeng29/spring-boot-demo:first
first: digest: sha256:b7161bd07457c52efa7b92565482df73e3b8f9da52f24439498397eecca0f1a4 size: 2212
  1. コンテナーに新しいjarを配置する

vi src/main/java/org/lee/App.java

    @RequestMapping(value = "/now")
    String now() {
        return Calendar.getInstance().getTime().toString();
    }
$ echo "再ビルド"
$ mvn clean package
  
$ echo "コンテナー一覧"
$ docker ps -a
  
$ echo "コンテナーにjarをコピーする"
$ docker cp spring-boot-sample.jar コンテナーID:/opt/spring-boot-sample/
  
$ echo "コンテナー再起動"
$ docker restart コンテナーID
  
$ echon "検証"
$ curl http://localhost:9090

$ curl http://localhost:9090/now
→ 時刻が表示される
  1. コンテナーからイメージを作成&DockerHubへpush
$ echo "commit"
docker commit コンテナーID イメージ名:タグ名
例:docker commit 8eb5bba4cf8e liguofeng29/spring-boot-demo:second
  
$ echo "イメージ一覧"
docder images
    
$ echo "push"
$ docker push イメージ名:タグ名
例:docker push liguofeng29/spring-boot-demo:second

Dockerkコマンド一覧

コンテナ操作

  • attach コンテナにアタッチ
  • cp コンテナ・ホスト間でのファイルコピー
  • create コンテナ作成
  • diff コンテナのファイルシステム差分表示
  • exec 既存コンテナでコマンド実行
  • export コンテナをtarファイルで保存
  • history コンテナの履歴を表示
  • import tarファイルからコンテナ作成
  • inspect コンテナ・イメージの情報表示
  • kill コンテナの終了
  • logs コンテナのログ(出力)取得
  • pause コンテナの一時停止
  • port 公開ポートの表示
  • ps コンテナ一覧表示
  • rename コンテナ名の変更
  • restart コンテナの再起動
  • rm コンテナの削除
  • run コンテナの実行
  • start コンテナの実行
  • stats コンテナのリソース利用状況表示
  • stop コンテナの停止
  • top コンテナの実行状況表示
  • unpause コンテナの再開
  • update コンテナの設定を動的に変更
  • wait コンテナの終了を待つ

イメージ操作

  • build イメージのビルド
  • commit コンテナからイメージ作成
  • images イメージ一覧表示
  • inspect コンテナ・イメージの情報表示
  • load tarファイルからイメージ作成
  • rmi イメージの削除
  • save イメージをtar保存
  • tag イメージにタグ名を設定

Docker Hub(レジストリ)

  • login Docker Hub(レジストリ)にログイン
  • logout Docker Hub(レジストリ)からログアウト
  • pull Docker Hub(レジストリ)からコンテナ取得
  • push Docker Hub(レジストリ)にコンテナ保存
  • search Docker Hub(レジストリ)の検索

ネットワーク

  • network connect コンテナをネットワークに接続
  • network create ネットワーク作成
  • network disconnect コンテナのネットワークからの切断
  • network inspect ネットワークの状態表示
  • network ls ネットワーク一覧
  • network rm ネットワークの削除

ボリューム操作

  • volume create ボリュームの作成
  • volume inspect ボリュームの内容表示
  • volume ls ボリュームの一覧表示
  • volume rm ボリュームの削除

Swarmクラスタ

  • node accept Swarmノードをクラスタに追加
  • node demote Swarmノードをマネージャからワーカーに降格
  • node inspect Swarmノードの状態表示
  • node ls Swarmノードの一覧表示
  • node promote Swarmノードをワーカからマネージャに昇格
  • node rm Swarmノードをクラスタから削除
  • service create Swarmクラスタ上にサービス作成
  • service inspect Swarmクラスタ上にサービス状態表示
  • service ls Swarmクラスタ上のサービ一覧表示
  • service rm Swarmクラスタ上のサービス削除
  • service scale Swarmクラスタ上のサービスのコンテナ数(task)変更
  • service task Swarmクラスタ上のサービスのコンテナ(task)一覧表示
  • service update Swarmクラスタ上のサービスの設定変更
  • swarm init Swarmクラスタの作成
  • swarm join Swarmクラスタへのノード追加
  • swarm leave Swarmクラスタからのノード削除
  • swarm update Swarmクラスタの設定変更

プラグイン

  • plugin disable プラグインの無効化
  • plugin enable プラグインの有効化
  • plugin inspect プラグインの状態表示
  • plugin install プラグインのインストール
  • plugin ls プラグインの一覧表示
  • plugin rm プラグインの削除

その他

  • daemon サーバ起動
  • events イベントの監視
  • info Dockerの情報表示
  • system df ディスク利用状況の表示
  • system prune 不要なファイルの削除
  • version バージョン表示

spring4 - @Qualifier, alias

@Qualifierは、bean名を指定してDIを行う

package spring4.second;
public interface SomeService {
    public String getMessage();
}

package spring4.second;
import org.springframework.stereotype.Component;
@Component("SomeServiceAImpl")
public class SomeServiceAImpl implements SomeService {

    @Override
    public String getMessage() {
        return "AAA";
    }
}

package spring4.second;
import org.springframework.stereotype.Component;
@Component("SomeServiceBImpl")
public class SomeServiceBImpl implements SomeService {

    @Override
    public String getMessage() {
        return "BBB";
    }
}

package spring4.second;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class SomeObject {

    @Autowired
    @Qualifier("SomeServiceAImpl")
    private SomeService serviceA;

    @Autowired
    @Qualifier("SomeServiceBImpl")
    private SomeService serviceB;

    public void run() {
        System.out.println(serviceA.getMessage());
        System.out.println(serviceB.getMessage());
    }
}
package spring4.second;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        ApplicationContext context1 = new AnnotationConfigApplicationContext(App.class);
        context1.getBean(SomeObject.class).run();

    }
}

出力:
AAA
BBB

aliasを使い、Bean名にエイリアスの名前を付ける

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="spring4.second" />

    <alias name="SomeServiceAImpl" alias="SomeServiceBImpl" />
</beans>
package spring4.second;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.FileSystemXmlApplicationContext;

@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        ApplicationContext context2 = new FileSystemXmlApplicationContext(
                "classpath:spring4/second/applicationContext.xml");
        context2.getBean(SomeObject.class).run();
    }
}

出力:
AAA
AAA
<alias name="SomeServiceAImpl" alias="SomeServiceBImpl" />
をすることで、
@Qualifier("SomeServiceBImpl") -> @Qualifier("SomeServiceAImpl")になる

git:
https://github.com/liguofeng29/java-project/tree/master/src/main/java/spring4/second

Spring4 - BeanFactoryPostProcessor

Beanを遅延生成させる

@Component
public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            // bean名がcomponentB以外は遅延生成に設定
            if (!"componentB".equals(beanName)) {
                beanFactory.getBeanDefinition(beanName).setLazyInit(true);
            }
        }
    }
}

git:
https://github.com/liguofeng29/java-project/tree/master/src/main/java/spring4/third/lazyInitSample

Beanのscopeを変更する

package spring4.third.modifyBeanDefine;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
public class ModifyBeanDefinitionFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            // componentBのscopeを変更する
            if (beanName.equals("componentB")) {
                beanFactory.getBeanDefinition(beanName).setScope("prototype");
            }
        }
    }
}

git:
https://github.com/liguofeng29/java-project/tree/master/src/main/java/spring4/third/modifyBeanDefine