Microservices with Spring
Tags: microservices, spring, java, eureka
Published: 2020-11-16 00:01:05

[Spring Cloud](https://spring.io/projects/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
```java
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
```
##Implement the registry service

Add the @EnableEurekaServer annotation

```java
@SpringBootApplication
@EnableEurekaServer
public class RegistrationServer extends SpringBootServletInitializer {

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

##Add the properties
```java
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
server.port=1111
```

##Eureka
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2pfgb8mqcim4tpaxb174.png)

#2.Run a Microservice

##Add the Maven Dependencies for Spring Cloud and Eureka Client
```java
 <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

```java
@SpringBootApplication
@EnableDiscoveryClient
public class AccountsMicroservice {

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

##Add the @RestController methods
```java
	@GetMapping("/accounts/{id}")
	public Account byId(@PathVariable("id") Long id) {
		return accountRespository.getAccount(id);
	}
	...
```

##Add the properties
```java
spring.application.name=accounts-microservice
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=2222
```

##Eureka
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w7nq7hhgel4coxc8b13r.png)

#3.Find the Microservice

##Add the Maven Dependencies for Spring Cloud and Eureka Client
```java
 <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

```java
@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

```java
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
```java
	@GetMapping("/accountDetails")
	public String accountDetails(@RequestParam("entityId") long id, Model model) {
		model.addAttribute("account", accountManager.getAccount(id));
		return "accountDetails";
	}
	...
```

##Add the properties
```java
spring.application.name=accounts-web
eureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
```

##Eureka

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k1g6d76ozjccvd5w0vo7.png)

Learn more: [https://spring.io/blog/2015/07/14/microservices-with-spring](https://spring.io/blog/2015/07/14/microservices-with-spring)