升级相关的项目组件:
<java.version>1.8</java.version>
由
Spring Boot <version>1.5.3.RELEASE</version> Spring Cloud <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
升级到
Spring Boot <version>2.0.2.RELEASE</version> Spring Cloud <spring-cloud.version>Finchley.SR2</spring-cloud.version>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
改为
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
注:所有eureka client同理改为 spring-cloud-starter-netflix-eureka-client,以下不再赘述
配置文件
server: port: 8860 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:${server.port}/eureka/ instance: hostname: localhost instance-id: ${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true health-check-url: /actuator/health home-page-url: /actuator status-page-url-path: /actuator/info management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
只需要引入三个包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
通用配置文件改为
eureka: client: registryFetchIntervalSeconds: 5 service-url: defaultZone: http://localhost:8860/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health feign: hystrix: enabled: true management: endpoints: web: exposure: include: "*" exclude: loggers,beans endpoint: health: show-details: ALWAYS
项目依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 配置--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--安全认证组件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
开启的服务
@SpringBootApplication @EnableAdminServer @EnableEurekaClient @EnableDiscoveryClient public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
配置文件
spring: security: user: name: "admin" password: "123456" roles: "ACTUATOR_ADMIN" eureka: instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}
项目依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- 配置--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
配置文件 bootstrap
spring: application: name: gateway-service cloud: config: uri: http://localhost:8862 fail-fast: true profiles: active: pro main: allow-bean-definition-overriding: true
配置文件 application
. . . hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 50000 ribbon: ReadTimeout: 10000 ConnectTimeout: 10000 MaxAutoRetries: 0 #当前服务重试次数 MaxAutoRetriesNextServer: 0 #切换服务重试次数 zuul: host: connect-timeout-millis: 60000 socket-timeout-millis: 80000 . . .
Application
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class GatewayServiceApplication { public static void main(String[] args) { SpringApplication.run(GatewayServiceApplication.class, args); } @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); // 允许cookies跨域 config.addAllowedOrigin("*");// 允许向该服务器提交请求的URI,*表示全部允许。。这里尽量限制来源域,比如http://xxxx:8080 ,以降低安全风险。。 config.addAllowedHeader("*");// 允许访问的头信息,*表示全部 config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许,也可以单独设置GET、PUT等 /* config.addAllowedMethod("HEAD"); config.addAllowedMethod("GET");// 允许Get的请求方法 config.addAllowedMethod("PUT"); config.addAllowedMethod("POST"); config.addAllowedMethod("DELETE"); config.addAllowedMethod("PATCH");*/ source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } }
SecuritySecureConfig
@Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) { this.adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter( "redirectTo" ); http.authorizeRequests() .antMatchers( adminContextPath + "/assets/**" ).permitAll() .antMatchers( adminContextPath + "/login" ).permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and() .logout().logoutUrl( adminContextPath + "/logout" ).and() .httpBasic().and() .csrf().disable(); } }
特别感谢
方志朋相关文章及博客