Skip to content

Commit 8212209

Browse files
committedAug 21, 2018
Spring Cloud Ribbon客户端负载均衡
1 parent eeafc67 commit 8212209

File tree

16 files changed

+552
-0
lines changed

16 files changed

+552
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>Eureka-Client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Eureka-Client</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.13.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-dependencies</artifactId>
32+
<version>Edgware.SR3</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
<dependencies>
39+
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-eureka</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter</artifactId>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
8+
@EnableDiscoveryClient
9+
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
10+
public class DemoApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(DemoApplication.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.demo.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
import org.springframework.cloud.client.discovery.DiscoveryClient;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
public class TestController {
13+
14+
private Logger log = LoggerFactory.getLogger(this.getClass());
15+
16+
@Autowired
17+
private DiscoveryClient client;
18+
19+
@GetMapping("/info")
20+
public String info() {
21+
@SuppressWarnings("deprecation")
22+
ServiceInstance instance = client.getLocalServiceInstance();
23+
String info = "host:" + instance.getHost() + ",service_id:" + instance.getServiceId();
24+
log.info(info);
25+
return info;
26+
}
27+
28+
@GetMapping("/hello")
29+
public String hello() {
30+
return "hello world";
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example.demo.controller;
2+
3+
import com.example.demo.domain.User;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("user")
13+
public class UserController {
14+
15+
private Logger log = LoggerFactory.getLogger(this.getClass());
16+
17+
@GetMapping("/{id:\\d+}")
18+
public User get(@PathVariable Long id) {
19+
log.info("获取用户id为 " + id + "的信息");
20+
return new User(id, "mrbird", "123456");
21+
}
22+
23+
@GetMapping
24+
public List<User> get() {
25+
List<User> list = new ArrayList<>();
26+
list.add(new User(1L, "mrbird", "123456"));
27+
list.add(new User(2L, "scott", "123456"));
28+
log.info("获取用户信息 " + list);
29+
return list;
30+
}
31+
32+
@PostMapping
33+
public void add(@RequestBody User user) {
34+
log.info("新增用户成功 " + user);
35+
}
36+
37+
@PutMapping
38+
public void update(@RequestBody User user) {
39+
log.info("更新用户成功 " + user);
40+
}
41+
42+
@DeleteMapping("/{id:\\d+}")
43+
public void delete(@PathVariable Long id) {
44+
log.info("删除用户成功 " + id);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.example.demo.domain;
2+
3+
import java.io.Serializable;
4+
5+
public class User implements Serializable {
6+
7+
private static final long serialVersionUID = 1339434510787399891L;
8+
private Long id;
9+
10+
private String username;
11+
12+
private String password;
13+
14+
public User() {
15+
}
16+
17+
public User(Long id, String username, String password) {
18+
this.id = id;
19+
this.username = username;
20+
this.password = password;
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getUsername() {
32+
return username;
33+
}
34+
35+
public void setUsername(String username) {
36+
this.username = username;
37+
}
38+
39+
public String getPassword() {
40+
return password;
41+
}
42+
43+
public void setPassword(String password) {
44+
this.password = password;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "User{" +
50+
"id=" + id +
51+
", username='" + username + '\'' +
52+
", password='" + password + '\'' +
53+
'}';
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
server:
2+
port: 8082
3+
4+
spring:
5+
application:
6+
name: Server-Provider
7+
8+
eureka:
9+
client:
10+
register-with-eureka: true
11+
fetch-registry: true
12+
serviceUrl:
13+
defaultZone: http://mrbird:123456@peer1:8080/eureka/,http://mrbird:123456@peer2:8081/eureka/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>Eureka-Service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Eureka-Service</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.13.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-dependencies</artifactId>
32+
<version>Edgware.SR3</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-eureka-server</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-security</artifactId>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<repositories>
52+
<repository>
53+
<id>nexus-aliyun</id>
54+
<name>Nexus aliyun</name>
55+
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
56+
</repository>
57+
</repositories>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
8+
@EnableEurekaServer
9+
@SpringBootApplication
10+
public class DemoApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(DemoApplication.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
application:
6+
name: Eureka-Server
7+
8+
eureka:
9+
instance:
10+
hostname: peer1
11+
client:
12+
serviceUrl:
13+
defaultZone: http://mrbird:123456@peer2:8081/eureka/
14+
server:
15+
enable-self-preservation: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 8081
3+
4+
spring:
5+
application:
6+
name: Eureka-Server
7+
8+
eureka:
9+
instance:
10+
hostname: peer2
11+
client:
12+
serviceUrl:
13+
defaultZone: http://mrbird:123456@peer1:8080/eureka/
14+
server:
15+
enable-self-preservation: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
security:
2+
basic:
3+
enabled: true
4+
user:
5+
name: mrbird
6+
password: 123456
7+
spring:
8+
profiles:
9+
active: peer1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>Ribbon-Consumer</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Ribbon-Consumer</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.13.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-dependencies</artifactId>
32+
<version>Edgware.SR3</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.springframework.cloud</groupId>
41+
<artifactId>spring-cloud-starter-eureka</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.cloud</groupId>
49+
<artifactId>spring-cloud-starter-ribbon</artifactId>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
63+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@EnableDiscoveryClient
11+
@SpringBootApplication
12+
public class DemoApplication {
13+
14+
@Bean
15+
@LoadBalanced
16+
RestTemplate restTemplate() {
17+
return new RestTemplate();
18+
}
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(DemoApplication.class, args);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.example.demo.controller;
2+
3+
import com.example.demo.domain.User;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.client.RestTemplate;
10+
import org.springframework.web.util.UriComponentsBuilder;
11+
12+
import java.net.URI;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
@RestController
18+
public class TestController {
19+
20+
@Autowired
21+
private RestTemplate restTemplate;
22+
23+
@GetMapping("user/{id:\\d+}")
24+
public User getUser(@PathVariable Long id) {
25+
Map<String, Object> params = new HashMap<>();
26+
params.put("id", id);
27+
URI uri = UriComponentsBuilder.fromUriString("http://Server-Provider/user/{id}")
28+
.build().expand(params).encode().toUri();
29+
return this.restTemplate.getForEntity(uri, User.class).getBody();
30+
}
31+
32+
@GetMapping("user")
33+
public List<User> getUsers() {
34+
return this.restTemplate.getForObject("http://Server-Provider/user", List.class);
35+
}
36+
37+
@GetMapping("user/add")
38+
public String addUser() {
39+
User user = new User(1L, "mrbird", "123456");
40+
HttpStatus status = this.restTemplate.postForEntity("http://Server-Provider/user", user, null).getStatusCode();
41+
if (status.is2xxSuccessful()) {
42+
return "新增用户成功";
43+
} else {
44+
return "新增用户失败";
45+
}
46+
}
47+
48+
@GetMapping("user/update")
49+
public void updateUser() {
50+
User user = new User(1L, "mrbird", "123456");
51+
this.restTemplate.put("http://Server-Provider/user", user);
52+
}
53+
54+
@GetMapping("user/delete/{id:\\d+}")
55+
public void deleteUser(@PathVariable Long id) {
56+
this.restTemplate.delete("http://Server-Provider/user/{1}", id);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example.demo.domain;
2+
3+
import java.io.Serializable;
4+
5+
public class User implements Serializable {
6+
7+
private static final long serialVersionUID = 1339434510787399891L;
8+
private Long id;
9+
private String username;
10+
private String password;
11+
12+
public User() {
13+
}
14+
15+
public User(Long id, String username, String password) {
16+
this.id = id;
17+
this.username = username;
18+
this.password = password;
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getUsername() {
30+
return username;
31+
}
32+
33+
public void setUsername(String username) {
34+
this.username = username;
35+
}
36+
37+
public String getPassword() {
38+
return password;
39+
}
40+
41+
public void setPassword(String password) {
42+
this.password = password;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "User{" +
48+
"id=" + id +
49+
", username='" + username + '\'' +
50+
", password='" + password + '\'' +
51+
'}';
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server:
2+
port: 9000
3+
4+
spring:
5+
application:
6+
name: Server-Consumer
7+
8+
eureka:
9+
client:
10+
serviceUrl:
11+
defaultZone: http://mrbird:123456@peer1:8080/eureka/,http://mrbird:123456@peer2:8081/eureka/

0 commit comments

Comments
 (0)
Please sign in to comment.