Spring Cloud Upgrade

Upgrade related project components:

  • eureka-server registry
  • config-server configuration center
  • zipkin-server link tracking
  • admin-server monitoring center
  • gateway-service gateway
  • user-server user service
  • uaa-service authorization center
  • log-service log Center
  • client-server business service
<java.version>1.8</java.version>

from

Spring Boot <version>1.5.3.RELEASE</version>
Spring Cloud <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>

Upgrade to

Spring Boot <version>2.0.2.RELEASE</version>
Spring Cloud <spring-cloud.version>Finchley.SR2</spring-cloud.version>
  • First, the registry Eureka
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

Change to

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

Note: All Eureka clients should be replaced by spring-cloud-starter-netflix-eureka-client, which will not be repeated below
configuration file

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
  • Then the configuration center config

Only three packages need to be introduced

        <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>

Change the general configuration file to

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
  • zipkin no longer supports user-defined, download jar package directly, default port is 9411 acquisition rate 0.1
  • Aggregation monitoring admin

Project dependence

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- To configure-->
        <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>

        <!--Security Authentication Component-->
        <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>

Open Services

@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
@EnableDiscoveryClient
public class AdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }

}

configuration file

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 gateway

Project dependence

    <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>

        <!-- To configure-->
        <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>

Configuration file bootstrap

spring:
  application:
    name: gateway-service
  cloud:
    config:
      uri: http://localhost:8862
      fail-fast: true
  profiles:
    active: pro
  main:
    allow-bean-definition-overriding: true

Configuration file application

.
.
.
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 50000

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000
  MaxAutoRetries: 0  #Number of current service retries
  MaxAutoRetriesNextServer: 0 #Number of retries for switching services

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); // Allow cookies to cross domains
        config.addAllowedOrigin("*");// The URI that allows requests to be submitted to the server, * means all allowed. Here try to limit the source areas, such as http://xxx:8080, to reduce security risks.
        config.addAllowedHeader("*");// Header information allowed to be accessed,* represents all
        config.setMaxAge(18000L);// Cache time (seconds) for preview requests, i.e. during this time period, no preview will be performed for the same cross-domain requests
        config.addAllowedMethod("*");// The method to allow submission of requests * means that all requests are allowed, or GET, PUT, etc. can be set separately.
    /*    config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");// Request method to allow 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();
    }
}

Special thanks
Fang Zhipeng's related articles and blogs

Tags: Java Spring socket

Posted on Thu, 10 Oct 2019 12:29:49 -0700 by natasha_thomas