Saturday 30 December 2017

Microservices architecture design using Zuul and Eureka - part 5

Microservices architecture design using Zuul and Eureka - part 5


VIDEO:



Module
Used For
Module Type
Port
Registered Name
Dependencies added
zuul-module-service
API Gateway(Zuul Sever)
SpringBoot Module
9003
zuul-server
Web, Discovery Client,Zuul server


  1. zuul-module-service

application.yml

server:
 port: 9003
 
spring:
 application:
   name: zuul-server


eureka:
 client:
   serviceUrl:
     defaultZone: http://localhost:9002/eureka/


zuul:
 #Service will be mapped under the /api URI
 prefix: /api
#  Uncomment to disable auto-registering all services read from Eureka
#  ignoredServices: '*'
#  use url attribute to use service URL instead of serviceId
 routes:
   PRODUCTS-CRAWLER:
     path: /crawler-service/**
     serviceId: PRODUCTS-CRAWLER
   PRODUCTS-DB:
     path: /prducts-db-service/**
     serviceId: PRODUCTS-DB


ZuulModuleServiceApplication.java
package com.sachin4java.zuulmoduleservice;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;


@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulModuleServiceApplication {


public static void main(String[] args) {
SpringApplication.run(ZuulModuleServiceApplication.class, args);
}
}


Module zuul-module-service --> https://github.com/ranesaci/zuul-module-service.git

Microservices architecture design using Zuul and Eureka - part 4

Microservices architecture design using Zuul and Eureka - part 4


VIDEO:






eureka-server-module



Module
Used For
Module Type
Port
Registered Name
Dependencies added
eureka-server-module
Service Registry(Eureka Server)
SpringBoot Module
9002
eureka-service
Web, Eureka Server


application.properties
server.port=9002
spring.application.name=eureka-service
#Eureka server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false


logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF


EurekaServerModuleApplication.java


package com.sachin4java.eurekaservermodule;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


@EnableEurekaServer
@SpringBootApplication
public class EurekaServerModuleApplication {


public static void main(String[] args) {
SpringApplication.run(EurekaServerModuleApplication.class, args);
}
}

Module eureka-server-module  --> https://github.com/ranesaci/eureka-server-module.git

Microservices architecture design using Zuul and Eureka - part 3

Microservices architecture design using Zuul and Eureka - part 3


VIDEO:





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;
}


ProductCrawl.java
package com.sachin4java.productscrawler.model;


import java.util.Map;


public class ProductCrawl {
private String productName;
private Map<String, String> prices;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Map<String, String> getPrices() {
return prices;
}
public void setPrices(Map<String, String> prices) {
this.prices = prices;
}


}

Module products-crawler --> Repository https://github.com/ranesaci/products-crawler.git

Microservices architecture design using Zuul and Eureka - part 2

Microservices architecture design using Zuul and Eureka - part 2




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

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...