Microservice - Spring Cloud Netflix Eureka Service Registry
Homejavacourse-spring

Microservice - Spring Cloud Netflix Eureka Service Registry

mas template

Let’s go ahead and create our service registry project (terawarehouse-service-discovery). A service registry like Netflix Eureka is a fixed ...


Let’s go ahead and create our service registry project (terawarehouse-service-discovery). A service registry like Netflix Eureka is a fixed point that allows the services to find and communicate with each other without knowing the hostname nor the port but with only the Spring application name. Eureka enables client-side load balancing and with a peer setup each client can act as a server as each client communicates and synchronize with each other.

Let’s start with:

  1. Setting up the Eureka Server - terawarehouse-service-discovery
  2. Create a new SpringBoot project.
  3. Add dependency to spring-cloud-starter-netflix-eureka-server which will provide the necessary dependencies to make this project a service registry server.
  4. Add dependencies spring-boot-starter-web and spring-boot-starter-actuator to let us display the server information.
  5. Annotate SpringBootApplication with EnableEurekaServer.
  6. Create an application.yml configuration file with 2 profiles. Notice that I use different network IP for each profile. Don’t register the eureka server as a client.
    1. instance1 - which we will deploy on our machine
    2. instance2 - which we will deploy on another machine
  7. Run the config server.
  8. Run 2 instances with different profiles.
  9. Check that they are a replica of each other.

Configuring the Client 

  1. Add dependency to spring-cloud-starter-netflix-eureka-client. This will provide the needed dependencies for this project to connect to the service discovery server.
  2. Annotate the SpringBoot class with EnableDiscoveryClient.
  3. And finally add the eureka client information to our project’s configuration:
    eureka.client.register-with-eureka: true
    eureka.client.fetch-registry: true
    eureka.client.service-url.defaultZone:
    http://192.168.1.100:8761/eureka,http://192.168.1.101:8761/eureka
    eureka.client.instance.prefer-ip-address: true
    eureka.client.instance.ip-address: 192.168.1.100
  4. Run the client.
  5. Check if the client properly registers with the eureka server.
Later on, as we introduce new microservices (inventory, order, etc) we can use this service registry for the catalog to send a request to the inventory or simply allow REST communication between them with only the spring application name.

In the next lesson, we will introduce a server-side load balancer that will receive the requests from a client and forward it to the appropriate API using the service discovery information.

I hope everything is clear to this point. You can always ask questions in the comment section below and I’ll be sure to address them.

If you want to get notified when I upload the next video, please subscribe to my channel and hit the bell icon. That will validate that what I’m doing is indeed helpful and will definitely inspire me to create more similar learning videos.

I hope to catch you in the next one. Thanks for watching and have a nice day. Bye.

References

