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:
- Setting up the Eureka Server - terawarehouse-service-discovery
- Create a new SpringBoot project.
- Add dependency to spring-cloud-starter-netflix-eureka-server which will provide the necessary dependencies to make this project a service registry server.
- Add dependencies spring-boot-starter-web and spring-boot-starter-actuator to let us display the server information.
- Annotate SpringBootApplication with EnableEurekaServer.
- 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.
- instance1 - which we will deploy on our machine
- instance2 - which we will deploy on another machine
- Run the config server.
- Run 2 instances with different profiles.
- Check that they are a replica of each other.
Configuring the Client
- 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.
- Annotate the SpringBoot class with EnableDiscoveryClient.
- 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 - Run the client.
- Check if the client properly registers with the eureka server.
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
Repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server: | |
address: 192.168.1.100 | |
eureka: | |
instance: | |
hostname: ${server.address} | |
client: | |
service-url: | |
defaultZone: http://192.168.1.101:8761/eureka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server: | |
address: 192.168.1.101 | |
port: 8761 | |
eureka: | |
instance: | |
hostname: ${server.address} | |
client: | |
service-url: | |
defaultZone: http://192.168.1.100:8761/eureka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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.
Delete