An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
Some of Our Sources
View All SourcesMicroservices 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>
Implement the registry service
Add the @EnableEurekaServer annotation
@SpringBootApplication@EnableEurekaServerpublic class RegistrationServer extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(RegistrationServer.class, args); }}
Add the properties
eureka.instance.hostname=localhosteureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseserver.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@EnableDiscoveryClientpublic 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-microserviceeureka.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@EnableDiscoveryClientpublic 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-webeureka.client.serviceUrl.defaultZone: http://localhost:1111/eureka/
Eureka
Learn more: https://spring.io/blog/2015/07/14/microservices-with-spring
Original Link: https://dev.to/eidher/microservices-with-spring-5a6p
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To