Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Spring Cloud Client Service

Tech 1

The client module serves as the actual provider of services within a Spring Cloud environment. It registers itself with the service registry (Eureka server) to make its capabilities available to other components.

Creating the client module follows a similar process to setting up the registry:

  1. Create a new module using the quickstart template and proceed to the next step.
  2. Leave the group ID and parent location fields empty. Set the artifact ID to any valid name (for example: springcloud-eureka-support).
  3. After proceeding, ensure the project path is correctly configured.
  4. Once created, you should see two modules in your project structure.

Next, configure the pom.xml for the service support module. It mirrors the configuration of the server's pom.xml, with one key difference: replace the server dependency with the client dependency.

Similar to the first microservice, we need to set up the main application class and the application.yml file. Since this is a service provider, a controller must also be implemented.

Here's the content for application.yml:

server:
  port: 8701
 
# Specifies the Eureka client registration endpoint
spring:
  application:
    name: eureka-service

# Eureka client settings
eureka:
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8700/eureka
  instance:
    hostname: localhost

Update the pom.xml with the following dependencies:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.1.RELEASE</version>
  <relativePath/>
</parent>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- Eureka client dependency -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Hoxton.SR6</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Ensure compatibility between Spring Boot and Spring Cloud versions. Refer to https://start.spring.io/actuator/info for version mapping. In this case, Spring Boot 2.3.1.RELEASE corresponds to Spring Cloud Hoxton.SR6.

Implement a basic controller to expose the service:

package com.yun;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Hello")
public class Controller {
    @RequestMapping("/World")
    public String helloWorld(String s) {
        System.out.println("Received value: " + s);
        return "Received value: " + s;
    }
}

Create the main application class and enable discovery:

package com.yun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

Run the EurekaServiceApplication class. After startup, navigate to http://localhost:8700/ to confirm that the service has been registered with the Eureka server.

Accessing the service directly at http://localhost:8701/Hello/World?s=John confirms it is operational.

In production environments, service are typically accessed through the Eureka server rather than direct. The server provides a list of available service instances, which may form a cluster. Load balancing strategies determine how requests are distributed among these instances. Common algorithms include round-robin, random selection, and weighted distribution based on server capacity.

These load-balancing mechanisms allow consumers to select an appropriate service instance dynamical, enabling scalability and fault tolerance.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.