server:
address: 192.168.1.100
eureka:
instance:
hostname: ${server.address}
client:
service-url:
defaultZone: http://192.168.1.101:8761/eureka
server:
address: 192.168.1.101
port: 8761
eureka:
instance:
hostname: ${server.address}
client:
service-url:
defaultZone: http://192.168.1.100:8761/eureka
spring:
application:
name: terawarehouse-service-discovery
profiles:
active:
- ${spring.profiles.active}
server:
port: 8761
eureka:
environment: ${spring.profiles.active}
client:
register-with-eureka: false
fetch-registry: false
instance:
prefer-ip-address: true
appname: eureka
view raw application.yml hosted with ❤ by GitHub
# app
spring.application.name=catalog
server.port=8001
spring.main.banner-mode=off
spring.main.allow-bean-definition-overriding=true
management.endpoints.web.exposure.include=*
# api
server.servlet.context-path=/api/v1/
spring.data.rest.base-path=/api/spring/rest/
# data source
spring.datasource.url=jdbc:postgresql://localhost:5432/tw_catalog
spring.datasource.username=kerri
spring.datasource.password=kerri
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#spring.jpa.hibernate.ddl-auto=create-drop
spring.liquibase.change-log=classpath:db/changelog/db.changelog-rebuild.xml
# basic auth credentials
spring.security.user.name=client
spring.security.user.password=client
# config to connect to admin server
spring.boot.admin.client.url=http://localhost:9990
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
# config to send info to admin server
spring.boot.admin.client.instance.metadata.user.name=${spring.boot.admin.client.username}
spring.boot.admin.client.instance.metadata.user.password=${spring.boot.admin.client.username}
# discovery client, will fail if not configured here
eureka.client.register-with-eureka: true
eureka.client.fetch-registry: true
eureka.client.service-url.defaultZone: http://192.168.1.100:8761/eureka,http://192.168.1.101:8761/eureka
eureka.client.instance.prefer-ip-address: true
eureka.client.instance.ip-address: 192.168.1.100
#spring.h2.console.enabled=true
#logging.level.org.springframework=OFF
#logging.level.root=OFF
#logging.level.com.terawarehouse=debug
#logging.level.com.broodcamp=debug
/**
* An Open Source Inventory and Sales Management System
* Copyright (C) 2019 Edward P. Legaspi (https://github.com/czetsuya)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.terawarehouse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.broodcamp.bean.ExtendedJPARepositoryFactoryBean;
import com.broodcamp.data.repository.BaseRepositoryImpl;
/**
* @author Edward P. Legaspi | czetsuya@gmail.com
*/
@SpringBootApplication
@EnableJpaRepositories( //
basePackages = { "com.broodcamp.data.repository", "com.terawarehouse.data.repository" } //
, repositoryBaseClass = BaseRepositoryImpl.class, repositoryFactoryBeanClass = ExtendedJPARepositoryFactoryBean.class)
@EntityScan(basePackages = { "com.broodcamp.data.entity", "com.terawarehouse.data.entity" })
@ComponentScan(basePackages = { "com.broodcamp", "com.terawarehouse" })
@EnableDiscoveryClient
public class TerawarehouseCatalogApplication {
public static void main(String[] args) {
SpringApplication.run(TerawarehouseCatalogApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaServer
public class TerawarehouseServiceDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(TerawarehouseServiceDiscoveryApplication.class, args);
}
}
/**
* An Open Source Inventory and Sales Management System
* Copyright (C) 2019 Edward P. Legaspi (https://github.com/czetsuya)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.terawarehouse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.broodcamp.bean.ExtendedJPARepositoryFactoryBean;
import com.broodcamp.data.repository.BaseRepositoryImpl;
/**
* @author Edward P. Legaspi | czetsuya@gmail.com
*/
@SpringBootApplication
@EnableJpaRepositories( //
basePackages = { "com.broodcamp.data.repository", "com.terawarehouse.data.repository" } //
, repositoryBaseClass = BaseRepositoryImpl.class, repositoryFactoryBeanClass = ExtendedJPARepositoryFactoryBean.class)
@EntityScan(basePackages = { "com.broodcamp.data.entity", "com.terawarehouse.data.entity" })
@ComponentScan(basePackages = { "com.broodcamp", "com.terawarehouse" })
@EnableDiscoveryClient
public class TerawarehouseCatalogApplication {
public static void main(String[] args) {
SpringApplication.run(TerawarehouseCatalogApplication.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RC1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.terawarehouse</groupId>
<artifactId>terawarehouse-service-discovery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>terawarehouse-service-discovery</name>
<description>Terawarehouse Service Discovery</description>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.terawarehouse</groupId>
<artifactId>terawarehouse-bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
  1. What an excellent blog; I especially liked the section where you advised us to write in a less formal tone. A Netflix clone that will help you start your own video streaming business. Video streaming is on the rise, and so is its related app and clone development. Undoubtedly, launching apps and their clones are not as easy as it sounds. Thus, there is a need to find a solution that can help ease the launch of the video streaming app Clone.

    Reply Delete

Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Microservice - Spring Cloud Netflix Eureka Service Registry
Microservice - Spring Cloud Netflix Eureka Service Registry
https://i.ytimg.com/vi/y-ekG7XGSbg/0.jpg
https://i.ytimg.com/vi/y-ekG7XGSbg/0.jpg
toztech
https://toztech.blogspot.com/2019/10/microservice-spring-cloud-netflix.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2019/10/microservice-spring-cloud-netflix.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content