JAVA-8982: Fix circular dependency issue in spring-cloud-ribbon-client

This commit is contained in:
Krzysiek 2021-12-20 12:20:50 +01:00
parent 3a2adcdb8e
commit 71b9f28eaf
4 changed files with 18 additions and 16 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.ribbon.client;
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;
@Configuration
public class RestTemplateConfiguration {
@LoadBalanced
@Bean
RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

View File

@ -3,9 +3,7 @@ package com.baeldung.spring.cloud.ribbon.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@ -15,19 +13,12 @@ import org.springframework.web.client.RestTemplate;
@RibbonClient(name = "ping-a-server", configuration = RibbonConfiguration.class)
public class ServerLocationApp {
@LoadBalanced
@Bean
RestTemplate getRestTemplate() {
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/server-location")
public String serverLocation() {
String servLoc = this.restTemplate.getForObject("http://ping-server/locaus", String.class);
return servLoc;
return this.restTemplate.getForObject("http://ping-server/locaus", String.class);
}
public static void main(String[] args) {

View File

@ -2,9 +2,6 @@ spring:
application:
name: spring-cloud-ribbon
main:
allow-circular-references: true
server:
port: 8888

View File

@ -1,6 +1,5 @@
package com.baeldung.spring.cloud.ribbon.client;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.Assert.assertEquals;
import org.junit.After;
@ -13,7 +12,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@ -43,7 +41,7 @@ public class ServerLocationAppIntegrationTest {
private TestRestTemplate testRestTemplate;
@Test
public void loadBalancingServersTest() throws InterruptedException {
public void loadBalancingServersTest() {
ResponseEntity<String> response = this.testRestTemplate.getForEntity("http://localhost:" + this.port + "/server-location", String.class);
assertEquals(response.getBody(), "Australia");
}