1. 程式人生 > >spring cloud 實戰專案搭建

spring cloud 實戰專案搭建

spring cloud 實戰專案搭建

 

 

Spring Cloud簡介

 

Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它為基於JVM的雲應用開發中的配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、全域性鎖、決策競選、分散式會話和叢集狀態管理等操作提供了一種簡單的開發方式。

Spring Cloud包含了多個子專案(針對分散式系統中涉及的多個不同開源產品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等專案。

1.搭建eureka服務

 

       1.1 建立springboot專案

 http://start.spring.io/ 自定義spring boot線上maven構建工具很方便

 

 

1.2 修改pom檔案,新增spring cloud 依賴如下

 
  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-test</artifactId>

  5. <scope>test</scope>

  6. </dependency>

  7.  
  8. <dependency>

  9. <groupId>org.springframework.cloud</groupId>

  10. <artifactId>spring-cloud-starter-eureka-server</artifactId>

  11. </dependency>

  12. </dependencies>

  13.  
  14. <dependencyManagement>

  15. <dependencies>

  16. <dependency>

  17. <groupId>org.springframework.cloud</groupId>

  18. <artifactId>spring-cloud-dependencies</artifactId>

  19. <version>Dalston.RELEASE</version>

  20. <type>pom</type>

  21. <scope>import</scope>

  22. </dependency>

  23. </dependencies>

  24. </dependencyManagement>

<version>Dalston.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

 程式碼中加紅的部分是對應的版本

如果 spring boot的版本是 1.4之前 對應的版本是Brixton.RELEASE,1.4之後 對應的Dalston.RELEASE版本如果對應不起來會報錯

 

 

1.3 在啟動類上加上註解 如下

 
  1. @EnableEurekaServer

  2. @SpringBootApplication

  3. public class SpringCloudApplication {

  4.  
  5. public static void main(String[] args) {

  6. SpringApplication.run(SpringCloudApplication.class, args);

  7. }

  8. }

@SpringBootApplication public class SpringCloudApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudApplication.class, args); } }

1.4 配置檔案application.properties

 

 
  1. #埠號

  2. server.port=1111

  3. # eureka.client.registerWithEureka :表示是否將自己註冊到Eureka Server,預設為true。

  4. # 由於當前這個應用就是Eureka Server,故而設為false

  5. eureka.client.register-with-eureka=false

  6. # eureka.client.fetchRegistry :表示是否從Eureka Server獲取註冊資訊,預設為true。因為這是一個單點的Eureka Server,

  7. # 不需要同步其他的Eureka Server節點的資料,故而設為false。

  8. eureka.client.fetch-registry=false

  9. # eureka.client.serviceUrl.defaultZone :設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。預設是

  10. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

配置好了以後啟動  訪問 http://localhost:1111/ 就出現了spring cloud 管理中心

 

2.搭建服務端

    2.1 建立springboot專案同上

 

    2.2 修改pom檔案,新增spring cloud 依賴如下

 

 
  1.         <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.9.RELEASE</version>

  5. <relativePath/> <!-- lookup parent from repository -->

  6. </parent>

  7.  
  8. <properties>

  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  10. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  11. <java.version>1.8</java.version>

  12. </properties>

  13.  
  14. <dependencies>

  15. <dependency>

  16. <groupId>org.springframework.boot</groupId>

  17. <artifactId>spring-boot-starter</artifactId>

  18. </dependency>

  19. <dependency>

  20. <groupId>org.springframework.cloud</groupId>

  21. <artifactId>spring-cloud-starter-config</artifactId>

  22. </dependency>

  23. <!-- sping cloud 註冊服務 -->

  24. <dependency>

  25. <groupId>org.springframework.cloud</groupId>

  26. <artifactId>spring-cloud-starter-eureka</artifactId>

  27. </dependency>

  28. <dependency>

  29. <groupId>org.springframework.boot</groupId>

  30. <artifactId>spring-boot-starter-test</artifactId>

  31. <scope>test</scope>

  32. </dependency>

  33. <dependency>

  34. <groupId>org.springframework.boot</groupId>

  35. <artifactId>spring-boot-starter-web</artifactId>

  36. </dependency>

  37. </dependencies>

  38.  
  39. <dependencyManagement>

  40. <dependencies>

  41. <dependency>

  42. <groupId>org.springframework.cloud</groupId>

  43. <artifactId>spring-cloud-dependencies</artifactId>

  44. <version>Dalston.RELEASE</version>

  45. <type>pom</type>

  46. <scope>import</scope>

  47. </dependency>

  48. </dependencies>

  49. </dependencyManagement>

  50.  
  51. <build>

  52. <plugins>

  53. <plugin>

  54. <groupId>org.springframework.boot</groupId>

  55. <artifactId>spring-boot-maven-plugin</artifactId>

  56. </plugin>

  57. </plugins>

  58. </build>

2.3 在啟動類上加上註解 如下

 
  1. @SpringBootApplication

  2. @EnableEurekaClient

  3. @EnableFeignClients

  4. public class SpringServiceApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(SpringServiceApplication.class, args);

  8. }

  9. }

2.4 配置檔案application.properties

 
  1. #服務名稱

  2. spring.application.name=compute-service1

  3. #埠號

  4. server.port=2222

  5. #註冊中心

  6. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  7. spring.cloud.config.discovery.enabled=true

  8. #註冊中心的服務id

  9. spring.cloud.config.discovery.serviceId=compute-server

    2.5啟動專案後如下圖    

