建立一个parent
因为用了大量开源的jar包,好多已经报了漏洞,公司上线前有校验,老项目版本控制乱七八糟的,升级jar超级麻烦
所以每个项目先搞一个parent(或者共用一个parent),团队专人维护jar包版本管理,省得到了上线的时候手忙脚乱的升级jar
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dails</groupId>
<artifactId>dails-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-boot.version>2.7.17</spring-boot.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
<druid.version>1.2.1</druid.version>
<lombok.version>1.18.22</lombok.version>
<mybatisplus.version>3.4.3.4</mybatisplus.version>
<guava.version>31.1-jre</guava.version>
<gson.version>2.9.0</gson.version>
<xstream.version>1.4.18</xstream.version>
<junit.version>4.13.1</junit.version>
<okhttp.version>4.9.0</okhttp.version>
<fastjson.version>2.0.5</fastjson.version>
<mysql.version>8.0.25</mysql.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath></relativePath>
</parent>
<dependencyManagement>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-dependencies</artifactId>-->
<!-- <version>${spring-boot.version}</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.69</version>
</dependency>
<!--分页 log4j-->
</dependencies>
</dependencyManagement>
<build>
</build>
</project>
2、spring boot 版本和spring cloud版本对应关系
官方对应关系 https://start.spring.io/actuator/info
{
"git":{
"branch":"908dae56aac82ef4c98a0d7214fc59e910ab9b40",
"commit":{
"id":"908dae5",
"time":"2022-07-15T10:14:55Z"
}
},
"build":{
"version":"0.0.1-SNAPSHOT",
"artifact":"start-site",
"versions":{
"spring-boot":"2.7.1",
"initializr":"0.13.0-SNAPSHOT"
},
"name":"start.spring.io website",
"time":"2022-07-15T10:16:59.527Z",
"group":"io.spring.start"
},
"bom-ranges":{
"codecentric-spring-boot-admin":{
"2.4.3":"Spring Boot >=2.3.0.M1 and <2.5.0-M1",
"2.5.6":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
"2.6.7":"Spring Boot >=2.6.0.M1 and <2.7.0-M1",
"2.7.1":"Spring Boot >=2.7.0.M1 and <3.0.0-M1",
"3.0.0-M3":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"solace-spring-boot":{
"1.1.0":"Spring Boot >=2.3.0.M1 and <2.6.0-M1",
"1.2.1":"Spring Boot >=2.6.0.M1 and <2.7.0-M1"
},
"solace-spring-cloud":{
"1.1.1":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"2.1.0":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
"2.3.0":"Spring Boot >=2.6.0.M1 and <2.7.0-M1"
},
"spring-cloud":{
"Hoxton.SR12":"Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
"2020.0.6":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
"2021.0.0-M1":"Spring Boot >=2.6.0-M1 and <2.6.0-M3",
"2021.0.0-M3":"Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
"2021.0.0-RC1":"Spring Boot >=2.6.0-RC1 and <2.6.1",
"2021.0.3":"Spring Boot >=2.6.1 and <3.0.0-M1",
"2022.0.0-M1":"Spring Boot >=3.0.0-M1 and <3.0.0-M2",
"2022.0.0-M2":"Spring Boot >=3.0.0-M2 and <3.0.0-M3",
"2022.0.0-M3":"Spring Boot >=3.0.0-M3 and <3.1.0-M1"
},
"spring-cloud-azure":{
"4.3.0":"Spring Boot >=2.5.0.M1 and <3.0.0-M1"
},
"spring-cloud-gcp":{
"2.0.11":"Spring Boot >=2.4.0-M1 and <2.6.0-M1",
"3.3.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1"
},
"spring-cloud-services":{
"2.3.0.RELEASE":"Spring Boot >=2.3.0.RELEASE and <2.4.0-M1",
"2.4.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"3.3.0":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
"3.4.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1"
},
"spring-geode":{
"1.3.12.RELEASE":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"1.4.13":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"1.5.14":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
"1.6.9":"Spring Boot >=2.6.0-M1 and <2.7.0-M1",
"1.7.1":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
"2.0.0-M3":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"spring-shell":{
"2.1.0-RC1":"Spring Boot >=2.7.0 and <3.0.0-M1"
},
"vaadin":{
"14.8.13":"Spring Boot >=2.1.0.RELEASE and <2.6.0-M1",
"23.1.2":"Spring Boot >=2.6.0-M1 and <2.8.0-M1"
},
"wavefront":{
"2.0.2":"Spring Boot >=2.1.0.RELEASE and <2.4.0-M1",
"2.1.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"2.2.2":"Spring Boot >=2.5.0-M1 and <2.7.0-M1",
"2.3.0":"Spring Boot >=2.7.0-M1 and <3.0.0-M1"
}
},
"dependency-ranges":{
"native":{
"0.9.0":"Spring Boot >=2.4.3 and <2.4.4",
"0.9.1":"Spring Boot >=2.4.4 and <2.4.5",
"0.9.2":"Spring Boot >=2.4.5 and <2.5.0-M1",
"0.10.0":"Spring Boot >=2.5.0-M1 and <2.5.2",
"0.10.1":"Spring Boot >=2.5.2 and <2.5.3",
"0.10.2":"Spring Boot >=2.5.3 and <2.5.4",
"0.10.3":"Spring Boot >=2.5.4 and <2.5.5",
"0.10.4":"Spring Boot >=2.5.5 and <2.5.6",
"0.10.5":"Spring Boot >=2.5.6 and <2.5.9",
"0.10.6":"Spring Boot >=2.5.9 and <2.6.0-M1",
"0.11.0-M1":"Spring Boot >=2.6.0-M1 and <2.6.0-RC1",
"0.11.0-M2":"Spring Boot >=2.6.0-RC1 and <2.6.0",
"0.11.0-RC1":"Spring Boot >=2.6.0 and <2.6.1",
"0.11.0":"Spring Boot >=2.6.1 and <2.6.2",
"0.11.1":"Spring Boot >=2.6.2 and <2.6.3",
"0.11.2":"Spring Boot >=2.6.3 and <2.6.4",
"0.11.3":"Spring Boot >=2.6.4 and <2.6.6",
"0.11.5":"Spring Boot >=2.6.6 and <2.7.0-M1",
"0.12.0":"Spring Boot >=2.7.0-M1 and <2.7.1",
"0.12.1":"Spring Boot >=2.7.1 and <3.0.0-M1"
},
"okta":{
"1.4.0":"Spring Boot >=2.2.0.RELEASE and <2.4.0-M1",
"1.5.1":"Spring Boot >=2.4.0-M1 and <2.4.1",
"2.0.1":"Spring Boot >=2.4.1 and <2.5.0-M1",
"2.1.5":"Spring Boot >=2.5.0-M1 and <2.7.0-M1"
},
"mybatis":{
"2.1.4":"Spring Boot >=2.1.0.RELEASE and <2.5.0-M1",
"2.2.2":"Spring Boot >=2.5.0-M1"
},
"camel":{
"3.5.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"3.10.0":"Spring Boot >=2.4.0.M1 and <2.5.0-M1",
"3.13.0":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
"3.17.0":"Spring Boot >=2.6.0.M1 and <2.7.0-M1"
},
"picocli":{
"4.6.3":"Spring Boot >=2.4.0.RELEASE and <3.0.0-M1"
},
"open-service-broker":{
"3.2.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"3.3.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"3.4.1":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
"3.5.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1"
}
}
}
这是个比较麻烦的事情,还可以参考官网
https://spring.io/projects/spring-cloud#learn
GA:(Ggeneral Availability):发行稳定版,官方推荐使用此版本。
RC:(Release Candidate):候选版本,几乎就是正式版了,
R,RELEASE:正式版,等价于GA
SNAPSHOT:快照版,可以稳定使用,且仍在继续改进版本
Release:发行版,有时间限制
SR:修正版
我们正常开发的时候,新开一个镜像版SNAPSHOT,上线前把SNAPSHOT去掉,或者改成Release,引用jar包,尽量不要用SNAPSHOT版本
这里看springboot 就整个GA版本,下面是较新的GA版本
- cloud Version: 2021.0.8 ~ Boot Version: 2.7.17
3、添加一个eureka server
3.1、新建一个项目,pom内添加
clpud已经没有eureka了,尴尬
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>3.1.7</version>
</dependency>
<!--安全要求必须要有密码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.2、加一个java类
package cn.dms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
<!--高版本的Security对csrd做校验了,咱们得Spring Security 5.6.5及更旧版本或Spring Boot 2.6.8-->
//@Configuration
//@EnableWebSecurity
//public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// @Override
// protected void configure(HttpSecurity http) throws Exception {
// http.csrf().disable();// 关闭csrf校验
// http.authorizeRequests().anyRequest().authenticated().and().httpBasic();// 开启认证
super.configure(http);// 需要注释,否则Caused by: java.lang.IllegalStateException: Can't configure anyRequest after itself
// }
//}
<!--Spring Security 5.7.1及更新版本或者Spring Boot 2.7.0及更新版本已经不用WebSecurityConfigurerAdapter,换成下面的-->
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//关闭csrf
http.csrf().disable();
// 支持httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
return http.build();
}
}
3.3、application.yml添加
server:
port: 8088
spring:
application:
name: eureka-server
security:
enabled: true
user:
name: wolf # 用户名
password: abc123 # 用户密码
eureka:
instance:
hostname: localhost
lease-expiration-duration-in-seconds: 10
lease-renewal-interval-in-seconds: 5
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://wolf:abc123@${eureka.instance.hostname}:${server.port}/eureka/
healthcheck:
enabled: true
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
4 命令行关闭项目
关闭前,应该先通知eureka关闭
curl -X PUT 'http://172.19.133.70:8088/eureka/apps/SERVICE-USER/172.19.133.69:28081/status?value=OUT_OF_SERVICE'
curl -X PUT 'http://172.19.133.70:8088/eureka/apps/SERVICE-USER/172.19.133.69:28081/status?value=DOWN'
关闭项目后,重启
curl -X DELETE http://172.19.133.70:8088/eureka/apps/SERVICE-USER/172.19.133.69:28081
DELETE 之后,可以心跳重新发现微服务
可以启动前,或者启动后执行DELETE