Getting started

更新时间:
复制 MD 格式

This topic describes how to use Spring Cloud SOFA Registry.

Existing projects

If you have an existing Spring Cloud project that is integrated with a registry center, such as Nacos, Consul, or Eureka, follow the steps in Integrate with Spring Cloud SOFA to complete the common configurations. Then, replace the dependency of the other registry center with the Spring Cloud SOFA Registry dependency.

<!--<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>-->
<dependency>
     <groupId>com.alipay.cloud</groupId>
     <artifactId>spring-cloud-starter-sofa-registry</artifactId>
</dependency>

Spring Cloud SOFA Registry requires a load balancing module. The registry center module does not automatically import one because Spring Cloud SOFA supports multiple versions of Spring Cloud, and each version supports different load balancing modules. Therefore, you must add a dependency for either Ribbon or LoadBalancer to your project.

The following table describes the load balancing modules supported by different Spring Cloud versions:

Version

Ribbon support

LoadBalancer support

Greenwich

No

Yes (since SR3)

Hoxton

Yes

Yes

2020.X

No

Yes

2021.X

No

Yes

Example of a manual import:

  • Add Ribbon

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
  • Importing a LoadBalancer

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
  • Importing a LoadBalancer (Greenwich version)

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-loadbalancer</artifactId>
    </dependency>
Important

The Greenwich and Hoxton versions of Spring Cloud support both the Ribbon and LoadBalancer frameworks. Ribbon is the default load balancing framework. To use the LoadBalancer framework in these versions, you must configure it correctly. For more information, see the official Spring Cloud documentation.

No projects

To get started with the SOFA Registry, follow the steps below:

  1. Create a Spring Cloud project and add the dependencies for Spring Cloud SOFA Registry and a load balancer.

    Note

    Do not specify a version because the version is controlled by dependencyManagement.

    <dependency>
        <groupId>com.alipay.cloud</groupId>
        <artifactId>spring-cloud-starter-sofa-registry</artifactId>
    </dependency>
    <!-- The following example uses LoadBalancer. Select a load balancer based on your Spring Cloud version. -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
  2. In the project's startup class, use the EnableDiscoveryClient annotation to enable service discovery and the EnableFeignClients annotation to enable OpenFeign.

    image.png

  3. In the project, use OpenFeign or RestTemplate to call other services.

    The following example uses OpenFeign:

    image.png

  4. In the application.properties file, set provider.application.name to the service name of the provider.

    image.png

  5. Create another Spring Cloud project to use as the provider test application. Set the application's spring.application.name to MySpringCloudProviderApplication. In the startup class, add the EnableDiscoveryClient annotation to enable automatic service registration. Write a Controller to handle requests (details are omitted here).

  6. Add the required common configurations, then start the application to test it.

    For more information about the required common configurations, see Add common configurations.