Eureka Client1 Base Code

Pending:
Test Cases
This commit is contained in:
Kunwar
2020-05-11 12:56:18 +05:30
parent 09b524fefe
commit 3f71752f53
5 changed files with 147 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javadevjournal</groupId>
<artifactId>eurekaclient1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient1</name>
<description>Spring Cloud Netflix Eureka Client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>

View File

@@ -0,0 +1,14 @@
package com.javadevjournal.eurekaclient1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDiscoveryClient
@SpringBootApplication
public class Eurekaclient1Application {
public static void main(String[] args) {
SpringApplication.run(Eurekaclient1Application.class, args);
}
}

View File

@@ -0,0 +1,40 @@
package com.javadevjournal.eurekaclient1.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class Eurekaclient1Controller {
@Autowired
private LoadBalancerClient loadBalancerClient;
private RestTemplate restTemplate = new RestTemplate();
@RequestMapping("/calleurekaclient1")
public ResponseEntity<String> callEurekaClient1(){
return new ResponseEntity<String>("Hello From Client 1 ", HttpStatus.OK);
}
@RequestMapping("/callEurekaClient2viaClient1")
public ResponseEntity<String> callClient2(){
try {
return new ResponseEntity<String>(
restTemplate.getForObject(getEurkaClient2BaseUri() + "/calleurekaclient2", String.class), HttpStatus.OK);
}catch (Exception exp) {
return new ResponseEntity<String>(
restTemplate.getForObject(getEurkaClient2BaseUri() + "/calleurekaclient2", String.class), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private String getEurkaClient2BaseUri(){
ServiceInstance serviceInstance = loadBalancerClient.choose("EUREKACLIENT2");
return serviceInstance.getUri().toString();
}
}

View File

@@ -0,0 +1,14 @@
spring:
application:
name: 'eurekaclient1'
server:
port: 8081
eureka:
instance:
hostname: ${vcap.application.uris[0]:localhost}
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 20
client:
service-url:
defaultZone: http://localhost:9090/eureka

View File

@@ -0,0 +1,13 @@
package com.javadevjournal.eurekaclient1;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Eurekaclient1ApplicationTests {
@Test
void contextLoads() {
}
}