Sunday, July 30, 2017

Working with Ribbon with out Eureka

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.example(/groupId)
(artifactId)RibbonWithoutEureka(/artifactId)
(version)0.0.1-SNAPSHOT(/version)
(packaging)jar(/packaging)

(name)RibbonWithoutEureka(/name)
(description)Demo project for Spring Boot(/description)

(parent)
(groupId)org.springframework.boot(/groupId)
(artifactId)spring-boot-starter-parent(/artifactId)
(version)1.5.6.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.boot(/groupId)
(artifactId)spring-boot-starter-web(/artifactId)
(/dependency)

(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-starter-ribbon(/artifactId)
(/dependency)

(/dependencies)

(dependencyManagement)
(dependencies)
(dependency)
(groupId)org.springframework.cloud(/groupId)
(artifactId)spring-cloud-dependencies(/artifactId)
(version)Camden.SR6(/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)


application.properties:
================
testApp.ribbon.listOfServers=http://localhost:8081,http://localhost:8082
testApp.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.WeightedResponseTimeRule
testApp.ribbon.serverRefreshInterval=15000
testApp.ribbon.eureka.enabled=false
server.port=8083


SpringBootHelloWorldApplication.java
=============================

package com.javainuse;

import java.io.IOException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClientException;

import com.javainuse.controllers.ConsumerControllerClient;
import com.spring.TestAppConfiguration;

@SpringBootApplication
@RibbonClient(name="testApp",configuration=TestAppConfiguration.class)
public class SpringBootHelloWorldApplication {

public static void main(String[] args) throws RestClientException, IOException {
ApplicationContext ctx = SpringApplication.run(
SpringBootHelloWorldApplication.class, args);
ConsumerControllerClient consumerControllerClient=ctx.getBean(ConsumerControllerClient.class);
System.out.println(consumerControllerClient);
consumerControllerClient.getEmployee();
}
@Bean
public  ConsumerControllerClient  consumerControllerClient()
{
return  new ConsumerControllerClient();
}
}


ConsumerControllerClient.java
======================

package com.javainuse.controllers;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerControllerClient {
@Autowired
private LoadBalancerClient loadBalancer;
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public void getEmployee() throws RestClientException, IOException {
ServiceInstance serviceInstance=loadBalancer.choose("testApp");
System.out.println(serviceInstance.getUri());
String baseUrl=serviceInstance.getUri().toString();
baseUrl=baseUrl+"/employee";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity response=null;
try{
response=restTemplate.exchange(baseUrl,
HttpMethod.GET, getHeaders(),String.class);
}catch (Exception ex)
{
System.out.println(ex);
}
System.out.println(response.getBody());
}

private static HttpEntity getHeaders() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(headers);
}
}


TestAppConfiguration.java
===================
package com.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.WeightedResponseTimeRule;

@Configuration
public class TestAppConfiguration {
@Bean
IRule rule(){
return new WeightedResponseTimeRule();
}
}

No comments:

Post a Comment

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

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