1、項目模塊介紹 2、 父項目 主要依賴 spring-cloud 的 版本控制 <properties> <!-- springCloud 版本 --> <scd.version>Dalston.SR4</scd.version> </properties> <dependencyManageme ...
1、項目模塊介紹
2、 父項目
主要依賴 spring-cloud 的 版本控制
<properties>
<!-- springCloud 版本 -->
<scd.version>Dalston.SR4</scd.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${scd.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3、 eureka 模塊
3.1 主要依賴
<!-- eureka 註冊中心 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
3.2 配置信息
# 埠
server:
port: 8096
# 服務名
spring:
application:
name: edocmall-eureka
# eureka 服務註冊與發現 配置
eureka:
client:
#Eureka 監控頁面
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka
register-with-eureka: false # 是否註冊自己的服務到註冊中心,預設為true
on-demand-update-status-change: false # 是否主動拉取其他註冊的服務信息,預設也是true
3.3 主啟動類上的註解
@EnableEurekaServer //eureka服務端啟動,可以就接受別人來註冊
3.4 測試 訪問
啟動項目,訪問 http://localhost:8096
4、server 服務提供模塊
4.1 主要依賴
<!-- eureka 客戶端依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
4.2 配置信息
# 埠
server:
port: 8097
# 服務名配置,eureka註冊信息,服務調用基於服務名,必須增加
spring:
application:
name: edocmall-server
#數據源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kh96_springboot_edocbd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
username: root
password: root
# mybatis-plus 配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
# eureka 註冊中心的配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8096/eureka # 註冊中心的地址
# 關閉自我保護機制,保證不可用的服務及時剔除
server:
enable-self-preservation: false
4.3 代碼介紹
4.4 主啟動類上的註解
@MapperScan("com.kgc.scd.mapper")
@EnableEurekaClient // 開啟 eureka 服務註冊,將此服務註冊到 eureka中
4.5 請求測試
服務提供端的請求最好先單獨測試一下,成功後再進行遠程調用;
5、 web 服務消費模塊
5.1 使用restTemplate 調用
5.1.1 主啟動類 向容器中放入 restTemplate
@SpringBootApplication
public class Edocmall96WebApplication {
public static void main(String[] args) {
SpringApplication.run(Edocmall96WebApplication.class, args);
}
//往容器中添加 restTemplate
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.1.2 請求中直接調用具體url下的請求
@RestController
public class WebEntryController {
@Autowired
private RestTemplate restTemplate;
// 根據文檔編號,獲取文檔詳情
@GetMapping("/entryById")
public RequestResult<EdocEntryVO> entryDetail(Integer id){
log.info("------ 根據文檔編號:{},獲取文檔詳情 ------",id);
//模擬發送http請求請求server端,獲取文檔詳情
//弊端:消費端,必須在程式內,記錄提供者的ip地址,如果地址出現變更,還需要計時更新,如果服務者有多個及其,無法實現負載均衡
EdocEntryVO edocEntryVO = restTemplate.getForObject("http://127.0.0.1:8097/entry?id="+id,EdocEntryVO.class);
return ResultBuildUtil.success(edocEntryVO);
}
}
5.1.3 請求測試
5.2 使用 feign 遠程調用
5.2.1 主要依賴
<!-- eureka 客戶端依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign遠程調用依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
5.2.2 配置信息
# 埠
server:
port: 8098
spring:
application:
name: edocmall-web
# 服務名
# eureka 註冊中心的配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8096/eureka
# 關閉自我保護機制,保證不可用的服務及時剔除
server:
enable-self-preservation: false
# 開啟客戶端服務降級,預設是關閉的,需要打開
feign:
hystrix:
enabled: true
5.2.3 代碼介紹
5.2.3.1 業務層介面
//指定服務名
@FeignClient(value = "edocmall-server")
public interface WebEntryService {
//基於feign 遠程調用edoc-server服務端提供的查詢文檔詳情介面
@GetMapping("/entry") //必須跟遠程請求一直
EdocEntryVO invokeEntryServiceUserFeign(@RequestParam("id") Integer id); //必須寫 @RequestParam("id")
// 註意 一點更要寫 @RequestParam("id") 裡面的參數必須寫
}
5.2.3.1 控制層之直接調用介面
@RestController
public class WebEntryController {
@Autowired
private WebEntryService webEntryService;
// 根據文檔編號,獲取文檔詳情
@GetMapping("/entryById")
public RequestResult<EdocEntryVO> entryDetail(Integer id){
log.info("------ 根據文檔編號:{},獲取文檔詳情 ------",id);
//基於feign 遠程調用服務端介面,獲取文檔詳情
EdocEntryVO edocEntryVO = webEntryService.invokeEntryServiceUserFeign(id);
return ResultBuildUtil.success(edocEntryVO);
}
}
5.2.4 主啟動類上的註解
@EnableEurekaClient // 開啟 eureka 服務註冊,將此服務註冊到 eureka中
@EnableFeignClients //開啟 feign 遠程調用服務
5.2.5 請求測試
eureka 註冊中心:
feign遠程調用:
6、Feign的服務降級與熔斷
6.1 服務降級(服務消費端)
6.1.1 依賴
由於feign中有hystrix的依賴,所以不用單獨添加;
6.1.2 介面指定服務降級後的實現類
@FeignClient(value = "edocmall-server",fallback = WebEntryServiceImpl.class) //fallback 指定調用失敗後 降級調用的數據
public interface WebEntryService {
......
}
6.1.3 實現類,具體降級服務操作
@Service
public class WebEntryServiceImpl implements WebEntryService {
@Override
public EdocEntryVO invokeEntryServiceUserFeign(Integer id) {
//此方法,就是regn遠程調用,觸發服務剪輯的預設實現,正常請求不會調用
//只用遠程 feign調用失敗,才會調用
EdocEntryVO edocEntryVO = new EdocEntryVO();
edocEntryVO.setId(999);
edocEntryVO.setCid(0);
edocEntryVO.setTitle("觸發熔斷服務降級");
edocEntryVO.setSummary("當feign遠程調用介面失敗,默的預設實現");
edocEntryVO.setUploadUser("feign-hystrix");
edocEntryVO.setCreateDate(new Date());
return edocEntryVO;
}
6.1.4 測試
6.1.4.1 不添加服務降級時
6.1.4.2 添加服務降級後
6.2 服務熔斷(服務提供端)
6.2.1 依賴
<!--導入Hystrix依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
6.2.2 熔斷 備選方法 和 熔斷觸發條件
@Slf4j
@RestController
public class EntryController {
@Autowired
private EntryService entryService;
//根據 id 查詢文檔詳情
@GetMapping("/entry")
@HystrixCommand(fallbackMethod = "fallbackStoryDetail") //指定 服務熔斷後的備選方法
public EdocEntryVO entryDetail(@RequestParam Integer id){
log.info("------ 根據文檔編號:{},獲取文檔詳情 ------",id);
EdocEntryVO edocEntry = entryService.getEdocEntryById(id);
if (edocEntry == null){
//拋出異常,觸發 熔斷 備選方法
throw new RuntimeException("id為:"+id+"的用戶不存在,觸發服務熔斷");
}
return edocEntry;
}
// 根據 id 查詢文檔詳情 方法 服務熔斷後 的備選方案
public EdocEntryVO fallbackStoryDetail(@RequestParam Integer id){
//此方法,只用服務熔斷時,才會被調用
EdocEntryVO edocEntryVO = new EdocEntryVO();
edocEntryVO.setId(999);
edocEntryVO.setCid(0);
edocEntryVO.setTitle("根據 id 查詢文檔詳情 方法 服務熔斷後 的備選方案");
edocEntryVO.setSummary("當根據id 查詢不到具體用戶信息時,就會觸發");
edocEntryVO.setUploadUser("hystrix");
edocEntryVO.setCreateDate(new Date());
return edocEntryVO;
}
}
6.2.3 主啟動類上的註解
@EnableHystrix //開啟熔斷服務 舊的開啟服務熔斷註解: @EnableCircuitBreaker
6.2.4 測試
6.2.4.1 沒有服務降級,也沒有服務熔斷 時
直接返回錯誤;
6.2.4.2 有服務降級,沒有服務熔斷 時
觸發服務降級;
6.2.4.3 有服務降級,也有服務熔斷 時
觸發服務熔斷;