Microservices architecture design using Zuul and Eureka - part 2
VIDEO:
VIDEO
Module
Used For
Module Type
Port
Registered Name
Dependencies added
products-db
Eureka Client
SpringBoot Module
9000
products-db
Web, Discovery Client,JPA,Mysql
1. products-db
application.yml
spring:
application:
name: products-db
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:9002/eureka/
instance:
hostname: localhost
Application.properties
#server
server.port=9000
spring.datasource.url= jdbc:mysql://localhost:3306/test1
spring.datasource.username=sachin
spring.datasource.password=Sachin@123
#spring.jpa.hibernate.ddl-auto=create-drop
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=150
# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#logging
logging.level.org.springframework.web=ERROR
com.sachin4java.productsdb=DEBUG
logging.file=/home/sachin/log/url-scanner.log
Endpoints:
Get All products
GET
http://localhost:9000/products
Create Product
POST
http://localhost:9000/products/product
Get products by category
GET
http://localhost:9000/products/categories/{categoryname}
ProductsDbApplication.java
package com.sachin4java.productsdb;
import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@EnableEurekaClient
@SpringBootApplication
public class ProductsDbApplication {
public static void main(String[] args) {
SpringApplication.run(ProductsDbApplication.class, args);
}
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
ProductEntity.java
package com.sachin4java.productsdb.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="products")
public class ProductEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name="category_name")
private String categoryName;
@Column(name="product_name")
private String productName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
Product.java
package com.sachin4java.productsdb.model;
public class Product {
private String categoryName;
private String productName;
public Product() {
// TODO Auto-generated constructor stub
}
public Product(String categoryName, String productName) {
this.categoryName = categoryName;
this.productName = productName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
2. Products-crawler
Module
Used For
Module Type
Port
Registered Name
Dependencies added
products-crawler
Eureka Client
SpringBoot Module
9001
products-crawler
Web, Discovery Client
application.yml
spring:
application:
name: products-crawler
server:
port: 9001
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:9002/eureka/
instance:
hostname: localhost
Endpoints:
crawlByProductName
GET
http://localhost:9001/crawlers/products/product/{Banana}
crawlByCategoryName
GET
http://localhost:9001/crawlers/products/categories/category/{categoryname}
Get Crawl Data:
private List<ProductCrawl> getCrawlData() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<ProductCrawl>> crawlData = restTemplate.exchange("https://next.json-generator.com/api/json/get/4k2T-g0z4",
HttpMethod.GET, null, new ParameterizedTypeReference<List<ProductCrawl>>() {
});
List<ProductCrawl> productCrawls = crawlData.getBody();
return productCrawls;
}
Module products-db -->Repository
https://github.com/ranesaci/products-db.git