标签:多次 int() interface eof his eth elements ntp 名称
https://github.com/zq2599/blog_demos
经过前面的知识积累,我们知道了SpringBoot-2.3新增的探针规范以及适用场景,这里做个简短的回顾:
小结完毕,接下来开始实打实的编码和操作实战,验证上述理论;
本次实战有两个环境:开发和运行环境,其中开发环境信息如下:
运行环境信息如下:
事实证明,用Ubuntu桌面版作为开发环境是可行的,体验十分顺畅,IDEA、SubLime、SSH、Chrome、微信都能正常使用,下图是我的Ubuntu开发环境:
本次实战包括以下内容:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bolingcavalry</groupId>
<artifactId>probedemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>probedemo</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<!--该配置会在jar中增加layer描述文件,以及提取layer的工具-->
<configuration>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.bolingcavalry.probedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProbedemoApplication {
public static void main(String[] args) {
SpringApplication.run(ProbedemoApplication.class, args);
}
}
package com.bolingcavalry.probedemo.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.AvailabilityState;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* description: 监听系统事件的类 <br>
* date: 2020/6/4 下午12:57 <br>
* author: willzhao <br>
* email: zq2599@gmail.com <br>
* version: 1.0 <br>
*/
@Component
@Slf4j
public class AvailabilityListener {
/**
* 监听系统消息,
* AvailabilityChangeEvent类型的消息都从会触发此方法被回调
* @param event
*/
@EventListener
public void onStateChange(AvailabilityChangeEvent<? extends AvailabilityState> event) {
log.info(event.getState().getClass().getSimpleName() + " : " + event.getState());
}
}
package com.bolingcavalry.probedemo.controller;
import org.springframework.boot.availability.ApplicationAvailability;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
@RestController
@RequestMapping("/statereader")
public class StateReader {
@Resource
ApplicationAvailability applicationAvailability;
@RequestMapping(value="/get")
public String state() {
return "livenessState : " + applicationAvailability.getLivenessState()
+ "<br>readinessState : " + applicationAvailability.getReadinessState()
+ "<br>" + new Date();
}
}
package com.bolingcavalry.probedemo.controller;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.LivenessState;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
/**
* description: 修改状态的controller <br>
* date: 2020/6/4 下午1:21 <br>
* author: willzhao <br>
* email: zq2599@gmail.com <br>
* version: 1.0 <br>
*/
@RestController
@RequestMapping("/staterwriter")
public class StateWritter {
@Resource
ApplicationEventPublisher applicationEventPublisher;
/**
* 将存活状态改为BROKEN(会导致kubernetes杀死pod)
* @return
*/
@RequestMapping(value="/broken")
public String broken(){
AvailabilityChangeEvent.publish(applicationEventPublisher, StateWritter.this, LivenessState.BROKEN);
return "success broken, " + new Date();
}
/**
* 将存活状态改为CORRECT
* @return
*/
@RequestMapping(value="/correct")
public String correct(){
AvailabilityChangeEvent.publish(applicationEventPublisher, StateWritter.this, LivenessState.CORRECT);
return "success correct, " + new Date();
}
/**
* 将就绪状态改为REFUSING_TRAFFIC(导致kubernetes不再把外部请求转发到此pod)
* @return
*/
@RequestMapping(value="/refuse")
public String refuse(){
AvailabilityChangeEvent.publish(applicationEventPublisher, StateWritter.this, ReadinessState.REFUSING_TRAFFIC);
return "success refuse, " + new Date();
}
/**
* 将就绪状态改为ACCEPTING_TRAFFIC(导致kubernetes会把外部请求转发到此pod)
* @return
*/
@RequestMapping(value="/accept")
public String accept(){
AvailabilityChangeEvent.publish(applicationEventPublisher, StateWritter.this, ReadinessState.ACCEPTING_TRAFFIC);
return "success accept, " + new Date();
}
}
package com.bolingcavalry.probedemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
/**
* description: hello demo <br>
* date: 2020/6/4 下午4:38 <br>
* author: willzhao <br>
* email: zq2599@gmail.com <br>
* version: 1.0 <br>
*/
@RestController
public class Hello {
/**
* 返回的是当前服务器IP地址,在k8s环境就是pod地址
* @return
* @throws SocketException
*/
@RequestMapping(value="/hello")
public String hello() throws SocketException {
List<Inet4Address> addresses = getLocalIp4AddressFromNetworkInterface();
if(null==addresses || addresses.isEmpty()) {
return "empty ip address, " + new Date();
}
return addresses.get(0).toString() + ", " + new Date();
}
public static List<Inet4Address> getLocalIp4AddressFromNetworkInterface() throws SocketException {
List<Inet4Address> addresses = new ArrayList<>(1);
Enumeration e = NetworkInterface.getNetworkInterfaces();
if (e == null) {
return addresses;
}
while (e.hasMoreElements()) {
NetworkInterface n = (NetworkInterface) e.nextElement();
if (!isValidInterface(n)) {
continue;
}
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress i = (InetAddress) ee.nextElement();
if (isValidAddress(i)) {
addresses.add((Inet4Address) i);
}
}
}
return addresses;
}
/**
* 过滤回环网卡、点对点网卡、非活动网卡、虚拟网卡并要求网卡名字是eth或ens开头
* @param ni 网卡
* @return 如果满足要求则true,否则false
*/
private static boolean isValidInterface(NetworkInterface ni) throws SocketException {
return !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual()
&& (ni.getName().startsWith("eth") || ni.getName().startsWith("ens"));
}
/**
* 判断是否是IPv4,并且内网地址并过滤回环地址.
*/
private static boolean isValidAddress(InetAddress address) {
return address instanceof Inet4Address && address.isSiteLocalAddress() && !address.isLoopbackAddress();
}
}
以上就是该SpringBoot工程的所有代码了,请确保可以编译运行;
# 指定基础镜像,这是分阶段构建的前期阶段
FROM openjdk:8u212-jdk-stretch as builder
# 执行工作目录
WORKDIR application
# 配置参数
ARG JAR_FILE=target/*.jar
# 将编译构建得到的jar文件复制到镜像空间中
COPY ${JAR_FILE} application.jar
# 通过工具spring-boot-jarmode-layertools从application.jar中提取拆分后的构建结果
RUN java -Djarmode=layertools -jar application.jar extract
# 正式构建镜像
FROM openjdk:8u212-jdk-stretch
WORKDIR application
# 前一阶段从jar中提取除了多个文件,这里分别执行COPY命令复制到镜像空间中,每次COPY都是一个layer
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
mvn clean package -U -DskipTests
sudo docker build -t bolingcavalry/probedemo:0.0.1 .
SpringBoot的镜像准备完毕,接下来要让kubernetes环境用上这个镜像;
此时的镜像保存在开发环境的电脑上,可以有以下三种方式加载到kubernetes环境:
以上三种方法的优缺点整理如下:
我的kubernetes环境只有一台电脑,因此用的是方法三,参考命令如下(建议安装sshpass,就不用每次输入帐号密码了):
# 将镜像保存为tar文件
sudo docker save bolingcavalry/probedemo:0.0.1 > probedemo.tar
# scp到kubernetes服务器
sshpass -p 888888 scp ./probedemo.tar root@192.168.50.135:/root/temp/202006/04/
# 远程执行ssh命令,加载docker镜像
sshpass -p 888888 ssh root@192.168.50.135 "docker load < /root/temp/202006/04/probedemo.tar"
apiVersion: v1
kind: Service
metadata:
name: probedemo
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30080
selector:
name: probedemo
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: probedemo
spec:
replicas: 2
template:
metadata:
labels:
name: probedemo
spec:
containers:
- name: probedemo
image: bolingcavalry/probedemo:0.0.1
tty: true
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 5
failureThreshold: 10
timeoutSeconds: 10
periodSeconds: 5
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 10
periodSeconds: 5
ports:
- containerPort: 8080
resources:
requests:
memory: "512Mi"
cpu: "100m"
limits:
memory: "1Gi"
cpu: "500m"
至此,从编码到部署都完成了,接下来验证SpringBoot-2.3.0.RELEASE的探针技术;
kubernetes所在机器的IP地址是192.168.50.135,因此SpringBoot服务的访问地址是http://192.168.50.135:30080/xxx
访问地址http://192.168.50.135:30080/actuator/health/liveness,返回码如下图红框,可见存活探针已开启:
curl http://10.233.90.195:8080/statewriter/accept
https://github.com/zq2599/blog_demos
标签:多次 int() interface eof his eth elements ntp 名称
原文地址:https://www.cnblogs.com/bolingcavalry/p/13090502.html