紅框內就是啟動的專案,顯示的是服務的id和埠號

 

 

3 spring cloud 路由閘道器服務

 

 

    3.1 建立 spring boot專案 同上

    3.2 修改pom檔案,新增spring cloud 依賴如下

 
  1.     <groupId>com.xue</groupId>

  2. <artifactId>sring-zuul</artifactId>

  3. <version>0.0.1-SNAPSHOT</version>

  4. <packaging>jar</packaging>

  5.  
  6. <name>sring-zuul</name>

  7. <description>Demo project for Spring Boot</description>

  8.  
  9. <parent>

  10. <groupId>org.springframework.boot</groupId>

  11. <artifactId>spring-boot-starter-parent</artifactId>

  12. <version>1.5.9.RELEASE</version>

  13. <relativePath/> <!-- lookup parent from repository -->

  14. </parent>

  15.  
  16. <properties>

  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  19. <java.version>1.8</java.version>

  20. </properties>

  21.  
  22. <dependencies>

  23. <dependency>

  24. <groupId>org.springframework.cloud</groupId>

  25. <artifactId>spring-cloud-starter-eureka</artifactId>

  26. </dependency>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-zuul</artifactId>

  30. </dependency>

  31. <dependency>

  32. <groupId>org.springframework.boot</groupId>

  33. <artifactId>spring-boot-starter-web</artifactId>

  34. </dependency>

  35.  
  36. <dependency>

  37. <groupId>org.springframework.boot</groupId>

  38. <artifactId>spring-boot-starter-test</artifactId>

  39. <scope>test</scope>

  40. </dependency>

  41. </dependencies>

  42. <dependencyManagement>

  43. <dependencies>

  44. <dependency>

  45. <groupId>org.springframework.cloud</groupId>

  46. <artifactId>spring-cloud-dependencies</artifactId>

  47. <version>Dalston.RELEASE</version>

  48. <type>pom</type>

  49. <scope>import</scope>

  50. </dependency>

  51. </dependencies>

  52. </dependencyManagement>

  53. <build>

  54. <plugins>

  55. <plugin>

  56. <groupId>org.springframework.boot</groupId>

  57. <artifactId>spring-boot-maven-plugin</artifactId>

  58. </plugin>

  59. </plugins>

  60. </build>

   

3.3 在啟動類上加上註解 如下

        

 
  1. @EnableZuulProxy

  2. @EnableEurekaClient

  3. @SpringBootApplication

  4. public class SringZuulApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(SringZuulApplication.class, args);

  8. }

  9. }

   

3.4 配置檔案application.properties

     

 
  1. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  2. server.port=3333

  3. spring.application.name=service-zuul

  4. #表示只要訪問以/api-a/開頭的多層目錄都可以路由到 id為compute-service1的服務上

  5. zuul.routes.compute-service1=/api-a/**

#表示只要訪問以/api-a/開頭的多層目錄都可以路由到 id為compute-service1的服務上
zuul.routes.compute-service1=/api-a/**
上面的一行等同於下面的兩行
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=compute-service1

萬用字元 含義 舉例 解釋
? 匹配任意單個字元 /feign-consumer/? 匹配/feign-consumer/a,/feign-consumer/b,/feign-consumer/c等
* 匹配任意數量的字元 /feign-consumer/* 匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,無法匹配/feign-consumer/a/b/c
** 匹配任意數量的字元 /feign-consumer/* 匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,也可以匹配/feign-consumer/a/b/c

    3.4 啟動專案 測試

 

   3.5服務過濾

zuul不僅只是路由,並且還能過濾,做一些安全驗證。繼續改造工程;

 
  1. @Component

  2. public class MyFilter extends ZuulFilter{

  3.  
  4. private static Logger log = LoggerFactory.getLogger(MyFilter.class);

  5. @Override

  6. public String filterType() {

  7. return "pre";

  8. }

  9.  
  10. @Override

  11. public int filterOrder() {

  12. return 0;

  13. }

  14.  
  15. @Override

  16. public boolean shouldFilter() {

  17. return true;

  18. }

  19.  
  20. @Override

  21. public Object run() {

  22. RequestContext ctx = RequestContext.getCurrentContext();

  23. HttpServletRequest request = ctx.getRequest();

  24. log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));

  25. Object accessToken = request.getParameter("token");

  26. if(accessToken == null) {

  27. log.warn("token is empty");

  28. ctx.setSendZuulResponse(false);

  29. ctx.setResponseStatusCode(401);

  30. try {

  31. ctx.getResponse().getWriter().write("token is empty");

  32. }catch (Exception e){}

  33.  
  34. return null;

  35. }

  36. log.info("ok");

  37. return null;

  38. }

  39. }

  • filterType:返回一個字串代表過濾器的型別,在zuul中定義了四種不同生命週期的過濾器型別,具體如下: 
    • pre:路由之前
    • routing:路由之時
    • post: 路由之後
    • error:傳送錯誤呼叫
    • filterOrder:過濾的順序
    • shouldFilter:這裡可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。
    • run:過濾器的具體邏輯。可用很複雜,包括查sql,nosql去判斷該請求到底有沒有許可權訪問。

 

總結

專案原始碼地址: springcloud專案原始碼