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 依賴如下
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-eureka-server</artifactId>
-
</dependency>
-
</dependencies>
-
<dependencyManagement>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-dependencies</artifactId>
-
<version>Dalston.RELEASE</version>
-
<type>pom</type>
-
<scope>import</scope>
-
</dependency>
-
</dependencies>
-
</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 在啟動類上加上註解 如下
-
@EnableEurekaServer
-
@SpringBootApplication
-
public class SpringCloudApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(SpringCloudApplication.class, args);
-
}
-
}
@SpringBootApplication public class SpringCloudApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudApplication.class, args); } }
1.4 配置檔案application.properties
-
#埠號
-
server.port=1111
-
# eureka.client.registerWithEureka :表示是否將自己註冊到Eureka Server,預設為true。
-
# 由於當前這個應用就是Eureka Server,故而設為false
-
eureka.client.register-with-eureka=false
-
# eureka.client.fetchRegistry :表示是否從Eureka Server獲取註冊資訊,預設為true。因為這是一個單點的Eureka Server,
-
# 不需要同步其他的Eureka Server節點的資料,故而設為false。
-
eureka.client.fetch-registry=false
-
# eureka.client.serviceUrl.defaultZone :設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。預設是
-
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
配置好了以後啟動 訪問 http://localhost:1111/ 就出現了spring cloud 管理中心
2.搭建服務端
2.1 建立springboot專案同上
2.2 修改pom檔案,新增spring cloud 依賴如下
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>1.5.9.RELEASE</version>
-
<relativePath/> <!-- lookup parent from repository -->
-
</parent>
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-
<java.version>1.8</java.version>
-
</properties>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-config</artifactId>
-
</dependency>
-
<!-- sping cloud 註冊服務 -->
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-eureka</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
</dependencies>
-
<dependencyManagement>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-dependencies</artifactId>
-
<version>Dalston.RELEASE</version>
-
<type>pom</type>
-
<scope>import</scope>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
2.3 在啟動類上加上註解 如下
-
@SpringBootApplication
-
@EnableEurekaClient
-
@EnableFeignClients
-
public class SpringServiceApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(SpringServiceApplication.class, args);
-
}
-
}
2.4 配置檔案application.properties
-
#服務名稱
-
spring.application.name=compute-service1
-
#埠號
-
server.port=2222
-
#註冊中心
-
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
-
spring.cloud.config.discovery.enabled=true
-
#註冊中心的服務id
-
spring.cloud.config.discovery.serviceId=compute-server
2.5啟動專案後如下圖
紅框內就是啟動的專案,顯示的是服務的id和埠號
3 spring cloud 路由閘道器服務
3.1 建立 spring boot專案 同上
3.2 修改pom檔案,新增spring cloud 依賴如下
-
<groupId>com.xue</groupId>
-
<artifactId>sring-zuul</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
<packaging>jar</packaging>
-
<name>sring-zuul</name>
-
<description>Demo project for Spring Boot</description>
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>1.5.9.RELEASE</version>
-
<relativePath/> <!-- lookup parent from repository -->
-
</parent>
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-
<java.version>1.8</java.version>
-
</properties>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-eureka</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-zuul</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
</dependencies>
-
<dependencyManagement>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-dependencies</artifactId>
-
<version>Dalston.RELEASE</version>
-
<type>pom</type>
-
<scope>import</scope>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
3.3 在啟動類上加上註解 如下
-
@EnableZuulProxy
-
@EnableEurekaClient
-
@SpringBootApplication
-
public class SringZuulApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(SringZuulApplication.class, args);
-
}
-
}
3.4 配置檔案application.properties
-
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
-
server.port=3333
-
spring.application.name=service-zuul
-
#表示只要訪問以/api-a/開頭的多層目錄都可以路由到 id為compute-service1的服務上
-
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不僅只是路由,並且還能過濾,做一些安全驗證。繼續改造工程;
-
@Component
-
public class MyFilter extends ZuulFilter{
-
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
-
@Override
-
public String filterType() {
-
return "pre";
-
}
-
@Override
-
public int filterOrder() {
-
return 0;
-
}
-
@Override
-
public boolean shouldFilter() {
-
return true;
-
}
-
@Override
-
public Object run() {
-
RequestContext ctx = RequestContext.getCurrentContext();
-
HttpServletRequest request = ctx.getRequest();
-
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
-
Object accessToken = request.getParameter("token");
-
if(accessToken == null) {
-
log.warn("token is empty");
-
ctx.setSendZuulResponse(false);
-
ctx.setResponseStatusCode(401);
-
try {
-
ctx.getResponse().getWriter().write("token is empty");
-
}catch (Exception e){}
-
return null;
-
}
-
log.info("ok");
-
return null;
-
}
-
}
- filterType:返回一個字串代表過濾器的型別,在zuul中定義了四種不同生命週期的過濾器型別,具體如下:
- pre:路由之前
- routing:路由之時
- post: 路由之後
- error:傳送錯誤呼叫
- filterOrder:過濾的順序
- shouldFilter:這裡可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。
- run:過濾器的具體邏輯。可用很複雜,包括查sql,nosql去判斷該請求到底有沒有許可權訪問。
總結
專案原始碼地址: springcloud專案原始碼