DEV Community

eidher
eidher

Posted on • Updated on

Microservices with Spring

Spring Cloud provides tools for Cloud and Microservice applications. Tools as Microservice Infrastructure (Service Discovery, Configuration and Monitoring), Platform Support, and Starters.

We need a Service Discovery to allow the services to find other services. Spring Cloud supports Netflix Eureka, Hashicorp Consul, and Apache Zookeeper. The Discovery Server may return the location of multiple instances. Therefore, we need a load balancer. e.g. Netflix Ribbon.

In the next example, I'll create three projects: the discovery server, the producer, and the consumer.

1.Run a Discovery Service

Add the Maven Dependency for Eureka Server

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

Implement the registry service

Add the @EnableEurekaServer annotation

@SpringBootApplication
@EnableEurekaServer
public class RegistrationServer extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(RegistrationServer.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add the properties

eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
server.port=1111
Enter fullscreen mode Exit fullscreen mode

Eureka

Alt Text

2.Run a Microservice

Add the Maven Dependencies for Spring Cloud and Eureka Client

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter</artifactId>
  </dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
Enter fullscreen mode Exit fullscreen mode

Performs Service Registration

Add the @EnableDiscoveryClient annotation

@SpringBootApplication
@EnableDiscoveryClient
public class AccountsMicroservice {

    public static void main(String[] args) {
        SpringApplication.run(AccountsMicroservice.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add the @RestController methods

    @GetMapping("/accounts/{id}")
    public Account byId(@PathVariable("id") Long id) {
        return accountRespository.getAccount(id);
    }
    ...
Enter fullscreen mode Exit fullscreen mode

Add the properties

spring.application.name=accounts-microservice
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=2222
Enter fullscreen mode Exit fullscreen mode

Eureka

Alt Text

3.Find the Microservice

Add the Maven Dependencies for Spring Cloud and Eureka Client

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter</artifactId>
  </dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
Enter fullscreen mode Exit fullscreen mode

Enable the Consumer to find the Producer

Add the @EnableDiscoveryClient annotation

@SpringBootApplication
@EnableDiscoveryClient
public class AccountsWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccountsWebApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Enter fullscreen mode Exit fullscreen mode

Use the Producer

public class RemoteAccountManager implements AccountService {

    @Autowired
    @LoadBalanced
    RestTemplate restTemplate;

    public Account getAccount(Long id) {
        return restTemplate.getForObject("http://ACCOUNTS-MICROSERVICE/accounts/{id}",
                Account.class, id);
    }

    ...
}
Enter fullscreen mode Exit fullscreen mode

Use the Service from the @Controller

    @GetMapping("/accountDetails")
    public String accountDetails(@RequestParam("entityId") long id, Model model) {
        model.addAttribute("account", accountManager.getAccount(id));
        return "accountDetails";
    }
    ...
Enter fullscreen mode Exit fullscreen mode

Add the properties

spring.application.name=accounts-web
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
Enter fullscreen mode Exit fullscreen mode

Eureka

Alt Text

Learn more: https://spring.io/blog/2015/07/14/microservices-with-spring

Top comments (1)

Collapse
 
l222p profile image
l222p

Are there tools like spring clod on node using express??