阿八博客
  • 100000+

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

Spring Cloud 升级之路

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/1010/116293.html

升级相关的项目组件:

eureka-server 注册中心config-server 配置中心zipkin-server 链路追踪admin-server 监控中心gateway-service 网关user-server 用户服务uaa-service 授权中心log-service 日志中心client-server 业务服务
<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>
首先是注册中心 Eureka
        <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: 8860eureka:  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/infomanagement:  endpoints:    web:      exposure:        include: "*"  endpoint:    health:      show-details: ALWAYS
然后是配置中心 config

只需要引入三个包

        <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/healthfeign:  hystrix:    enabled: truemanagement:  endpoints:    web:      exposure:        include: "*"        exclude: loggers,beans  endpoint:    health:      show-details: ALWAYS
链路追踪(zipkin)不再支持用户自定义,直接下载jar包,默认端口是 9411 采集率0.1聚合监控 admin

项目依赖

    <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@EnableDiscoveryClientpublic 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}
网关 gateway

项目依赖

    <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: 50000ribbon:  ReadTimeout: 10000  ConnectTimeout: 10000  MaxAutoRetries: 0  #当前服务重试次数  MaxAutoRetriesNextServer: 0 #切换服务重试次数zuul:  host:    connect-timeout-millis: 60000    socket-timeout-millis: 80000...

Application

@SpringBootApplication@EnableZuulProxy@EnableEurekaClientpublic 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

@Configurationpublic 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();    }}

特别感谢
方志朋相关文章及博客

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!