×
Community Blog Upgrade Spring Boot Applications to Spring Cloud

Upgrade Spring Boot Applications to Spring Cloud

This article outlines the process for upgrading a Spring Boot application to Spring Cloud, capitalizing on the microservice ecosystem of Spring Cloud.

By Jun Liu

Spring Cloud is a set of microservice ecosystems built atop Spring Boot, encompassing service discovery, configuration management, rate limiting with fallbacks, distributed transactions, and asynchronous messaging. Thus, by simply adding dependencies and annotations in just four straightforward steps, you can upgrade your Spring Boot applications to Spring Cloud.

Officially launched on the Spring Cloud Alibaba (SCA) official website: sca.aliyun.com

Upgrade Spring Boot Applications to Spring Cloud

The following are the complete steps to upgrade a Spring Boot application to Spring Cloud.

Step 1: Add Spring Cloud Dependencies

First, add Spring Cloud and Spring Cloud Alibaba dependencies to the application. Select an appropriate Spring Cloud version based on the Spring Boot version of the current application. For more information, see the version mapping table[1].

<properties>
    <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
    <spring-cloud.version>2022.0.0</spring-cloud.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <!-- Nacos service discovery -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- service discovery:OpenFeign service calls-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  <!-- service discovery:OpenFeign service calls -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
</dependencies>

In the preceding example, dependencies such as service registration and discovery and OpenFeign are added.

Step 2: Add Configurations

Add the following configuration items to the application.yml or application.properties file to set the application name and registry address.

application.yml:

spring:
  application:
    #The project name is required and is unique in the registry.
    #Better be consistent with the previous domain name specification and Kubernetes service name, because it will be used as the call and load balancing basis.
    name: service-provider
  cloud: 
    nacos: 
      discovery: #Enable Spring Cloud Nacos Discovery.
        server-addr: 127.0.0.1:8848

application.properties:

#The project name is required and is unique in the registry.
# Better be consistent with the previous domain name specification and Kubernetes service name, because it will be used as the call and load balancing basis.
spring.application.name=service-provider
 #Enable spring cloud nacos discovery
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Step 3: Add Annotations to the Startup Class

Add EnableDiscoveryClient and EnableFeignClients annotations to the startup class to start the automatic registration and discovery of service addresses.

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

Step 4: Adjust the Methods for Calling Services

Note:

  1. To ensure a smooth upgrade, make sure that the downstream application has transformed Spring Cloud and registered the service in the registry before transforming the call method.
  2. By default, the hostname (service-provider in this example) of RestTemplate/FeignClient is the name of the corresponding Spring Cloud application. Therefore, to ensure minor modification, the application name spring.name=service-provider set during the transformation should be consistent with the previous naming convention. For example:
  • If there already exists a custom domain name, the application name should be consistent with the domain name.
  • If you used Kubernetes Service, the application name should be consistent with the Service Name.

1. RestTemplate Mode

Add the @LoadBlanced annotation to the previous RestTemplate bean to enable RestTemplate to access service discovery and loading balance:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Keep other code used to call RestTemplate unchanged, and change only the hostname, as shown in the following figure.

@RestController
 public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/echo-rest/{str}")
    public String rest(@PathVariable String str) {
        return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    }
}

2. FeignClient Mode

Use the @FeignClient annotation to package the EchoService interface into a FeignClient. The attribute name corresponds to the peer application name spring.name=service-provider.

//@FeignClient(name = "service-provider", url="http://service.example.com/") 
@FeignClient(name = "service-provider")
public interface EchoService {
    @GetMapping(value = "/echo/{str}")
    String echo(@PathVariable("str") String str);
}

Inject the EchoService as a standard bean to initiate a request to the remote service.

@RestController
 public class TestController {

    @Autowired
    private EchoService echoService;

    @GetMapping(value = "/echo-feign/{str}")
    public String feign(@PathVariable String str) {
        return echoService.echo(str);
    }
}

3. HtClient and Custom HTTP Access Tools

If you use HttpClient or self-encapsulated HTTP calling tools, we recommend that you transform to the RestTemplate mode or the FeignClient mode.

References

Complete Sample Code

The architecture of applications built based on Spring Boot varies. For example, they may be any of the following common architectures, but the general transformation method of upgrading to Spring Cloud is similar. All can be converted to address discovery and loading balance based on the Nacos registry.

• Customize domain names based on DNS, and implement loading balance between services through domain name calls.

• Centralized traffic forwarding based on SLB. Calls are directly forwarded to SLB, and SLB implements traffic forwarding between services.

• Implement loading balance and call forwarding based on the Kubernetes Service microservice system and relying on Kubernetes ClusterIP.

In this example, we adopt a DNS custom domain name architecture to upgrade from Spring Boot to Spring Cloud. The following figure shows the application architecture before and after the upgrade. For more information, see Github source code[2].

1

Spring Boot architecture before upgrade👆

2

