Skip to content

Commit b9c3710

Browse files
authored
Added code for new module
1 parent 979b678 commit b9c3710

6 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-cloud-ribbon</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>spring-cloud-ribbon-client</name>
9+
<description>Introduction to Spring Cloud Rest Client with Netflix Ribbon</description>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.1.RELEASE</version>
15+
<relativePath /> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.cloud</groupId>
26+
<artifactId>spring-cloud-starter-ribbon</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-dependencies</artifactId>
45+
<version>Camden.SR1</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
<repositories>
62+
<repository>
63+
<id>spring-snapshots</id>
64+
<name>Spring Snapshots</name>
65+
<url>https://repo.spring.io/snapshot</url>
66+
<snapshots>
67+
<enabled>true</enabled>
68+
</snapshots>
69+
</repository>
70+
<repository>
71+
<id>spring-milestones</id>
72+
<name>Spring Milestones</name>
73+
<url>https://repo.spring.io/milestone</url>
74+
<snapshots>
75+
<enabled>false</enabled>
76+
</snapshots>
77+
</repository>
78+
</repositories>
79+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.spring.cloud.ribbon.client;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
6+
import com.netflix.client.config.IClientConfig;
7+
import com.netflix.loadbalancer.IPing;
8+
import com.netflix.loadbalancer.IRule;
9+
import com.netflix.loadbalancer.PingUrl;
10+
import com.netflix.loadbalancer.WeightedResponseTimeRule;
11+
import com.netflix.loadbalancer.AvailabilityFilteringRule;
12+
13+
public class RibbonConfiguration {
14+
15+
@Autowired
16+
IClientConfig ribbonClientConfig;
17+
18+
@Bean
19+
public IPing ribbonPing(IClientConfig config) {
20+
return new PingUrl();
21+
}
22+
23+
@Bean
24+
public IRule ribbonRule(IClientConfig config) {
25+
return new WeightedResponseTimeRule();
26+
}
27+
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring.cloud.ribbon.client;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7+
import org.springframework.cloud.netflix.ribbon.RibbonClient;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
@SpringBootApplication
14+
@RestController
15+
@RibbonClient(name = "ping-a-server", configuration = RibbonConfiguration.class)
16+
public class ServerLocationApp {
17+
18+
@LoadBalanced
19+
@Bean
20+
RestTemplate getRestTemplate() {
21+
return new RestTemplate();
22+
}
23+
24+
@Autowired
25+
RestTemplate restTemplate;
26+
27+
@RequestMapping("/server-location")
28+
public String serverLocation() {
29+
String servLoc = this.restTemplate.getForObject("http://ping-server/locaus", String.class);
30+
return servLoc;
31+
}
32+
33+
public static void main(String[] args) {
34+
SpringApplication.run(ServerLocationApp.class, args);
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
application:
3+
name: spring-cloud-ribbon
4+
5+
server:
6+
port: 8888
7+
8+
ping-server:
9+
ribbon:
10+
eureka:
11+
enabled: false
12+
listOfServers: localhost:9092,localhost:9999
13+
ServerListRefreshInterval: 15000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.spring.cloud.ribbon.client;
2+
3+
import static org.assertj.core.api.BDDAssertions.then;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.context.embedded.LocalServerPort;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.web.client.TestRestTemplate;
15+
import org.springframework.context.ConfigurableApplicationContext;
16+
import org.springframework.http.HttpStatus;
17+
import org.springframework.http.ResponseEntity;
18+
import org.springframework.test.context.junit4.SpringRunner;
19+
20+
@SuppressWarnings("unused")
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(classes = ServerLocationApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
23+
public class ServerLocationAppTests {
24+
ConfigurableApplicationContext application2;
25+
ConfigurableApplicationContext application3;
26+
27+
@Before
28+
public void startApps() {
29+
this.application2 = startApp(9092);
30+
this.application3 = startApp(9999);
31+
}
32+
33+
@After
34+
public void closeApps() {
35+
this.application2.close();
36+
this.application3.close();
37+
}
38+
39+
@LocalServerPort
40+
private int port;
41+
42+
@Autowired
43+
private TestRestTemplate testRestTemplate;
44+
45+
@Test
46+
public void loadBalancingServersTest() throws InterruptedException {
47+
ResponseEntity<String> response = this.testRestTemplate.getForEntity("http://localhost:" + this.port + "/server-location", String.class);
48+
assertEquals(response.getBody(), "Australia");
49+
}
50+
51+
private ConfigurableApplicationContext startApp(int port) {
52+
return SpringApplication.run(TestConfig.class, "--server.port=" + port, "--spring.jmx.enabled=false");
53+
}
54+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring.cloud.ribbon.client;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@Configuration
9+
@EnableAutoConfiguration
10+
@RestController
11+
public class TestConfig {
12+
13+
@RequestMapping(value = "/locaus")
14+
public String locationAUSDetails() {
15+
return "Australia";
16+
}
17+
}

0 commit comments

Comments
 (0)