介紹
原文連結:https://bennyrhys.blog.csdn.net/article/details/111593251
技術之前有用eureka 現在用nacos工作流和gateway
介面資料流向資料表Eurak(發現)Eureka的作用114、物業 (註冊中心、心跳機制60s失效剔除)
沒有服務註冊於發現可以,但是會引來無窮無盡的麻煩靜態ip變更,影響多服務模組
架構Eurak Server程式碼新建moudle,和業務完全獨立pom依賴,最外層pomcloud版本號新建配置檔案註解啟動
驗證http://localhost:8000/
Eureka客戶端程式碼配置dom配置properties啟動client
利用Feign實現服務間呼叫介紹歷史netflex -> open (捐給spring cloud)
非常方便基於介面和註解,和本地方法一樣爽的http請求
程式碼價格中呼叫課程服務
引入依賴
#openfeign消費的負載均衡後期再配1
加註解
//啟動類的客戶端@EnableFeignClients12
客戶端(在呼叫類寫介面,複製被呼叫服務的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();}123456789101112131415161718
驗證pom中(自動引入其他服務的依賴)
controller(在price服務中呼叫course服務的方法)
驗證
利用Ribbon實現負載均衡修改配置檔案
price服務呼叫course服務的負載均衡設定
#openfeign消費的負載均衡course-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule12
利用Hystrix實現斷路器
比如獲取使用者資訊卡住,但資料庫的連線池一直未被釋放。系統崩潰斷路器保護,某一處出現問題,保證不影響全部不可用,避免故障蔓延
依賴pom
#斷路器 客戶端(預設關閉)feign.hystrix.enabled=true12
啟動類註解
@EnableCircuitBreaker1
斷路器實現類CourseListClientHystrix
package com.bennyrhys.course.client;import com.bennyrhys.entity.Course;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Component;/** * 描述: 斷路器實現類 */@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; }}12345678910111213141516171819202122232425
指明呼叫服務的斷路器類
/** * @Author bennyrhys * @Date 12/27/20 8:04 PM * 課程列表的Feign客戶端 */@FeignClient(value = "course-list", fallback = CourseListClientHystrix.class)@Primary //防止呼叫服務的controller爆紅線不好看public interface CourseListClient { @GetMapping("/course") List<Course> getList();}1234567891011
斷路器效果
整合兩個服務將課程列表和課程價格進行整合
返回實體CourseAndPrice
Integer id; Integer courseId; String name; Integer price;1234
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; }}12345678910111213141516171819
透過閘道器Zuul實現路由功能兩個特點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>12345678910111213141516171819
配置檔案
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-price1234567891011
啟動類 註解
package com.bennyrhys.course;import org.springframework.boot.SpringApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/** * 描述: 閘道器啟動類 */@EnableZuulProxy@SpringCloudApplicationpublic class ZuulGatewayApplication { public static void main(String[] args) { SpringApplication.run(ZuulGatewayApplication.class, args); }}1234567891011121314151617
效果圖
實現閘道器過濾器過濾前
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("過濾器已經記錄時間"); return null; }}123456789101112131415161718192021222324252627282930313233343536373839
過濾後
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; }}12345678910111213141516171819202122232425262728293031323334353637383940
uri:/bennyrhys/list/course,處理時長:919