1. 程式人生 > >開源專案學習之------AG-Admin環境搭建

開源專案學習之------AG-Admin環境搭建

一、框架簡介


AG-Admin是基於Spring Cloud微服務化開發平臺,具有統一授權、認證後臺管理系統,其中包含具備使用者管理、資源許可權管理、閘道器API管理等多個模組,支援多業務系統並行開發,可以作為後端服務的開發腳手架。程式碼簡潔,架構清晰,適合學習和直接專案中使用。核心技術採用Eureka、Fegin、Ribbon、Zuul、Hystrix、JWT Token、Mybatis等主要框架和中介軟體,前端採用vue-element-admin元件。


模組說明:



二、環境搭建


(一)、匯入原始碼

首先下載原始碼到本地,這裡下載的2.2版本,最新的有可能有問題。。。




然後匯入到IDEA中,並更新maven依賴,由於專案中使用了lombok,所有需要安裝lombok外掛,否則你的IDE會報程式碼缺失。




專案結構如下




├─ace-security
│  │  
│  ├─ace-admin----------------管理端服務層
│  │ 
│  ├─ace-auth-----------------鑑權中心
│  │ 
│  ├─ace-gate-----------------閘道器負載中心
│  │ 
│  ├─ace-center---------------服務註冊中心
│  │   
│  ├─ace-monitor--------------統一監控中心
│  │
│  ├─ace-config---------------統一配置中心
│  │
│  ├─ace-api------------------公共服務介面包
│  │
│  ├─ace-demo-----------------示例服務程式碼
│  │
│  └─ace-sidebar--------------呼叫第三方語言



(二)、安裝Redis與rabbitmq和node


Redis安裝可參考我先前的zheng環境搭建篇,node安裝也很簡單一直下一步下一步,就行了,安裝好之後,更換成淘寶源就行了。


2.1、rabbitmq安裝


Rabbit MQ 是建立在Erlang OTP平臺上,所以在安裝rabbitMQ之前,需要先安裝Erlang ,下載地址為http://www.erlang.org/downloads




下載好之後,雙擊進行安裝






安裝完成之後建立一個名為ERLANG_HOME的環境變數,其值指向erlang的安裝目錄




同時將%ERLANG_HOME%\bin加入到Path中




最後開啟命令列,輸入erl




如果出現erlang的版本資訊就表示erlang語言環境安裝成功;


然後下載Rabbitmq,下載地址為:http://www.rabbitmq.com/download.html








下載好之後,雙擊進行安裝







預設安裝的RabbitMQ 監聽埠是5672,預設使用者名稱:guest,密碼:guest。


2.2、RabbitMQ-Plugins外掛安裝


這個外掛相當於是一個管理介面,方便我們在瀏覽器介面檢視RabbitMQ各個訊息佇列以及exchange的工作情況,安裝方法是:開啟命令列cd進入rabbitmq的sbin目錄(我的目錄是:C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.2\sbin),輸入:


rabbitmq-plugins enable rabbitmq_management
   

命令,稍等會會發現出現plugins安裝成功的提示




然後重啟服務,使用命令(此時最後以管理員執行CMD):

net stop RabbitMQ && net start RabbitMQ
   



外掛安裝完之後,在瀏覽器輸入http://localhost:15672進行驗證




(三)、初始化資料


分別匯入ace-admin/db/init.sql、ace-auth-server/db/init.sql 這兩個指令碼,匯入成功之後,會建立ag_admin和ag_auth兩個資料庫以及以下一些表:


表竟然沒有中文註釋。。。。


(四)、配置hosts檔案



    
  1. 127.0.0.1 ace-admin
  2. 127.0.0.1 ACE-ADMIN

修改專案裡所有的defaultZone引數,他預設用的是docker,但是我們是這本地開發的,所以改成本地的




(五)、修改資料庫配置檔案


修改ace-admin/src/main/resources/application.yml、ace-gate/src/main/resources/application.yml這兩個配置檔案中連線資料庫的一些資訊:




(六)、按順序執行main類:CenterBootstrap(ace-center)、ConfigServerBootstrap(ace-config)、AuthBootstrap(ace-auth-server)、AdminBootstrap(ace-admin)、GateBootstrap(ace-gate)







如果報以下錯誤MQ超時:

