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>
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);
}
}
Add the properties
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
server.port=1111
Eureka
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>
Performs Service Registration
Add the @EnableDiscoveryClient annotation
@SpringBootApplication
@EnableDiscoveryClient
public class AccountsMicroservice {
public static void main(String[] args) {
SpringApplication.run(AccountsMicroservice.class, args);
}
}
Add the @RestController methods
@GetMapping("/accounts/{id}")
public Account byId(@PathVariable("id") Long id) {
return accountRespository.getAccount(id);
}
...
Add the properties
spring.application.name=accounts-microservice
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=2222
Eureka
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>
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();
}
}
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);
}
...
}
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";
}
...
Add the properties
spring.application.name=accounts-web
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
Eureka
Learn more: https://spring.io/blog/2015/07/14/microservices-with-spring
Top comments (1)
Are there tools like spring clod on node using express??