最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

介紹

技術(shù)

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

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

在這里插入圖片描述

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

在這里插入圖片描述

數(shù)據(jù)表

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

在這里插入圖片描述

新建項(xiàng)目

新建cloud-刪除src-新建modle

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

 Eureka的作用

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

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

架構(gòu)

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

Eurak Server代碼

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

驗(yàn)證http://localhost:8000/

在這里插入圖片描述

Eureka客戶端代碼

配置dom
配置properties
啟動(dòng)client

在這里插入圖片描述

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

介紹

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

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

在這里插入圖片描述

代碼

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

引入依賴

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

配置文件

#openfeign消費(fèi)的負(fù)載均衡后期再配

加注解

//啟動(dòng)類的客戶端@EnableFeignClients

客戶端(在調(diào)用類寫(xiě)接口,復(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();}

驗(yàn)證pom中(自動(dòng)引入其他服務(wù)的依賴)

在這里插入圖片描述

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

在這里插入圖片描述

驗(yàn)證

在這里插入圖片描述

利用Ribbon實(shí)現(xiàn)負(fù)載均衡

在這里插入圖片描述

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

修改配置文件

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

#openfeign消費(fèi)的負(fù)載均衡course-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

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

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

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

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

配置

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

啟動(dòng)類注解

@EnableCircuitBreaker

斷路器實(shí)現(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;/** * 描述:  斷路器實(shí)現(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("默認(rèn)課程");  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();}

斷路器效果

在這里插入圖片描述

整合兩個(gè)服務(wù)

將課程列表和課程價(jià)格進(jìn)行整合

返回實(shí)體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; }}

在這里插入圖片描述

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

兩個(gè)特點(diǎ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

啟動(dòng)類 注解

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)啟動(dòng)類 */@EnableZuulProxy@SpringCloudApplicationpublic class ZuulGatewayApplication { public static void main(String[] args) {  SpringApplication.run(ZuulGatewayApplication.class, args); }}

效果圖

在這里插入圖片描述

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

在這里插入圖片描述

過(guò)濾前

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;/** * 描述:  記錄請(qǐng)求時(shí)間 */@Componentpublic class PreRequestFilter extends ZuulFilter { @Override public String filterType() {  //過(guò)濾器的類型  return FilterConstants.PRE_TYPE; } @Override public int filterOrder() {  return 0; } @Override public boolean shouldFilter() {  //是否啟用過(guò)濾器  return true; } @Override public Object run() throws ZuulException {  RequestContext currentContext = RequestContext.getCurrentContext();  currentContext.set("startTime", System.currentTimeMillis());  System.out.println("過(guò)濾器已經(jīng)記錄時(shí)間");  return null; }}

過(guò)濾后

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;/** * 描述:  請(qǐng)求處理后的過(guò)濾器 */@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 + ",處理時(shí)長(zhǎng):" + duration);  return null; }}

uri:/bennyrhys/list/course,處理時(shí)長(zhǎng):919

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

相關(guān)文章

  • Spring Bean裝載方式代碼實(shí)例解析

    Spring Bean裝載方式代碼實(shí)例解析

    這篇文章主要介紹了Spring Bean裝載方式代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java垃圾回收機(jī)制算法詳解

    Java垃圾回收機(jī)制算法詳解

    這篇文章主要介紹了Java垃圾回收機(jī)制算法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java并發(fā)內(nèi)存模型詳情

    Java并發(fā)內(nèi)存模型詳情

    這篇文章主要介紹了Java并發(fā)內(nèi)存模型,Java是一門(mén)支持多線程執(zhí)行的語(yǔ)言,要編寫(xiě)正確的并發(fā)程序,了解Java內(nèi)存模型是重要前提。而了解硬件內(nèi)存模型有助于理解程序的執(zhí)行,下面文章就來(lái)看看詳細(xì)內(nèi)容吧
    2021-10-10
  • Java搭建RabbitMq消息中間件過(guò)程詳解

    Java搭建RabbitMq消息中間件過(guò)程詳解

    這篇文章主要介紹了Java搭建RabbitMq消息中間件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java刪除指定文件夾下的所有內(nèi)容的方法(包括此文件夾)

    Java刪除指定文件夾下的所有內(nèi)容的方法(包括此文件夾)

    下面小編就為大家?guī)?lái)一篇Java刪除指定文件夾下的所有內(nèi)容的方法(包括此文件夾) 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • 詳解Java8新特性如何防止空指針異常

    詳解Java8新特性如何防止空指針異常

    要說(shuō) Java 編程中哪個(gè)異常是你印象最深刻的,那 NullPointerException 空指針可以說(shuō)是臭名昭著的,不要說(shuō)初級(jí)程序員會(huì)碰到, 即使是中級(jí),專家級(jí)程序員稍不留神,就會(huì)掉入這個(gè)坑里,本文就和大家聊聊Java8新特性如何防止空指針異常
    2023-08-08
  • SpringBoot面試突擊之過(guò)濾器和攔截器區(qū)別詳解

    SpringBoot面試突擊之過(guò)濾器和攔截器區(qū)別詳解

    過(guò)濾器(Filter)和攔截器(Interceptor)都是基于?AOP(Aspect?Oriented?Programming,面向切面編程)思想實(shí)現(xiàn)的,用來(lái)解決項(xiàng)目中某一類問(wèn)題的兩種“工具”,但二者有著明顯的差距,接下來(lái)我們一起來(lái)看
    2022-10-10
  • Java如何在不存在文件夾的目錄下創(chuàng)建文件

    Java如何在不存在文件夾的目錄下創(chuàng)建文件

    這篇文章主要介紹了Java如何在不存在文件夾的目錄下創(chuàng)建文件,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-08-08
  • Java方法及數(shù)組相關(guān)原理解析

    Java方法及數(shù)組相關(guān)原理解析

    這篇文章主要介紹了Java方法及數(shù)組相關(guān)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化

    如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化

    今天帶大家學(xué)習(xí)如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化,文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

定日县| 临西县| 个旧市| 黔西县| 乌海市| 叙永县| 长岛县| 高台县| 同江市| 大同市| 古浪县| 昌乐县| 甘南县| 景宁| 错那县| 綦江县| 来安县| 阿巴嘎旗| 平乡县| 美姑县| 蓬莱市| 鹿邑县| 新泰市| 吉首市| 砚山县| 伊春市| 嵩明县| 遂平县| 寻甸| 郑州市| 塔城市| 年辖:市辖区| 沛县| 安龙县| 怀安县| 开远市| 明星| 达日县| 桂东县| 宝兴县| 渭源县|