亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringCloud開發(fā)課程查詢功能

 更新時間:2020年12月28日 10:54:09   作者:瑞 新  
這篇文章主要介紹了SpringCloud開發(fā)課程查詢功能,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

介紹

技術(shù)

在這里插入圖片描述
在這里插入圖片描述

之前有用eureka 現(xiàn)在用nacos
工作流和gateway

在這里插入圖片描述

接口數(shù)據(jù)流向

在這里插入圖片描述

數(shù)據(jù)表

在這里插入圖片描述在這里插入圖片描述

在這里插入圖片描述

新建項目

新建cloud-刪除src-新建modle

Eurak(發(fā)現(xiàn))

 Eureka的作用

114、物業(yè) (注冊中心、心跳機制60s失效踢除)

沒有服務(wù)注冊于發(fā)現(xiàn)可以,但是會引來無窮無盡的麻煩
靜態(tài)ip變更,影響多服務(wù)模塊

架構(gòu)

在這里插入圖片描述
在這里插入圖片描述

Eurak Server代碼

新建moudle,和業(yè)務(wù)完全獨立
pom依賴,最外層pomcloud版本號
新建配置文件
注解啟動

驗證http://localhost:8000/

在這里插入圖片描述

Eureka客戶端代碼

配置dom
配置properties
啟動client

在這里插入圖片描述

利用Feign實現(xiàn)服務(wù)間調(diào)用

介紹

歷史
netflex -> open (捐給spring cloud)

非常方便
基于接口和注解,和本地方法一樣爽的http請求

在這里插入圖片描述

代碼

價格中調(diào)用課程服務(wù)

引入依賴

<!--  openfeign -->  <dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-openfeign</artifactId>  </dependency>

配置文件

#openfeign消費的負載均衡后期再配

加注解

//啟動類的客戶端@EnableFeignClients

客戶端(在調(diào)用類寫接口,復(fù)制被調(diào)用服務(wù)的controller方法)

package com.bennyrhys.course.client;import com.bennyrhys.entity.Course;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import java.util.List;/** * @Author bennyrhys * @Date 12/27/20 8:04 PM * 課程列表的Feign客戶端 */@FeignClient("course-list")public interface CourseListClient { @GetMapping("/course") List<Course> getList();}

驗證pom中(自動引入其他服務(wù)的依賴)

在這里插入圖片描述

controller(在price服務(wù)中調(diào)用course服務(wù)的方法)

在這里插入圖片描述

驗證

在這里插入圖片描述

利用Ribbon實現(xiàn)負載均衡

在這里插入圖片描述

在這里插入圖片描述在這里插入圖片描述

修改配置文件

price服務(wù)調(diào)用course服務(wù)的負載均衡設(shè)置

#openfeign消費的負載均衡course-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

利用Hystrix實現(xiàn)斷路器

比如獲取用戶信息卡住,但數(shù)據(jù)庫的連接池一直未被釋放。系統(tǒng)崩潰
斷路器保護,某一處出現(xiàn)問題,保證不影響全部不可用,避免故障蔓延

在這里插入圖片描述
依賴pom

<!--  斷路器 客戶端-->  <dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>  </dependency>

配置

#斷路器 客戶端(默認關(guān)閉)feign.hystrix.enabled=true

啟動類注解

@EnableCircuitBreaker

斷路器實現(xiàn)類CourseListClientHystrix

package com.bennyrhys.course.client;import com.bennyrhys.entity.Course;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Component;/** * 描述:  斷路器實現(xiàn)類 */@Componentpublic class CourseListClientHystrix implements CourseListClient{ @Override public List<Course> getList() {  List<Course> defaultCourses = new ArrayList<>();  Course course = new Course();  course.setId(1);  course.setCourseId(1);  course.setCourseName("默認課程");  course.setValid(1);  defaultCourses.add(course);  return defaultCourses; }}

指明調(diào)用服務(wù)的斷路器類

/** * @Author bennyrhys * @Date 12/27/20 8:04 PM * 課程列表的Feign客戶端 */@FeignClient(value = "course-list", fallback = CourseListClientHystrix.class)@Primary //防止調(diào)用服務(wù)的controller爆紅線不好看public interface CourseListClient { @GetMapping("/course") List<Course> getList();}

斷路器效果

在這里插入圖片描述

整合兩個服務(wù)

將課程列表和課程價格進行整合

返回實體CourseAndPrice

 Integer id; Integer courseId; String name; Integer price;

service

 @Override public List<CourseAndPrice> getCoursesAndPrice() {  List<CourseAndPrice> courseAndPriceList = new ArrayList<>();  List<Course> courses = courseListClient.courseList();  for (int i = 0; i < courses.size(); i++) {   Course course = courses.get(i);   if (course != null) {    CoursePrice coursePrice = getCoursePrice(course.getCourseId());    CourseAndPrice courseAndPrice = new CourseAndPrice();    courseAndPrice.setPrice(coursePrice.getPrice());    courseAndPrice.setName(course.getCourseName());    courseAndPrice.setId(course.getId());    courseAndPrice.setCourseId(course.getCourseId());    courseAndPriceList.add(courseAndPrice);   }  }  return courseAndPriceList; }}

在這里插入圖片描述

通過網(wǎng)關(guān)Zuul實現(xiàn)路由功能

兩個特點

在這里插入圖片描述

Zuul集成

在這里插入圖片描述

新建mudle模塊sourse-zuul

引入依賴

