Saturday, September 9, 2017

FeignClient - Load Balancer with Eureka

feign-eureka-hello-server
=========================

HelloServerApplication.java
===========================
package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Spencer Gibb
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServerApplication {
@Autowired
DiscoveryClient client;

@RequestMapping("/")
public String hello() {
ServiceInstance localInstance = client.getLocalServiceInstance();
return "Hello World: "+ localInstance.getServiceId()+":"+localInstance.getHost()+":"+localInstance.getPort();
}

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


application.yml
===============
spring:
  application:
    name: HelloServer

server:
  port: 7111

eureka:
  password: password
  client:
    serviceUrl:
      defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}


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)org.test(/groupId)
(artifactId)feign-eureka-hello-server(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)

(name)feign-eureka-hello-server(/name)
(description)Demo project for Spring Cloud(/description)

(parent)
(groupId)org.test(/groupId)
(artifactId)feign-eureka(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(relativePath)..(/relativePath) (!-- lookup parent from repository --)
(/parent)

(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(java.version)1.8(/java.version)
(/properties)

(dependencies)
(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)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-feign(/artifactId)
(/dependency)
(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)

(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(plugin)
(groupId)pl.project13.maven(/groupId)
(artifactId)git-commit-id-plugin(/artifactId)
(/plugin)
(plugin)
(!--skip deploy (this is just a test module) --)
(artifactId)maven-deploy-plugin(/artifactId)
(configuration)
(skip)true(/skip)
(/configuration)
(/plugin)
(/plugins)
(/build)

(/project)


-------------------------------------------------------------------------------------------------------------------------------------------------------
feign-eureka-hello-client
=========================
HelloClientApplication.java
===========================
package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * @author Spencer Gibb
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
@Autowired
HelloClient client;

@RequestMapping("/")
public String hello() {
return client.hello();
}

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

@FeignClient("HelloServer")
interface HelloClient {
@RequestMapping(value = "/", method = GET)
String hello();
}
}


application.yml
===============
spring:
  application:
    name: HelloClient

server:
  port: 7211

eureka:
  password: password
  client:
    serviceUrl:
      defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}


endpoints:
  restart:
    enabled: true
  shutdown:
    enabled: true
  health:
    sensitive: false


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)org.test(/groupId)
(artifactId)feign-eureka-hello-client(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)

(name)feign-eureka-hello-client(/name)
(description)Demo project for Spring Cloud(/description)

(parent)
(groupId)org.test(/groupId)
(artifactId)feign-eureka(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(relativePath)..(/relativePath) (!-- lookup parent from repository --)
(/parent)

(properties)
(project.build.sourceEncoding)UTF-8(/project.build.sourceEncoding)
(java.version)1.8(/java.version)
(/properties)

(dependencies)
(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)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-feign(/artifactId)
(/dependency)
(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)

(build)
(plugins)
(plugin)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-maven-plugin(/artifactId)
(/plugin)
(plugin)
(groupId)pl.project13.maven(/groupId)
(artifactId)git-commit-id-plugin(/artifactId)
(/plugin)
(plugin)
(!--skip deploy (this is just a test module) --)
(artifactId)maven-deploy-plugin(/artifactId)
(configuration)
(skip)true(/skip)
(/configuration)
(/plugin)
(/plugins)
(/build)
(/project)


Testing:


When we call http://localhost:7211 one of the following messages will be shown: Hello World: HelloServer:192.168.0.14:7112

Hello World: HelloServer:192.168.0.14:7111

No comments:

Post a Comment

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

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