springboot升级到版本2.7.9

项目升级后版本号
5.0.0.000-SNAPSHOT
升级jar包,springboot、springCloud
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>

及查看spring对应版本;5.3.25
升级csp
<groupId>com.pingan.smartcity</groupId>
<artifactId>xchg-csp-security-spring-boot-starter</artifactId>
<version>5.0.0.000-SNAPSHOT</version>
指定版本号spring-cloud-starter-netflix-hystrix
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	<version>2.2.10.RELEASE</version>
</dependency>
添加配置文件
可以不添加配置文件,在启动类上添加配置
SpringApplication application = new SpringApplication(Application.class);
application.setAllowCircularReferences(Boolean.TRUE);

spring.main.allow-circular-references = true
有缺失时添加
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>

		<dependency>
			<groupId>org.junit</groupId>
			<artifactId>junit4-runner</artifactId>
			<version>5.0.0-ALPHA</version>
		</dependency>
禁止引入下面jar包
<dependency>
	<groupId>com.netflix.ribbon</groupId>
	<artifactId>ribbon-loadbalancer</artifactId>
	<version>2.7.18</version>
</dependency>
<dependency>
	<groupId>com.netflix.ribbon</groupId>
	<artifactId>ribbon-eureka</artifactId>
	<version>2.7.18</version>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
	<version>2.2.10.RELEASE</version>
</dependency>
pringBoot2.7兼容Swagger,在swagger配置中添加方法
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import java.util.Collection;

@ConditionalOnProperty(name = "knife4j.enable",havingValue = "true")
@EnableConfigurationProperties(CorsEndpointProperties.class)

    /**
     * springBoot2.7兼容Swagger
     **/
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }

    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
解决在类上@FeignClient、@RequestMapping同时存在的问题,添加类FeignSpringMvcContract、FeignSpringMvcContractConfig
import feign.MethodMetadata;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.cloud.openfeign.CollectionFormat;
import org.springframework.cloud.openfeign.FeignClientProperties;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.ConversionService;
import java.util.List;
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;

@Configuration
public class SpringMvcContractConfig {

    /**
     * 解决类上@FeignClient、@RequestMapping同时存在时的报错问题
     */
    @Bean
    @Primary
    public SpringMvcContract feignContract(List<AnnotatedParameterProcessor> parameterProcessors,
                                           ConversionService feignConversionService,
                                           FeignClientProperties feignClientProperties) {
        boolean decodeSlash = feignClientProperties == null || feignClientProperties.isDecodeSlash();
        return new SpringMvcContract(parameterProcessors, feignConversionService, decodeSlash) {
            @Override
            protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
                CollectionFormat collectionFormat = findMergedAnnotation(clz, CollectionFormat.class);
                if (collectionFormat != null) {
                    data.template().collectionFormat(collectionFormat.value());
                }
            }
        };
    }
}
获取eureka服务列表方法
@Autowired
private DiscoveryClient discoveryClient;

List<ServiceInstance> List=discoveryClient.getInstances(xxlJobParam.jobEurekaServerName);
if(CollectionUtils.isNotEmpty(List)){
    for (ServiceInstance s : List) {

    }
}

List<ServiceInstance> servers = discoveryClient.getInstances(schedule.getAppServerName());
ServiceInstance server = servers.get(new SecureRandom().nextInt(servers.size()));
接口类上需要注解@RequestMapping
posted @ 2025-04-02 20:22  rbcd  阅读(33)  评论(0)    收藏  举报