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 = "https://www.cnblogs.com/xiaoqigui/archive/2022/10/26/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 = "https://www.cnblogs.com/xiaoqigui/archive/2022/10/26/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 有服務降級,也有服務熔斷 時
觸發服務熔斷;

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520610.html
標籤:其他
