隨着微服務的普及以及docker容器的廣泛應用,有傳統的soa服務衍生出微服務的概念,微服務強調的是服務的獨立性,屏蔽底層物理平台的差異,此時你會發現微服務跟容器技術完美契合。在此基礎上衍生出的雲原生以及DevOps的概念,廢話不多說介紹一個非常牛叉的springCloud腳手架- -jhipster。
- 安裝
- 安裝Java 8 from the Oracle website.
- 安裝Node.js from the Node.js website (請安裝 64-bit version)
- 安裝npm包:
npm install -g npm
- 如果你想使用jhipster應用市場, 請安裝 Yeoman:
npm install -g yo
- 最后安裝JHipster:
npm install -g generator-jhipster
- 生成項目
- 選擇一個空的文件夾打開cmd:jhipster
- 根據一步步step提示選擇構建自己的服務項目
windows下:
以下demo選擇微服務應用。實際中根據自己需求生產項目。 1: Which *type* of application would you like to create? (Use arrow keys) Monolithic application (recommended for simple projects) //簡單項目 Microservice application // 微服務應用 Microservice gateway // 微服務網關 JHipster UAA server (for microservice OAuth2 authentication) // 微服務認證 2 :What is the base name of your application? (huhuawei) 輸入服務名稱 3: As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. (8081) 設置服務的端口號 4:What is your default Java package name? (com.mycompany.myapp) 設置包名 5:Which service discovery server do you want to use? (Use arrow keys) JHipster Registry (uses Eureka) Consul No service discovery 選擇注冊中心。一般選擇Registry比較多 6:Which *type* of authentication would you like to use? (Use arrow keys) JWT authentication (stateless, with a token) // jwt OAuth 2.0 / OIDC Authentication (stateful, works with Keycloak and Okta)//Oauth2 OIDC 認證服務 Authentication with JHipster UAA server (the server must be generated separately) // Oauth2+jwt Uaa認證服務 選擇授權中心 7: Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL) MongoDB Couchbase No database Cassandra 選擇數據庫支持Nosql跟常見RDMB數據庫 8:? Which *production* database would you like to use? (Use arrow keys) MySQL MariaDB PostgreSQL Oracle Microsoft SQL Server 選擇數據庫,這邊會出現兩次第一次是production 第二次是devlopment 9:Do you want to use the Spring cache abstraction? 根據需求選擇緩存 10:Do you want to use Hibernate 2nd level cache? (Y/n) 是否支持二級緩存 11: Would you like to use Maven or Gradle for building the backend? (Use arrow keys) Maven Gradle 12:Which other technologies would you like to use? 安裝一些其他的組件。如ES,KAFKA之類的 13:Would you like to enable internationalization support? (Y/n) 支持國際化? 14: Please choose the native language of the application (Use arrow keys) English Estonian Farsi French Galician ........ 選擇本地支持的語言包含中文 15:Please choose additional languages to install 可以額外安裝其他語言 16:Besides JUnit and Jest, which testing frameworks would you like to use? Gatling Cucumber 選擇測試框架,針對微服務http接口測試,生成測試報告 17:Would you like to install other generators from the JHipster Marketplace? 從jhipster市場中選擇組件安裝 - 如果你覺得安裝這些環境太麻煩,你又熟悉docker的基本命令,那建議使用docker去生成項目;
選擇linux服務器,安裝docker; yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce --showduplicates | sort -r sudo yum install -y docker-ce sudo systemctl start docker sudo systemctl enable docker 拉取jhipster官方鏡像 docker pull jhipster/jhipster:master 啟動jhipster鏡像,選擇一個空文件/jhipster夾掛載到容器中 docker container run --name jhipster -v /jhipster:/home/jhipster/app -v ~/.m2:/home/jhipster/.m2 -p 8080:8080 -p 9000:9000 -p 3001:3001 -d -t jhipster/jhipster 進入容器中 docker container exec -it --user root jhipster bash 然后就可以生成項目了。與windows上操作無差別
- 項目的組成簡單介紹
Gateway: springcloud Zuul Proxy 進行動態路由微服務。
Registry:主要封裝了Eureka以及配置中心Config Server。
Jhipster Console:封裝了Elk監控 以及 sleuth zipkin 等分布式鏈路監控組件。
Jhipster Uaa: 采用UAA用戶登錄認證 OAUTH2集中式認證,默認不使用的話則是JWT無狀態認證
- 總結
上述僅僅是大體的架構,Jhipster內部使用了很多插件來進行敏捷開發,包括實體類JDL快速生成從數據庫到Controller代碼,開發效率非常之高。適合中小型企業采用,而且Jhipster支持DockerFile與Compose 文件生成,可以幫助我們快速容器化服務部署。