天天看点

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3

如果没有看过前面的先看前面的: 

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读1

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2

到这里, 我们对语spring-boot2.0 + spring-cloud Finchley M7 的基本组件的基本使用就创建完成了,后续会对 监控,链路追踪, gateway 等案例在写一片文章。

BUG记录: ① 按照, 之前版本添加, 没有添加仓库信息, 会出现, loging 包下载失败, 这个实际没有截图, 解决方法是添加下面的依赖即可解决, 要连接spring 的仓库

  1. <repositories>  
  2.     <repository>  
  3.         <id>spring-milestones</id>  
  4.         <name>Spring Milestones</name>  
  5.         <url>https://repo.spring.io/libs-milestone</url>  
  6.         <snapshots>  
  7.             <enabled>false</enabled>  
  8.         </snapshots>  
  9.     </repository>  
  10. </repositories>  

② 依赖添加完成后, 可能会出现 spring-boot-maven-plugin 安装失败情况, 解决方法, 添加下面的依赖:

  1. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin -->  
  2. <dependency>  
  3.     <groupId>org.springframework.boot</groupId>  
  4.     <artifactId>spring-boot-maven-plugin</artifactId>  
  5.     <version>2.0.0.M7</version>  
  6. </dependency>  

③ 在启动Eureka 项目的出现下面的问题:

nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans]: Factory method 'configurationPropertiesBeans' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata 2018-03-03 13:34:51.051 INFO 48444 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-03 13:34:51.066 ERROR 48444 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans]: Factory method 'configurationPropertiesBeans' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE]

描述 : 这个时候一个BUG , 在issue 上面有。 https://github.com/spring-projects/spring-boot/issues/11954 并且我也提了一个Issue。

解决方案: 将之前的你在官网上看到的2.0.0.M7 的依赖换成下面的这个。 是不是感觉很坑。

  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>2.0.0.RELEASE</version>  
  5. </parent>  

④. 访问端点信息设置发生变化, 这个是一个比较大的坑。找了很久(这个spring boot 的变化) 1.5.x 版本: 1). 主要是添加依赖

  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-actuator</artifactId>  
  4. </dependency>  

2). application 文件中添加下面的文件

  1. management:  
  2.   security:  
  3.     enabled: false  

3). 访问下面等地址, 根据实际需求访问需要了解的 http://localhost:9095 /env

2.x 版本:

  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-actuator</artifactId>  
  4. </dependency>  
  5. <!-- 做简单的安全和端点开放 -->  
  6. <dependency>  
  7.     <groupId>org.springframework.boot</groupId>  
  8.     <artifactId>spring-boot-starter-security</artifactId>  
  9. </dependency>  

2). application.properties 文件中添加下面的文件

  1. management.endpoints.web.exposure.include=*  
  2. #management.endpoint.shutdown.enabled=true  
  3. management.endpoint.health.show-details=always  

3). 创建配置文件, 这里设了密码可以根据需求去掉。

  1. @Configuration  
  2. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {  
  3.    @SuppressWarnings("deprecation")  
  4.    @Bean  
  5.    public InMemoryUserDetailsManager inMemoryUserDetailsManager() {  
  6.       return new InMemoryUserDetailsManager(  
  7.            User.withDefaultPasswordEncoder().username("user").password("password")  
  8.                   .authorities("ROLE_USER").build(),  
  9.             User.withDefaultPasswordEncoder().username("admin").password("admin")  
  10.                   .authorities("ROLE_ACTUATOR", "ROLE_USER").build());  
  11.    }  
  12.    @Override  
  13.    protected void configure(HttpSecurity http) throws Exception {  
  14.       http  
  15.             .authorizeRequests()  
  16.             .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")  
  17.             .antMatchers("/**").permitAll()  
  18.             .and()  
  19.             .httpBasic();  
  20.    }  
  21. }  

4). 访问下面等地址, 根据实际需求访问需要了解的 http://localhost:9999/actuator/env

5). 要查看端点信息的, 不能使用yml 文件配置: 新版的bug 必须使用properties 文件 yml启动就会报错。

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3

源码地址 : https://github.com/zhongzunfa/zzf-spring-cloud-Finchley.git

参考地址 : https://projects.spring.io/spring-cloud/ http://cloud.spring.io/spring-cloud-static/Finchley.M7/single/spring-cloud.html https://projects.spring.io/spring-boot/ https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes

继续阅读