Spring Cloud architecture after upgrade👆

Version Mapping between Spring Boot and Spring Cloud Alibaba

Select a compatible Spring Cloud Alibaba version based on the Spring Boot version you are using:

Spring Boot Version Spring Cloud Alibaba Version Spring Cloud Version
3.0.2 2022.0.0.0 Spring Cloud 2022.0.0
3.0.2 2022.0.0.0-RC2 Spring Cloud 2022.0.0
3.0.0 2022.0.0.0-RC1 Spring Cloud 2022.0.0
2.6.13 2021.0.5.0 Spring Cloud 2021.0.5
2.6.11 2021.0.4.0 Spring Cloud 2021.0.4
2.6.3 2021.0.1.0 Spring Cloud 2021.0.1
2.4.2 2021.1 Spring Cloud 2020.0.1
2.3.12.RELEASE 2.2.10-RC1 Spring Cloud Hoxton.SR12
2.3.12.RELEASE 2.2.9.RELEASE Spring Cloud Hoxton.SR12
2.3.12.RELEASE 2.2.8.RELEASE Spring Cloud Hoxton.SR12
2.3.12.RELEASE 2.2.7.RELEASE Spring Cloud Hoxton.SR12
2.3.2.RELEASE 2.2.6.RELEASE Spring Cloud Hoxton.SR9
2.2.5.RELEASE 2.2.1.RELEASE Spring Cloud Hoxton.SR3
2.2.X.RELEASE 2.2.0.RELEASE Spring Cloud Hoxton.RELEASE
2.1.13.RELEASE 2.1.4.RELEASE Spring Cloud Greenwich.SR6
2.1.X.RELEASE 2.1.2.RELEASE Spring Cloud Greenwich
2.0.X.RELEASE 2.0.4.RELEASE (End of support. We recommend that you upgrade to the latest version.) Spring Cloud Finchley
1.5.X.RELEASE 1.5.1.RELEASE (End of support. We recommend that you upgrade to the latest version.) Spring Cloud Edgware

Spring Cloud Alibaba Starters List and User Manual

• spring-cloud-starter-alibaba-nacos-discovery[3]
• spring-cloud-starter-alibaba-nacos-config[4]
• spring-cloud-starter-alibaba-nacos-sentinel[5]
• spring-cloud-starter-alibaba-nacos-rocketmq[6]
• spring-cloud-starter-alibaba-nacos-seata[7]

Spring Cloud Alibaba Integrated Component Versions

The following table shows each Spring Cloud Alibaba version and its corresponding components:

Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version
2022.0.0.0 1.8.6 2.2.1 4.9.4 ~ 1.7.0
2022.0.0.0-RC2 1.8.6 2.2.1 4.9.4 ~ 1.7.0-native-rc2
2021.0.5.0 1.8.6 2.2.0 4.9.4 ~ 1.6.1
2.2.10-RC1 1.8.6 2.2.0 4.9.4 ~ 1.6.1
2022.0.0.0-RC1 1.8.6 2.2.1-RC 4.9.4 ~ 1.6.1
2.2.9.RELEASE 1.8.5 2.1.0 4.9.4 ~ 1.5.2
2021.0.4.0 1.8.5 2.0.4 4.9.4 ~ 1.5.2
2.2.8.RELEASE 1.8.4 2.1.0 4.9.3 ~ 1.5.1
2021.0.1.0 1.8.3 1.4.2 4.9.2 ~ 1.4.2
2.2.7.RELEASE 1.8.1 2.0.3 4.6.1 2.7.13 1.3.0
2.2.6.RELEASE 1.8.1 1.4.2 4.4.0 2.7.8 1.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE 1.8.0 1.3.3 4.4.0 2.7.8 1.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.2.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

References

[1] Version mapping table
https://sca.aliyun.com/en-us/docs/next/best-practice/spring-boot-to-spring-cloud/
[2] Github source code
https://github.com/spring-cloud-alibaba-group/springboot-transfer-to-springcloud
[3] spring-cloud-starter-alibaba-nacos-discovery
https://sca.aliyun.com/en-us/docs/next/user-guide/nacos/quick-start/#%E6%8E%A5%E5%85%A5-nacos-%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0
[4] spring-cloud-starter-alibaba-nacos-config
https://sca.aliyun.com/en-us/docs/next/user-guide/nacos/quick-start/#%E6%8E%A5%E5%85%A5-nacos-%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83
[5] spring-cloud-starter-alibaba-nacos-sentinel
https://sca.aliyun.com/en-us/docs/next/user-guide/sentinel/quick-start/
[6] spring-cloud-starter-alibaba-nacos-rocketmq
https://sca.aliyun.com/en-us/docs/next/user-guide/rocketmq/quick-start/
[7] spring-cloud-starter-alibaba-nacos-seata
https://sca.aliyun.com/en-us/docs/next/user-guide/seata/quick-start/

0 1 0
Share on

You may also like

Comments

Related Products