一. 服務說明
- eureka-server ------> 注冊中心
- member-service ------> 會員服務接口
- member-service-impl ------> 會員服務實現
- order-service ------> 訂單服務接口
- order-service-impl -------> 會員服務實現
補充說明: 之后要創建member-service,order-service兩個接口服務,是為了提高代碼的覆用性.具體可參見后面的代碼
二.創建項目
1.整體結構
2.創建springcloud-hystrix父工程
2.1 創建一個maven項目(略)
2.1 pom.xml依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>qinfeng.zheng</groupId>
<artifactId>springcloud-hystrix</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
<module>member-service</module>
<module>member-service-impl</module>
<module>order-service</module>
<module>order-service-impl</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>
<!-- 管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- SpringBoot整合fegnin客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- hystrix斷路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<!-- 注意: 這里必須要添加, 否者各種依賴有問題 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3.創建eureka-service子模塊--->注冊中心
3.1 創建一個maven工程
3.2 pom.xml依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-hystrix</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.3 在resources目錄下創建application.yml文件
###服務端口號
server:
port: 8888
##定義服務名稱
spring:
application:
name: eureka-server
eureka:
instance:
###注冊中心ip地址
hostname: 127.0.0.1
client:
serviceUrl:
##注冊地址
defaultZone: http://${eureka.instance.hostname}:8888/eureka/
####因為自己是注冊中心,是否需要將自己注冊給自己的注冊中心(集群的時候是需要是為true)
register-with-eureka: false
###因為自己是注冊中心, 不需要去檢索服務信息
fetch-registry: false
server:
# 測試時關閉自我保護機制,保證不可用服務及時踢出,,,若要及時踢出過期服務,還需要在客戶端進行配置
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
3.4 創建注冊中心啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args){
SpringApplication.run(EurekaServerApp.class, args);
}
}
3.5 啟動該服務,瀏覽器訪問
4. 創建member-service,會員接口項目
4.1 創建使用maven工程,略
4.2 pom.xml繼承父工作 springcloud-hystrix
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-hystrix</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>member-service</artifactId>
</project>
4.2 創建接口
qinfeng.zheng.api.service.MemberService
package qinfeng.zheng.api.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 創建時間: 13:31 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@RequestMapping("/member")
public interface MemberService {
@RequestMapping("/getMemberInfo")
String getMemberInfo(@RequestParam("username") String username);
}
5. 創建memebre-service-impl,會員服務的具體實現
5.1創建maven工程,略
5.2 pom.xml依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-hystrix</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>member-service-impl</artifactId>
<dependencies>
<!--依賴member-service工作,需要它的接口,減少重復代碼-->
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>member-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5.3 創建接口的具體實現邏輯
qinfeng.zheng.api.serice.impl.MemberServiceImpl
package qinfeng.zheng.api.serice.impl;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import qinfeng.zheng.api.service.MemberService;
/**
* 創建時間: 13:34 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@RestController
public class MemberServiceImpl implements MemberService{
@Override
public String getMemberInfo(@RequestParam("username") String username) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return username +",調用會員服務的getMemberInfo方法成功...";
}
}
5.4 創建application.yml文件
###服務啟動端口號
server:
port: 8001
###服務名稱(服務注冊到eureka名稱)
spring:
application:
name: client-member
###服務注冊到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
## 客戶端間隔30s重新讀eureka中服務信息
registry-fetch-interval-seconds: 30
# 心跳檢測檢測與續約時間
# 測試時將值設置設置小些,保證服務關閉后注冊中心能及時踢出服務
instance:
###Eureka客戶端向服務端發送心跳的時間間隔,單位為秒(客戶端告訴服務端自己會按照該規則)
lease-renewal-interval-in-seconds: 1
####Eureka服務端在收到最后一次心跳之后等待的時間上限,單位為秒,超過則剔除(客戶端告訴服務端按照此規則等待自己)
lease-expiration-duration-in-seconds: 2
5.5 創建服務啟動類
package qinfeng.zheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 創建時間: 13:35 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@SpringBootApplication
@EnableEurekaClient
public class MemberApp {
public static void main(String[] args){
SpringApplication.run(MemberApp.class, args);
}
}
5.6 啟動服務
5.6.1 查看注冊中心
5.6.2 接口測試
測試通過,服務正常!!!
6.創建order-service服務
創建過程與member-service一致.
接口方法如下:qinfeng.zheng.api.service.OrderService
package qinfeng.zheng.api.service;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 創建時間: 13:43 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@RequestMapping("/order")
public interface OrderService {
String getInfoFromMemberService(String username);
}
7.創建order-service-impl服務
7.1 創建maven工程,略
7.2 pom.xml依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-hystrix</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service-impl</artifactId>
<dependencies>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>order-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>qinfeng.zheng</groupId>
<artifactId>member-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
7.3 使用feign客戶端調用member服務接口
qinfeng.zheng.api.service.feign.MemberFeign
package qinfeng.zheng.api.service.feign;
import org.springframework.cloud.openfeign.FeignClient;
import qinfeng.zheng.api.service.MemberService;
import qinfeng.zheng.api.service.feign.fallback.MemberFallBack;
/**
* 創建時間: 14:01 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@FeignClient(name = "client-member",fallback = MemberFallBack.class)
public interface MemberFeign extends MemberService {
}
7.4 hystrix服務保護(使用繼承類的方式做服務保護)
qinfeng.zheng.api.service.feign.fallback.MemberFallBack
package qinfeng.zheng.api.service.feign.fallback;
import org.springframework.web.bind.annotation.RequestParam;
import qinfeng.zheng.api.service.feign.MemberFeign;
/**
* 創建時間: 14:46 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
public class MemberFallBack implements MemberFeign {
@Override
public String getMemberInfo(@RequestParam("username") String username) {
return "rpc掛了,還是走本地服務降級方法吧";
}
}
7.5 調用member服務的具體實現
qinfeng.zheng.api.service.impl.OrderServiceImpl
package qinfeng.zheng.api.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qinfeng.zheng.api.service.OrderService;
import qinfeng.zheng.api.service.feign.MemberFeign;
import qinfeng.zheng.api.service.feign.fallback.MemberFallBack;
/**
* 創建時間: 13:52 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@RestController
public class OrderServiceImpl extends MemberFallBack implements OrderService {
@Autowired
private MemberFeign memberFeign;
@Override
@RequestMapping("/getInfoFromMemberService") //映射url也可以放在接口方法上面
public String getInfoFromMemberService(String username) {
return memberFeign.getMemberInfo(username);
}
}
7.6 application.yml
###服務啟動端口號
server:
port: 8002
###服務名稱(服務注冊到eureka名稱)
spring:
application:
name: client-order
###服務注冊到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
## 客戶端間隔30s重新讀eureka中服務信息
registry-fetch-interval-seconds: 30
# 心跳檢測檢測與續約時間
# 測試時將值設置設置小些,保證服務關閉后注冊中心能及時踢出服務
instance:
###Eureka客戶端向服務端發送心跳的時間間隔,單位為秒(客戶端告訴服務端自己會按照該規則)
lease-renewal-interval-in-seconds: 1
####Eureka服務端在收到最后一次心跳之后等待的時間上限,單位為秒,超過則剔除(客戶端告訴服務端按照此規則等待自己)
lease-expiration-duration-in-seconds: 2
###SpringCloud Feign客戶端Http調用工具,默認已經整合了Ribbon負載均衡客戶端
###設置feign客戶端超時時間
ribbon:
###指的是建立連接所用的時間,適用於網絡狀況正常的情況下,兩端連接所用的時間。
ReadTimeout: 5000
###指的是建立連接后從服務器讀取到可用資源所用的時間。
ConnectTimeout: 5000
##開啟Hystrix斷路器
feign:
hystrix:
enabled: true
#### hystrix禁止服務超時時間,也就是說它不會走fallback方法,如果不配置這里,默認1秒內rpc不響應,就走fallback方法
hystrix:
command:
default:
execution:
# timeout:
### 直接將hystrix禁止服務超時時間禁掉, 這樣永遠不會走fallback方法了
# enabled: false
isolation:
thread:
#### 設置hystrix的超時時間, 如果2秒內rpc響應,瀏覽器正常響應,超過2秒走fallback方法
timeoutInMilliseconds: 2000
7.7 創建啟動類
qinfeng.zheng.OrderApp
package qinfeng.zheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 創建時間: 14:00 2018/9/16
* 修改時間:
* 編碼人員: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class OrderApp {
public static void main(String[] args){
SpringApplication.run(OrderApp.class, args);
}
}
7.8 啟動服務,測試
7.8.1 查看eureka注冊中心
7.8.2 接口測試
經測試,瀏覽器請求order服務的接口,走了服務降級方法,原因是:order-service-impl服務的application.yml中hystrix配置的超時時間是2000ms(它的意思是:如果調用服務接口時,2s內不能正常響應,那么就走本地的服務降級方法),而我們在member-service-impl服務的getMemberInfo接口內模擬業務邏輯,sleep(3000). 所以,對於order端而言,肯定超時,走服務降級方法.
如果我們將order-service-impl服務application.yml中超時時間改成5s,重啟服務,再測試
測試接口,響應正常的接口內容,正確!!!
至此,使用SpringCloud搭建微服務項目完成.