搭建eureka注册中心服务器
父项目
添加 eureka server 依赖
yml
关闭自我保护模式
主机名
针对单台服务器,不向自己注册,不从自己拉取
启动类注解:@EnableEurekaServer
触发 eureka 服务器的自动配置
eureka四条运行机制
客户端连接 eureka 注册中心
修改 2,3,4 项目
添加 eureka client 依赖
1.
yml
eureka连接地址: http://eureka1:2001/eureka
修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
1.pom.xml 添加eureka依赖
2.application.yml 添加eureka注册配置
3.主程序启用eureka客户端
4.启动服务,在eureka中查看注册信息
右键点击项目,或点击pom.xml,用 STS 工具编辑起步依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒
defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示 eureka 服务器的部署位置, 需要云服务器提供
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
修改 item-service、user-service 和 order-service,
主程序添加
@EnableDiscoveryClient
注解
http://eureka1:2001
02商品服务
--server.port=xxxx
启动参数 --server.port 可以覆盖yml中的端口配置
item-service-8001
–server.port=8001
item-service-8002
–server.port=8002
访问 eureka 查看 item-service 注册信息
http://localhost:8001/35
http://localhost:8002/35
添加两个服务器的 profile 配置文件
eureka:
instance:
hostname: eureka1
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://eureka2:2002/eureka #eureka1启动时向eureka2注册
eureka:
instance:
hostname: eureka2
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://eureka1:2001/eureka #eureka2启动时向eureka1注册
eureka1 启动参数:
–spring.profiles.active=eureka1 --server.port=2001
eureka2 启动参数:
–spring.profiles.active=eureka2 --server.port=2002
访问 eureka 服务器,查看注册信息
http://eureka1:2001/
http://eureka2:2002/
修改以下微服务
sp02-itemservice
sp03-userservice
sp04-orderservice
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
当一个 eureka 服务宕机时,仍可以连接另一个 eureka 服务
源码: https://github.com/benwang6/spring-cloud-repo 5484
zuul API 网关,为微服务应用提供统一的对外访问接口。
zuul 还提供过滤器,对所有微服务提供统一的请求校验。
统一的访问入口
新建模块: sp06-zuul
1.
添加依赖:
yml 配置路由转发规则
# **包含深层路径
# * 只包含一层路径
# 服务id设置成访问子路径,是默认规则,
# zuul根据注册表的注册信息完成自动配置
# 最好手动配置,防止注册表不全
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
启动类添加注解 @EnableZuulProxy
zuul统一权限校验
http://localhost:3001/item-service/t45t4 没有登录不能访问
http://localhost:3001/item-service/t45t4?token=65345rt 已经登录过,可以访问
@Component
注解zuul 的自动配置类可以在 spring 容器中自动发现过滤器实例,完成自动配置
pom.xml
需要添加 sp01-commons 依赖
<?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">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
zuul 路由配置可以省略,缺省以服务 id 作为访问路径
spring:
application:
name: zuul
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
添加 @EnableZuulProxy 和 @EnableDiscoveryClient 注解
package cn.tedu.sp06;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class Sp06ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(Sp06ZuulApplication.class, args);
}
}
http://localhost:3001/item-service/decreaseNumber
使用postman,POST发送以下格式数据:
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]
http://localhost:3001/order-service/123abc
http://localhost:3001/order-service/
默认没有启用重试
在入口位置进行重试,会造成后台大面积服务压力翻倍,可能造成故障传播、雪崩
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
zuul.retryable=true
需要开启重试,默认不开启
<?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">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Hystrix是容错和限流工具
调用后台服务出错(异常,阻塞,服务崩溃),可以执行当前服务的一段代码,直接向客户端返回降级结果
ItemServiceFallback
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.http.server.reactive.HttpHandler;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@Component
public class ItemFB implements FallbackProvider {
/**
* 设置调用哪个后台服务,会应用当前降级类
* ”item-service“ 只针对商品降级
* “*”对所有服务都降级
* null 对所有服务都降级
*
* @return
*/
@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 null;
}
@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:500,msg:调用商品失败,data:null}
String json = JsonResult.err().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;
}
};
}
}
OrderServiceFallback
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 {
///
/* 设置调用哪个后台服务,会应用当前降级类
/* ”order-service“ 只针对订单品降级
/* “/”对所有服务都降级
/ null 对所有服务都降级
/*
/* @return
/*/
@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 null;
}
@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:500,msg:调用订单失败,data:null}
String json = JsonResult.err().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;
}
};
}
}
spring:
application:
name: zuul
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
zuul:
retryable: true
ribbon:
ConnectTimeout: 1000
ReadTimeout: 2000
MaxAutoRetriesNextServer: 1
MaxAutoRetries: 1
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 500
hystrix 对请求的降级和熔断,可以产生监控信息,hystrix dashboard可以实时的进行监控
当流量过大,造成后台服务故障,可以断开链路,限制后台服务的访问流量,等待后台服务恢复
*
断路器打开的条件
半开状态
断路器打开一段时间后,会进入半开状态
会尝试发送一次客户端调用,
成功,关闭断路器
失败,继续保持打开状态
添加链接描述
用 ab 工具,以并发50次,来发送20000个请求
ab -n 20000 -c 50 http://localhost:3001/item-service/35
断路器状态为 Open,所有请求会被短路,直接降级执行 fallback 方法
Hystrix dashboard
Hystrix数据健康仪表盘
Hystrix日志,是通过Actuator工具来暴露出来
Acuator
springboot提供的一个项目指标工具,可以通过Actuator获取项目的各种日志数据
添加Actuator
1.添加actuator,默认添加过了
2.暴露监控日志
m.e.w.e.i = “/*” 暴露所有日志
m.e.w.e.i = health 暴露健康状态日志
m.e.w.e.i = health,beans,mapping,hystrix.stream 暴露多种日志
3.查看日志
[添加链接描述](http://localhost:3001/actuator)
Hystrix dashboard 仪表盘
<?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">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
允许抓取的的服务器列表:localhost
server:
port: 4001
# - 代表数组或list
hystrix:
dashboard:
proxy-stream-allow-list:
- localhost
package cn.tedu.sp07;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class Sp07HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(Sp07HystrixDashboardApplication.class, args);
}
}
填入 hystrix 的监控端点,开启监控
添加链接描述
聚合多台服务器的日志数据,提供给仪表盘显示
turbine依赖需手动添加
<?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">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sp08-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp08-turbine</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.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-turbine</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
聚合的服务: zuul
为聚合的日志数据命名:new String("default")
spring:
application:
name: turbine
# 2001 eureka
# 3001 zuul
# 4001 hystrix dashboard
server:
port: 5001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
turbine:
app-config: zuul
cluster-name-expression: new String("default")
@EnableTurbine
package cn.tedu.sp08;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@EnableTurbine
@SpringBootApplication
public class Sp08TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(Sp08TurbineApplication.class, args);
}
}
在 hystrix dashboard 中填入turbine 监控路径,开启监控
turbine聚合了order-service两台服务器的hystrix监控信息
合并的日志地址: http://localhost:5001/turbine.stream
VMware 版本: 16+
*
NAT网络网段使用 64 网段
192.168.64.0
课前资料\虚拟机\
centos-7-1908.zip
centos-8-2105.zip
已经做了几步基础设置:
yum源和扩展源,使用阿里镜像
安装了工具: python、pip、ansible
方便设置ip地址的脚本:
ip-static 设置固定ip
ip-dhcp 自动获取ip
root
# centos 7 禁用 NetworkManager 系统服务
systemctl stop NetworkManager
systemctl disable NetworkManager
# centos 8 开启 VMware 托管
nmcli n on
systemctl restart NetworkManager
# 还原 VMware 虚拟网络
# VMware 虚拟网络不稳定,经常出现故障
# 编辑 -- 虚拟网络编辑器 -- 左下角按钮“还原默认设置” -- 设置 VMnet8 的 64 网段
# 会删除所有虚拟网络,重新创建
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_53244569/article/details/120839404
内容来源于网络,如有侵权,请联系作者删除!