【Kubernetes】Spring Boot 通过名称访问
环境
- kubernetes 1.20.2
- Spring Boot 2.5.0-M1
目标
前面我们测试过服务名和 IP 是对应的,现在通过 Spring Boot 项目,通过名称访问数据库验证。
创建 Spring Boot 项目
pom.xml
<?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.5.0-M1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>jiangbo.spring.demo</groupId>
<artifactId>spring-demo</artifactId>
<version>svc</version>
<name>spring-boot-demo</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
application.yaml
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: "*"
spring:
datasource:
url: jdbc:postgresql://svc-pg/postgres
username: postgres
password: 123456
代码
package jiangbo.spring.docker;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.info.BuildProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringDemoApplication {
@Autowired
private JdbcTemplate jdbcTemplate;
private final BuildProperties buildProperties;
public SpringDemoApplication(BuildProperties buildProperties) {
this.buildProperties = buildProperties;
}
private static String hostname;
public static void main(String[] args) throws UnknownHostException, InterruptedException {
hostname = InetAddress.getLocalHost().getHostName();
SpringApplication.run(SpringDemoApplication.class, args);
}
@GetMapping("/hostname")
public String hello() {
return hostname + " " + buildProperties.getVersion();
}
private static final String SQL = "SELECT * FROM public.user";
@GetMapping("/users")
public List<Person> users() {
return jdbcTemplate.query(SQL, Person.ROW_MAPPER);
}
}
推送镜像
mvn clean package
docker build -t jiangbo920827/spring-demo:svc .
docker push jiangbo920827/spring-demo:svc
访问数据库
rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: rc-demo
spec:
replicas: 3
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: pod-demo
image: jiangbo920827/spring-demo:svc
ports:
- containerPort: 8080
创建 Service
apiVersion: v1
kind: Service
metadata:
name: svc-demo
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 8080
查看 Service
[root@master kubernetes]# kubectl describe service svc-demo
Name: svc-demo
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=myapp
Type: ClusterIP
IP Families: <none>
IP: 10.105.41.106
IPs: 10.105.41.106
Port: <unset> 80/TCP
TargetPort: 8080/TCP
Endpoints: 10.244.1.115:8080,10.244.1.116:8080,10.244.1.117:8080
Session Affinity: None
Events: <none>
验证
[root@master kubernetes]# curl 10.105.41.106/users | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29 0 29 0 0 73 0 --:--:-- --:--:-- --:--:-- 73
[
{
"name": "jiangbo",
"age": 44
}
]
总结
新建了一个 Spring Boot 项目,通过服务名的方式,访问到数据库中的数据,并展示。
附录
分类:
Kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!