eureka+zuul
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp06-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp06-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring:
application:
name: zuul
# eureka2001 3001 3001 4001 5001 6001
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
zuul:
routes:
# **包含深层子路径
# *只包含一层路径
# service-id 作为访问子路径,是默认设置
# 根据注册表中的注册信息,zuul可以自动配置,防止注册表不全
# 最好自己手动配置
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
@EnableZuulProxy
package cn.tedu.sp06;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class Sp06ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(Sp06ZuulApplication.class, args);
}
}
### 获取订单的商品列表
GET http://localhost:8001/ty4g33t3
### 减少商品库存
POST http://localhost:8001/decreaseNumber
Accept: application/json
Content-Type: application/json
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]
### 获取用户
GET http://localhost:8101/7
###
GET http://localhost:8101/8
###
GET http://localhost:8101/9
###
GET http://localhost:8101/10
### 增加积分
GET http://localhost:8101/8/score?score=1000
### 获取订单
GET http://localhost:8201/iujhygf435tg
### 添加订单
GET http://localhost:8201/add
# ---------------------------------------------------------------------------------
### 获取订单的商品列表
GET http://localhost:3001/item-service/ty4g33t3
### 减少商品库存
POST http://localhost:3001/item-service/decreaseNumber
Accept: application/json
Content-Type: application/json
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]
### 获取用户
GET http://localhost:3001/user-service/7
###
GET http://localhost:3001/user-service/8
###
GET http://localhost:3001/user-service/9
###
GET http://localhost:3001/user-service/10
### 增加积分
GET http://localhost:3001/user-service/8/score?score=1000
### 获取订单
GET http://localhost:3001/order-service/iujhygf435tg
### 添加订单
GET http://localhost:3001/order-service/add
package cn.tedu.sp06.filter;
import cn.tedu.web.util.JsonResult;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class AccessFilter extends ZuulFilter {
/*设置过滤器的类型 pre routing post error*/
@Override
public String filterType() {
// return "pre";
return FilterConstants.PRE_TYPE;
}
/*设置过滤器添加的顺序号 前置过滤器中有五个默认过滤器 自定义过滤器放在末尾 位置为6*/
@Override
public int filterOrder() {
return 6;
}
/*针对当前请求判断是否要执行下面的过滤代码*/
@Override
public boolean shouldFilter() {
/*调用商品需要判断权限 * 调用用户/订单不检查权限(这里只演示实现,不考虑业务逻辑) * */
//1.获得一个请求上下文对象
RequestContext ctx=RequestContext.getCurrentContext();
//2.从上下文对象获得调用后台服务的 serviceid 类型要转成字符串类型
String serviceId = (String)ctx.get(FilterConstants.SERVICE_ID_KEY);//"serviceId"
//3.如果调用了item-service 返回true
return "item-service".equals(serviceId);
}
/*过滤代码,检查权限*/
@Override
public Object run() throws ZuulException {
//1.获得上下文对象
RequestContext ctx=RequestContext.getCurrentContext();
//2.获得Request对象
HttpServletRequest request = ctx.getRequest();
//3.接收token参数
String token = request.getParameter("token");
//4.如果token不存在: ①null ②"" ③" "
if(StringUtils.isBlank(token)){ //区别idEmpty()
//5.阻止继续调用
ctx.setSendZuulResponse(false);//发送zuul响应关闭
//6.直接返回响应 提示未登录
//JsonResult--{code:400,msg:未登录,data:null}
String json=JsonResult.build().code(400).msg("not log in").toString();
//响应协议头
ctx.addZuulResponseHeader("Content-Type", "application/json;charset=UTF-8");
//响应协议体
ctx.setResponseBody(json);
}
return null;//zuul当前版本这个返回值不起任何作用
}
}
设置过滤器的类型
实现效果
xml <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency>
spring:
application:
name: zuul
# eureka2001 3001 3001 4001 5001 6001
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
zuul:
routes:
# **包含深层子路径
# *只包含一层路径
# service-id 作为访问子路径,是默认设置
# 根据注册表中的注册信息,zuul可以自动配置,防止注册表不全
# 最好自己手动配置
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
retryable: true
# 对所有服务都有效的通用配置
ribbon:
MaxAutoRetries: 1
# 对某一个单独配置
item-service:
ribbon:
MaxAutoRetries: 0
package cn.tedu.sp06.fb;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Component
public class itemFB implements FallbackProvider {
/*设置针对哪个后台服务进行降级 * -item-service只针对商品服务降级 * *:对所有服务都应用当前降级类 * null:对所有服务都应用当前降级类 * */
@Override
public String getRoute() {
return "item-service";
}
/*向客户端返回的响应数据*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override/*返回状态码对象*/
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
@Override/*状态码的码*/
public int getRawStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
@Override/*状态码的文本*/
public String getStatusText() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
}
@Override
public void close() {
//用来关闭流
//BAIS不占用底层系统资源,不需要关闭
}
@Override/*协议体*/
public InputStream getBody() throws IOException {
//JsonResult -{code msg data}
String json = JsonResult.build().code(500).msg("后台服务出错请稍后重试").toString();
return new ByteArrayInputStream(json.getBytes("UTF-8"));
}
@Override/*协议头*/
public HttpHeaders getHeaders() {
HttpHeaders h = new HttpHeaders();
h.add("Content-Type", "application/json;charset=UTF-8");
return h;
}
};
}
}
package cn.tedu.sp06.fb;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@Component
public class orderFB implements FallbackProvider {
/*设置针对哪个后台服务进行降级 * -item-service只针对商品服务降级 * *:对所有服务都应用当前降级类 * null:对所有服务都应用当前降级类 * */
@Override
public String getRoute() {
return "order-service";
}
/*向客户端返回的响应数据*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override/*返回状态码对象*/
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
@Override/*状态码的码*/
public int getRawStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
@Override/*状态码的文本*/
public String getStatusText() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
}
@Override
public void close() {
//用来关闭流
//BAIS不占用底层系统资源,不需要关闭
}
@Override/*协议体*/
public InputStream getBody() throws IOException {
//JsonResult -{code msg data}
String json = JsonResult.build().code(500).msg("后台服务出错请稍后重试").toString();
return new ByteArrayInputStream(json.getBytes("UTF-8"));
}
@Override/*协议头*/
public HttpHeaders getHeaders() {
HttpHeaders h = new HttpHeaders();
h.add("Content-Type", "application/json;charset=UTF-8");
return h;
}
};
}
}
实现效果
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
actuator依赖于zuul
m.e.w.e.i="*" # *:暴露所有监控指标
m.e.w.e.i=health,beans,mappings # *:暴露指定监控指标
m.e.w.e.i=hystrix.stream
***********************************************************
#暴露actuator的监控指标
management:
endpoints:
web:
exposure:
include: "*" #暴露所有监控
http://localhost:3001/actuator
### actuator
GET http://localhost:3001/actuator
###
GET http://localhost:3001/actuator/health
###
GET http://localhost:3001/actuator/info
###
GET http://localhost:3001/actuator/beans
###
GET http://localhost:3001/actuator/mappings
###
GET http://localhost:3001/actuator/heapdump
###
GET http://localhost:3001/actuator/hystrix.stream
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp07-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp07-hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<mainClass>cn.tedu.sp07.Sp07HystrixDashboardApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
server:
port: 4001
#允许抓取日志的服务器列表
hystrix:
dashboard:
proxy-stream-allow-list: localhost
@EnableHystrixDashboard
package cn.tedu.sp07;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class Sp07HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(Sp07HystrixDashboardApplication.class, args);
}
}
http://localhost:4001/hystrix
http://localhost:3001/actuator/hystrix.stream
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_55740233/article/details/121370733
内容来源于网络,如有侵权,请联系作者删除!