Caused by: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:368) at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:565) at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1430) at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411) at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:712) at org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint.send(AmqpOutboundEndpoint.java:134) at org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint.handleRequestMessage(AmqpOutboundEndpoint.java:122) at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109) at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) ... 55 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:50) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:907) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:799) at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:352) ... 63 more
   

基本上就是MQ配置有問題。



如果報以下錯誤:


    
  1. Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
  2. 2018-01-14 17:25:15.447 ERROR [bootstrap,,,] 16896 --- [ main] o.s.boot.SpringApplication : Application startup failed
  3. java.lang.IllegalStateException: Failed to execute CommandLineRunner
  4. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
  5. at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
  6. at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
  7. at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
  8. at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134)
  9. at com.github.wxiaoqi.security.admin.AdminBootstrap.main(AdminBootstrap.java:31)
  10. Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: ServiceAuthFeign#getUserPublicKey(String,String) timed-out and no fallback available.
  11. at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:819)
  12. at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:804)
  13. at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:140)
  14. at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
  15. at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
  16. at com.netflix.hystrix.AbstractCommand$DeprecatedOnFallbackHookApplication$1.onError(AbstractCommand.java:1472)
  17. at com.netflix.hystrix.AbstractCommand$FallbackHookApplication$1.onError(AbstractCommand.java:1397)
  18. at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
  19. at rx.observers.Subscribers$5.onError(Subscribers.java:230)
  20. at rx.internal.operators.OnSubscribeThrow.call(OnSubscribeThrow.java:44)
  21. at rx.internal.operators.OnSubscribeThrow.call(OnSubscribeThrow.java:28)
  22. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  23. at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51)
  24. at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
  25. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  26. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
  27. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
  28. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
  29. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
  30. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
  31. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
  32. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  33. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
  34. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
  35. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  36. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
  37. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
  38. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
  39. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
  40. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  41. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
  42. at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
  43. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
  44. at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
  45. at rx.Observable.unsafeSubscribe(Observable.java:10211)
  46. at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:142)
  47. at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
  48. at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
  49. at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1$1.run(AbstractCommand.java:1154)
  50. at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:45)
  51. at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41)
  52. at org.springframework.cloud.sleuth.instrument.hystrix.SleuthHystrixConcurrencyStrategy$HystrixTraceCallable.call(SleuthHystrixConcurrencyStrategy.java:154)
  53. at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:61)
  54. at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.tick(AbstractCommand.java:1159)
  55. at com.netflix.hystrix.util.HystrixTimer$1.run(HystrixTimer.java:99)
  56. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  57. at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
  58. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
  59. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
  60. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  61. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  62. at java.lang.Thread.run(Thread.java:748)
  63. Caused by: java.util.concurrent.TimeoutException: null
  64. at com.netflix.hystrix.AbstractCommand.handleTimeoutViaFallback(AbstractCommand.java:997)
  65. at com.netflix.hystrix.AbstractCommand.access$500(AbstractCommand.java:60)
  66. at com.netflix.hystrix.AbstractCommand$12.call(AbstractCommand.java:610)
  67. at com.netflix.hystrix.AbstractCommand$12.call(AbstractCommand.java:601)
  68. at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:140)
  69. ... 16 common frames omitted


版本問題。。



全部啟動之後




(七)、啟動前端專案


首先下載UI:https://gitee.com/geek_qi/AG-Admin-v2.0 下載下來之後,解壓如下



然後在該目錄下執行以下命令安裝依賴,用的是淘寶的源,不清楚地朋友可以百度node更換淘寶源:

cnpm install
   



安裝成功之後,會多出一個node_modules目錄




然後啟動服務


    
  1. # 本地開發 開啟服務
  2. npm run dev


他會自動開啟瀏覽器,訪問該頁面




然後點選登陸。




登陸成功之後如上。。


(八)、部署nginx配置參考


nginx配置如下:


    
  1. location / {
  2. # 指向我們打包後上傳的前端檔案
  3. root /opt/nginx/dist;
  4. index index.html;
  5. }
  6. location /jwt/ {
  7. # 轉發請求到後端服務閘道器
  8. proxy_pass http://127.0.0.1:8765/jwt/;
  9. }
  10. location /api/ {
  11. proxy_pass http://127.0.0.1:8765/api/;
  12. }


很簡單,不會的可參考我前兩篇文章的配置。。