Saturday, September 9, 2017

NetFlix Spring Boot ZUUL Example

tp-eureka-service
==================

EurekaServiceApplication.java
=============================
package com.techprimers.cloud;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

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

application.properties
======================
spring.application.name=eureka-service
server.port=8070


application.yml
===============
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
      waitTimeInMsWhenSyncEmpty: 0
  instance:
    hostname: localhost  

pom.xml
=======
(?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 http://maven.apache.org/xsd/maven-4.0.0.xsd")
(modelVersion)4.0.0(/modelVersion)
(groupId)com.techprimers.cloud(/groupId)
(artifactId)eureka-service(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)
(name)eureka-service(/name)
(description)Demo project for Spring Boot(/description)
(parent)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-parent(/artifactId)
(version)1.5.3.RELEASE(/version)
(relativePath/) (!-- lookup parent from repository --)
(/parent)
(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(project.reporting.outputEncoding)UTF-8(/project.reporting.outputEncoding)
(java.version)1.8(/java.version)
(/properties)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-eureka-server(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-test(/artifactId)
(scope)test(/scope)
(/dependency)
(/dependencies)
(dependencyManagement)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-dependencies(/artifactId)
(version)Dalston.RELEASE(/version)
(type)pom(/type)
(scope)import(/scope)
(/dependency)
(/dependencies)
(/dependencyManagement)
(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(/plugins)
(/build)
(/project)

Test:
====
http://localhost:8070/

---------------------------------------------------------------------------------------------------------------------

tp-hello-server
===============

HelloResource.java
==================
package com.techprimers.cloud;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest/hello/server")
public class HelloResource {

    @GetMapping
    public String hello() {
        return "Hello World!";
    }
    @GetMapping(path="/1")
    public String hello1() {
        return "Hello World!1";
    }
    @GetMapping(path="/1/2")
    public String hello2() {
        return "Hello World!2";
    }
    @GetMapping(path="/1/2/3")
    public String hello3() {
        return "Hello World!3";
    }
}

HelloServerApplication.java
===========================
package com.techprimers.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class HelloServerApplication {

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



application.yml
===============
spring:
  application:
    name: hello-server

server:
  port: 8071

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/
  instance:
    hostname: localhost

pom.xml
=======
(?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 http://maven.apache.org/xsd/maven-4.0.0.xsd")
(modelVersion)4.0.0(/modelVersion)
(groupId)com.techprimers.cloud(/groupId)
(artifactId)hello-server(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)
(name)hello-server(/name)
(description)Demo project for Spring Boot(/description)
(parent)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-parent(/artifactId)
(version)1.5.3.RELEASE(/version)
(relativePath/) (!-- lookup parent from repository --)
(/parent)
(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(project.reporting.outputEncoding)UTF-8(/project.reporting.outputEncoding)
(java.version)1.8(/java.version)
(/properties)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-eureka(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-test(/artifactId)
(scope)test(/scope)
(/dependency)
(/dependencies)
(dependencyManagement)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-dependencies(/artifactId)
(version)Dalston.RELEASE(/version)
(type)pom(/type)
(scope)import(/scope)
(/dependency)
(/dependencies)
(/dependencyManagement)
(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(/plugins)
(/build)
(/project)

Test:
=====
http://localhost:8071/rest/hello/server

Hello World!!

-------------------------------------------------------------------------------------------------------------------------------------

tp-hello-client
===============
HelloClientApplication.java
===========================
package com.techprimers.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class HelloClientApplication {

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


@Configuration
class Config {

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

}

HelloResource.java
==================
package com.techprimers.cloud;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/rest/hello/client")
public class HelloResource {


    @Autowired
    private RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "fallback", groupKey = "Hello",
            commandKey = "hello",
            threadPoolKey = "helloThread"
            )
    @GetMapping
    public String hello() {
        String url = "http://hello-server/rest/hello/server";
        return restTemplate.getForObject(url, String.class)+ " including client";
    }

    public String fallback(Throwable hystrixCommand) {
        return "Fall Back Hello world";
    }

}


application.yml
===============
spring:
  application:
    name: hello-client

server:
  port: 8072

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/
  instance:
    hostname: localhost

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000


pom.xml:
========
(?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 http://maven.apache.org/xsd/maven-4.0.0.xsd")
(modelVersion)4.0.0(/modelVersion)
(groupId)com.techprimers.cloud(/groupId)
(artifactId)hello-client(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)
(name)hello-client(/name)
(description)Demo project for Spring Boot(/description)
(parent)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-parent(/artifactId)
(version)1.5.3.RELEASE(/version)
(relativePath/) (!-- lookup parent from repository --)
(/parent)
(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(project.reporting.outputEncoding)UTF-8(/project.reporting.outputEncoding)
(java.version)1.8(/java.version)
(/properties)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-eureka(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-hystrix(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-test(/artifactId)
(scope)test(/scope)
(/dependency)
(/dependencies)
(dependencyManagement)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-dependencies(/artifactId)
(version)Dalston.RELEASE(/version)
(type)pom(/type)
(scope)import(/scope)
(/dependency)
(/dependencies)
(/dependencyManagement)
(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(/plugins)
(/build)
(/project)

Test:
=======
http://localhost:8072/rest/hello/client

Hello World! including client

---------------------------------------------------------------------------------------------------------------------------------------------------------

tp-zuul-service
===============

ZuulServiceApplication.java
======================
package com.techprimers.cloud;

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;

@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulServiceApplication {

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

application.yml
===============
zuul:
  #Service will be mapped under the /api URI
  prefix: /api
  routes:
    hello-server-url:
      path: /server/**
      url: http://localhost:8071
    hello-server-eurekaId:
      path: /server1/**
      url: HELLO-SERVER
    hello-client:
      path: /client/**
      serviceId: HELLO-CLIENT

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/
  instance:
    hostname: localhost

server:
  port: 8079

spring:
  application:
    name: zuul-service

# Increase the Hystrix timeout to 60s (for all)
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000


pom.xml
=======
(?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 http://maven.apache.org/xsd/maven-4.0.0.xsd")
(modelVersion)4.0.0(/modelVersion)
(groupId)com.techprimers.cloud(/groupId)
(artifactId)zuul-service(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)
(name)zuul-service(/name)
(description)Demo project for Spring Boot(/description)
(parent)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-parent(/artifactId)
(version)1.5.3.RELEASE(/version)
(relativePath/) (!-- lookup parent from repository --)
(/parent)
(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(project.reporting.outputEncoding)UTF-8(/project.reporting.outputEncoding)
(java.version)1.8(/java.version)
(/properties)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-eureka(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-zuul(/artifactId)
(/dependency)
(dependency)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-test(/artifactId)
(scope)test(/scope)
(/dependency)
(/dependencies)
(dependencyManagement)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-dependencies(/artifactId)
(version)Dalston.RELEASE(/version)
(type)pom(/type)
(scope)import(/scope)
(/dependency)
(/dependencies)
(/dependencyManagement)
(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(/plugins)
(/build)
(/project)

Test:
=====
http://localhost:8079/api/server1//rest/hello/server/1/2/3
Hello World!3

http://localhost:8079/api/server1//rest/hello/server/1/2
Hello World!2

http://localhost:8079/api/server1//rest/hello/server/1
Hello World!1

http://localhost:8079/api/server1//rest/hello/server
Hello World!

http://localhost:8079/api/client/rest/hello/client
Hello World! including client












No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...