Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 16, 2020 12:01 am GMT

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@EnableEurekaServerpublic 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=localhosteureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseserver.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@EnableDiscoveryClientpublic 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-microserviceeureka.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@EnableDiscoveryClientpublic 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-webeureka.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


Original Link: https://dev.to/eidher/microservices-with-spring-5a6p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To