SpringBoot + Jenkins 实现一条龙自动化部署完整版教程
这个问题涉及的内容较多,我将提供一个简化版的指导和代码示例。
- 配置SpringBoot应用的
application.properties
或application.yml
,开启远程部署功能:
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/yourdb
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 开启远程部署
spring.application.admin.enabled=true
- 在Jenkins中配置构建任务,包括获取代码、编译、测试、打包,并配置远程部署的脚本:
# Jenkins构建步骤
mvn clean package
# 远程部署脚本deploy.sh
curl -X POST http://localhost:8080/actuator/deploy
- 在SpringBoot应用中使用
Spring Boot Actuator
,暴露远程部署端点:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableDiscoveryClient
@EnableWebMvc
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Bean
public RemoteShellExecutor remoteShellExecutor() {
return new RemoteShellExecutor();
}
}
// RemoteShellExecutor.java
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerAdapter;
import org.springframework.boot.actuate.endpoint.annotation.EndpointWebExtension;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationContext;
@Component
@EndpointWebExtension(endpoint = ShutdownEndpoint.class)
public class RemoteShellExecutor {
private final ApplicationContext context;
public RemoteShellExecutor(ApplicationContext context) {
this.context =
评论已关闭