<dependencies> <dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> <build> <plugins>  <plugin>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>

配置文件

spring.application.name=course-gatewayserver.port=9000logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}mybatis.configuration.map-underscore-to-camel-case=trueeureka.client.service-url.defaultZone=http://localhost:8000/eureka/#zuul.prefix=/bennyrhyszuul.routes.course-list.path=/list/**zuul.routes.course-list.service-id=course-listzuul.routes.course-price.path=/price/**zuul.routes.course-price.service-id=course-price

啟動類 注解

package com.bennyrhys.course;import org.springframework.boot.SpringApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/** * 描述:  網(wǎng)關(guān)啟動類 */@EnableZuulProxy@SpringCloudApplicationpublic class ZuulGatewayApplication { public static void main(String[] args) {  SpringApplication.run(ZuulGatewayApplication.class, args); }}

效果圖

在這里插入圖片描述

實現(xiàn)網(wǎng)關(guān)過濾器

在這里插入圖片描述

過濾前

package com.bennyrhys.course.filter;import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;import org.springframework.stereotype.Component;/** * 描述:  記錄請求時間 */@Componentpublic class PreRequestFilter extends ZuulFilter { @Override public String filterType() {  //過濾器的類型  return FilterConstants.PRE_TYPE; } @Override public int filterOrder() {  return 0; } @Override public boolean shouldFilter() {  //是否啟用過濾器  return true; } @Override public Object run() throws ZuulException {  RequestContext currentContext = RequestContext.getCurrentContext();  currentContext.set("startTime", System.currentTimeMillis());  System.out.println("過濾器已經(jīng)記錄時間");  return null; }}

過濾后

package com.bennyrhys.course.filter;import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import org.springframework.cache.annotation.Cacheable;import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;import org.springframework.stereotype.Component;/** * 描述:  請求處理后的過濾器 */@Componentpublic class PostRequestFilter extends ZuulFilter { @Override public String filterType() {  return FilterConstants.POST_TYPE; } @Override public int filterOrder() {  return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; } @Override public boolean shouldFilter() {  return true; } @Override public Object run() throws ZuulException {  RequestContext currentContext = RequestContext.getCurrentContext();  Long startTime = (Long) currentContext.get("startTime");  long duration = System.currentTimeMillis() - startTime;  String requestURI = currentContext.getRequest().getRequestURI();  System.out.println("uri:" + requestURI + ",處理時長:" + duration);  return null; }}

uri:/bennyrhys/list/course,處理時長:919

到此這篇關(guān)于SpringCloud開發(fā)課程查詢功能的文章就介紹到這了,更多相關(guān)SpringCloud課程查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java @Transactional與synchronized使用的問題

    Java @Transactional與synchronized使用的問題

    這篇文章主要介紹了Java @Transactional與synchronized使用的問題,了解內(nèi)部原理是為了幫助我們做擴展,同時也是驗證了一個人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的
    2023-01-01
  • java實現(xiàn)屏蔽詞功能

    java實現(xiàn)屏蔽詞功能

    這篇文章主要介紹了java實現(xiàn)屏蔽詞功能,類似貼吧里面屏蔽各種用戶的發(fā)帖內(nèi)容,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Mybatis-plus實現(xiàn)join連表查詢的示例代碼

    Mybatis-plus實現(xiàn)join連表查詢的示例代碼

    mybatis-plus在連表查詢上是不行的,如果需要連表查詢,就得乖乖的去寫xml文件了,本文介紹了mybatis-plus-join框架,它支持連表查詢,感興趣的可以了解一下
    2023-08-08
  • java的main方法中調(diào)用spring的service方式

    java的main方法中調(diào)用spring的service方式

    這篇文章主要介紹了在java的main方法中調(diào)用spring的service方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • IDEA搭建dubbo項目的過程及存在的問題

    IDEA搭建dubbo項目的過程及存在的問題

    這篇文章主要介紹了IDEA搭建dubbo項目及存在的問題小結(jié),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • 詳解Java目錄操作與文件操作教程

    詳解Java目錄操作與文件操作教程

    本章具體介紹了目錄操作、文件操作的基本使用方法和常用函數(shù),圖解穿插代碼實現(xiàn),感興趣的朋友來看看吧
    2022-03-03
  • 實例解析Java的Jackson庫中的數(shù)據(jù)綁定

    實例解析Java的Jackson庫中的數(shù)據(jù)綁定

    這篇文章主要介紹了Java的Jackson庫中的數(shù)據(jù)綁定,這里分為通常的簡單數(shù)據(jù)綁定與全數(shù)據(jù)綁定兩種情況來講,需要的朋友可以參考下
    2016-01-01
  • Java利用多線程復(fù)制文件

    Java利用多線程復(fù)制文件

    這篇文章主要介紹了Java利用多線程復(fù)制文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 關(guān)于Spring?Boot內(nèi)存泄露排查的記錄

    關(guān)于Spring?Boot內(nèi)存泄露排查的記錄

    這篇文章主要介紹了關(guān)于Spring?Boot內(nèi)存泄露排查的記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Springboot中PropertySource的結(jié)構(gòu)與加載過程逐步分析講解

    Springboot中PropertySource的結(jié)構(gòu)與加載過程逐步分析講解

    本文重點講解一下Spring中@PropertySource注解的使用,PropertySource主要是對屬性源的抽象,包含屬性源名稱name和屬性源內(nèi)容對象source。其方法主要是對這兩個字段進行操作
    2023-01-01

最新評論