欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud版本升级记录(1)注册中心升级到Hoxton.SR12 博客分类: spring boot 实践笔记

程序员文章站 2024-03-13 18:49:45
...

     2018年开始做spring cloud微服务,选定版本是Finchley.RC2,时至今日,spring cloud版本从Finchley经历了GreenWich、Hoxton、2020.0.x、2021.0.x的变化,每一个版本的变化,使用的组件都有大的改变。Spring Cloud netflix中的zuul, ribbon, hystrix都基本上算是废了。

     zuul改用spring cloud gateway, Hystrix 直接改用Spring Cloud Circuit Breaker + Resilience4J,Ribbon改用 Spring Cloud Load Balancer.

     本次升级到spring cloud Hoxton.SR12,spring boot版本升级到2.3.12.RELEASE,仍然使用eureka server,服务发现服务的升级很简单,只需要更改spring cloud和spring boot的版本号即可。

pom.xml文件:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.3.12.RELEASE</version>
  </parent>
  <groupId>net.hzwei206.test</groupId>
  <artifactId>sctest-discovery-server</artifactId>
  <version>0.0.2</version>
  <description>sctest-discovery-server</description>
  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <java.version>1.8</java.version>
  </properties>
 
  <repositories>  
     <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
     </repository>
  </repositories> 
  <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Hoxton.SR12</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
  </dependencyManagement>
  <dependencies> 
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-tomcat</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-undertow</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
	      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency> 
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
      <dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version> 
	  </dependency>
	  <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId> 
    </dependency>
  </dependencies>
  
  <build>
      ........
  </build>
</project>

   这里我弃用tomcat,改用undertow

 

 

   增加spring security:

 

 

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().defaultSuccessUrl("/")
            .and().httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/login").permitAll()
            .anyRequest()
            .authenticated()
            .and().headers().frameOptions().disable()
            .and().csrf().disable();
    }
}

   .httpBasic()不能少,不然访问不了注册中心,服务无法注册。

 

 application.yml配置文件增加spring security的用户名密码:

    

server:
  port: 8761
spring:
  application:
    name: service-discovery-server
  security:
    basic:
      enabled: true
    user:
      name: admin
      password: 123456

   eureka-server配置:

   

eureka:
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 4000
  instance:
    prefer-ip-address: true
    hostname: 127.0.0.1
  client:
    # 如果是集群,下面两个改为true
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:${server.port}/eureka/