【SpringCloud】SpringCloud开发课程查询功能
source link: https://blog.csdn.net/weixin_43469680/article/details/111593251
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
之前有用eureka 现在用nacos
工作流和gateway
接口数据流向
新建cloud-删除src-新建modle
Eurak(发现)
Eureka的作用
114、物业 (注册中心、心跳机制60s失效踢除)
没有服务注册于发现可以,但是会引来无穷无尽的麻烦
静态ip变更,影响多服务模块
Eurak Server代码
新建moudle,和业务完全独立
pom依赖,最外层pomcloud版本号
新建配置文件
注解启动
验证http://localhost:8000/
Eureka客户端代码
配置dom
配置properties
启动client
利用Feign实现服务间调用
历史
netflex -> open (捐给spring cloud)
非常方便
基于接口和注解,和本地方法一样爽的http请求
价格中调用课程服务
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
#openfeign消费的负载均衡后期再配
//启动类的客户端
@EnableFeignClients
客户端(在调用类写接口,复制被调用服务的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中(自动引入其他服务的依赖)
controller(在price服务中调用course服务的方法)
验证
利用Ribbon实现负载均衡
修改配置文件
price服务调用course服务的负载均衡设置
#openfeign消费的负载均衡
course-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
利用Hystrix实现断路器
比如获取用户信息卡住,但数据库的连接池一直未被释放。系统崩溃
断路器保护,某一处出现问题,保证不影响全部不可用,避免故障蔓延
依赖pom
<!-- 断路器 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
#断路器 客户端(默认关闭)
feign.hystrix.enabled=true
启动类注解
@EnableCircuitBreaker
断路器实现类CourseListClientHystrix
package com.bennyrhys.course.client;
import com.bennyrhys.entity.Course;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
/**
* 描述: 断路器实现类
*/
@Component
public 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;
}
}
指明调用服务的断路器类
/**
* @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();
}
断路器效果
整合两个服务
将课程列表和课程价格进行整合
返回实体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;
}
}
通过网关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>
spring.application.name=course-gateway
server.port=9000
logging.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=true
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#zuul.prefix=/bennyrhys
zuul.routes.course-list.path=/list/**
zuul.routes.course-list.service-id=course-list
zuul.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;
/**
* 描述: 网关启动类
*/
@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
效果图
实现网关过滤器
过滤前
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;
/**
* 描述: 记录请求时间
*/
@Component
public 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;
}
}
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;
/**
* 描述: 请求处理后的过滤器
*/
@Component
public 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
Recommend
-
57
目前,很多妈妈们正在翻看自己孩子的接种疫苗记录,是因为,前不久国内第二大疫苗生产企业,长春长生生物公司疫苗记录造假被曝光。看到两天前阿里健康上线扫码排查问题疫苗功能后,一些妈妈如是反馈。“我的药盒已经丢了,怎么查询问题疫苗?”“疫苗...
-
57
FreeSql FreeSql 是一个功能强大的 NETStandard 库,用于对象关系映射程序(O/RM),提供了 CodeFirst/DbFirst/CURD/表达式函数/读写分离 等基础封装。
-
17
作者:小傅哥 博客: https://bugstack.cn 沉淀、分享、成长,让自己和他人都能有所收获! 一、前言介绍 微服务不是泥球小单体,而是具备更加清晰职责边界的...
-
11
12分钟视频演示springcloud微服务模块的开发过程 全栈技术...
-
4
电商的快递查询功能 薄荷商店在搭建物流信息系统还算顺利,把整个搭建过程中的所思所想写下来,供大家参考。 物流查询系统要保证 信息更新及时,最好以小时为单位更新。(及时) 所有的快递公司都覆盖。(数据一致)
-
6
晓说区块链 | 区块链未提供遍历查询功能,是出于怎样的设计理念? ...
-
5
腾讯出行服务上线ETC账单查询功能,每一笔费用都清清楚楚-品玩 腾讯出行服务上线ETC账单查询功能,每一笔费用都清清楚楚 13小时前 近日,腾讯与交通运输部路网监测和应急处置中心达成合作,在微信小程序“腾讯出行服务”中推出了“ETC账单查询”功...
-
3
SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin...
-
2
SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Promet...
-
3
大家好,今天我们继续来分享一个在项目开发过程中遇到的实际问题,这里也来梳理并总结一下我们是如何对它进行持续优化的,希望能对大家有所帮助。 分类树...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK