Jhipster 建立微服務詳細教程
一、簡介
jHipster 集成了springcloud的註冊中心(eureka server)和配置中心(config server),eureka server 能夠將我們自己開發的微服務(Microservices)都納入到它自身當中,config server 能夠集中管理應用程式各個環境下的配置,預設使用git 倉庫儲存配置內容。
Microservices沒有前端頁面,前端頁面由閘道器產生(gateway),許可權控制可以通過UAA進行管理,並且要在JHipster Registry中註冊、發現和管理。
JHipster Registry
是微服架構必不可少的。它把所有的元件連線在一起,使他們能夠互相通訊。Microservices
Gateway
它為所有微服務提供了HTTP路由和負載均衡,服務質量,安全和API文件。UAA
是一個使用OAuth2認證協議、為JHipster微服務提供使用者賬戶和授權的服務。
二、微服務安裝
1、安裝JHipster Registry
JHipster Registry 是一個標準的jhipster,我們只需要克隆下來執行它即可。沒有必要開啟idea或者修改它:
(1)第一種方式
git clone https://github.com/jhipster/jhipster-registry #cd到jhipster-registry資料夾,執行 mvnw
執行mvnw命令後,系統將自動編譯執行。
(2)第二種方式
git clone https://github.com/jhipster/jhipster-registry.git
# 編譯啟動
mvn clean package
java -jar target/jhipster-registry-*.war
開啟http://localhost:8761/,並且登入,預設賬戶admin,密碼admin。
Instances Registered是自動註冊的服務,當我們建立服務後,將自動註冊到這裡
2、安裝uaa
部署完jHipster registry ,我們需要建立一個用於控制訪問許可權的專案。
#建立資料夾存放專案
mkdir uaa
#切換到資料夾
cd uaa
#建立UAA服務
yo jhipster
#僅供參考
? (1/14) Which *type* of application would you like to create? [BETA] JHipster UAA server (for microservice OAuth2 authentication)
? (2/14) What is the base name of your application? uaa
? (3/14) Would you like to install other generators from the JHipster Market Place? No
? (3/14) 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. 9999
? (4/14) What is your default Java package name? cn.jhipter.uaa
? (5/14) Which Service Discovery and Configuration solution would you like to use? JHipster Registry (using Eureka and Spring Cloud Config)
? (6/14) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)
? (7/14) Which *production* database would you like to use? MySQL
? (8/14) Which *development* database would you like to use? MySQL
? (9/14) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (10/14) Would you like to use Maven or Gradle for building the backend? Maven
? (11/14) Which other technologies would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (12/14) Would you like to enable internationalization support? Yes
#國際化支援,選擇簡體中文
? Please choose the native language of the application? Chinese (Simplified)
? Please choose additional languages to install (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (13/14) Which testing frameworks would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)Gatling
通過mvnw
編譯啟動專案
3、建立一個Microservices Application(微服務)
操作同uaa
#建立資料夾存放專案
mkdir app
#切換到資料夾
cd app
#建立微服務
yo jhipster
#僅供參考
? (1/14) Which *type* of application would you like to create? Microservice application
? (2/14) What is the base name of your application? app
? (3/14) Would you like to install other generators from the JHipster Market Place? No
? (3/14) 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/14) What is your default Java package name? cn.jhipster.app
? (5/14) Which *type* of authentication would you like to use? [BETA] Authentication with JHipster UAA server (the server must be generated separately)
? (6/14) What is the folder path of your UAA application?. ../uaa
? (7/14) Which Service Discovery and Configuration solution would you like to use? JHipster Registry (using Eureka and Spring Cloud Config)
? (8/14) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle)
? (9/14) Which *production* database would you like to use? MySQL
#可以選擇預設的H2,會在磁碟或者記憶體中生成本地資料庫,就要就不需要我們修改配置檔案了
? (10/14) Which *development* database would you like to use? MySQL
? (11/14) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (12/14) Would you like to use Maven or Gradle for building the backend? Maven
? (13/14) Which other technologies would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (14/14) Would you like to enable internationalization support? Yes
? Please choose the native language of the application? Chinese (Simplified)
? Please choose additional languages to install (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (15/14) Which testing frameworks would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)Gatling
同樣還需要修改資料庫配置\src\main\resources\config\application-dev.yml
,在application.yml中可以看到,系統已經自動幫我們註冊了
啟動專案(參考uaa)
4、建立一個閘道器
建立一個閘道器,用來做為所有請求的入口
#建立資料夾存放專案
mkdir gateway
#切換到資料夾
cd gateway
#建立微服務閘道器
yo jhipster
#僅供參考
? (1/14) Which *type* of application would you like to create? Microservice gateway
? (2/14) What is the base name of your application? gateway
? (3/14) Would you like to install other generators from the JHipster Market Place? No
? (3/14) 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. 8080
? (4/14) What is your default Java package name? cn.jhipster.gateway
? (5/14) Which *type* of authentication would you like to use? [BETA] Authentication with JHipster UAA server (the server must be generated separately)
? (6/14) What is the folder path of your UAA application?. ../uaa
? (7/14) Which Service Discovery and Configuration solution would you like to use? JHipster Registry (using Eureka and Spring Cloud Config)
? (8/14) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)
? (9/14) Which *production* database would you like to use? MySQL
? (10/14) Which *development* database would you like to use? MySQL
? (11/14) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (12/14) Would you like to use Maven or Gradle for building the backend? Maven
? (13/14) Which other technologies would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (14/14) Would you like to use the LibSass stylesheet preprocessor for your CSS? No
? (15/14) Would you like to enable internationalization support? Yes
? Please choose the native language of the application? Chinese (Simplified)
? Please choose additional languages to install (Press <space> to select, <a> to toggle all, <i> to inverse selection)
? (16/14) Which testing frameworks would you like to use? (Press <space> to select, <a> to toggle all, <i> to inverse selection)Gatling
同樣需要修改資料庫配置並啟動專案
開啟http://localhost:8761,就可以檢視到我們建立的服務。
將它和閘道器服務進行對比,可以看出閘道器頁面管理裡面多了些選單。
軟體推薦:
由於建立都是敲命令實現的,而系統原生的cmd使用起來並不怎麼友好,可以使用cmder這款軟體進行操作。cmder
是一個跨平臺的命令列增強工具,可以整合windows batch, power shell, git, linux bash等多種命令行於一體,使用起來還是十分方便的
相關介紹: