Skip to content

Commit 11f246e

Browse files
committed
feat(feign负载均衡):feign集成ribbon进行负载均衡
1 parent 17c7636 commit 11f246e

File tree

10 files changed

+204
-4
lines changed

10 files changed

+204
-4
lines changed

api-gateway/src/main/resources/application.properties

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
55
# 禁止服务自动添加
66
zuul.ignoredServices='*'
77

8-
zuul.routes.api-a.path=/customer/**
9-
zuul.routes.api-a.serviceId=eureka-customer
8+
#zuul.routes.api-a.path=/customer/**
9+
#zuul.routes.api-a.serviceId=eureka-customer
1010

11-
zuul.routes.api-b.path=/order/**
12-
zuul.routes.api-b.serviceId=eureka-order
11+
#zuul.routes.api-b.path=/order/**
12+
#zuul.routes.api-b.serviceId=eureka-order
13+
14+
# 由网关分发到feign,再经feign负载均衡
15+
zuul.routes.api-c.path=/feign/**
16+
zuul.routes.api-c.serviceId=feign-server
1317

1418
#ribbon.ConnectTimeout=6000
1519
#ribbon.ReadTimeout=6000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
██████╗ ██████╗ ██████╗ ███████╗██████╗ ██████╗ ██╗ █████╗ ███╗ ██╗
2+
██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔══██╗██╔═══██╗██║██╔══██╗████╗ ██║
3+
██║ ██║ ██║██║ ██║█████╗ ██████╔╝██║ ██║██║███████║██╔██╗ ██║
4+
██║ ██║ ██║██║ ██║██╔══╝ ██╔══██╗██║▄▄ ██║██║██╔══██║██║╚██╗██║
5+
╚██████╗╚██████╔╝██████╔╝███████╗██║ ██║╚██████╔╝██║██║ ██║██║ ╚████║
6+
╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚══▀▀═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝
7+
░ ░▒ ▒ ░░ ▒░▒░▒░ ▒▒▓ ▒ ░░ ▒░ ░░ ▒▓ ░▒▓░░░ ▒▒░ ▒ ░▓ ▒▒ ▓▒█░░ ▒░ ▒ ▒
8+
░ ▒ ░ ▒ ▒░ ░ ▒ ▒ ░ ░ ░ ░▒ ░ ▒░ ░ ▒░ ░ ▒ ░ ▒ ▒▒ ░░ ░░ ░ ▒░
9+
░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░ ░ ░ ▒ ░ ░ ▒ ░ ░ ░
10+
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
11+

feign-server/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.coderqian</groupId>
7+
<artifactId>fegin-server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>feign-server</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.9.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+
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<!-- spring boot 2.0 -->
30+
<!--<dependency>-->
31+
<!--<groupId>org.springframework.cloud</groupId>-->
32+
<!--<artifactId>spring-cloud-starter-openfeign</artifactId>-->
33+
<!--</dependency>-->
34+
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-feign</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-eureka</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<dependencyManagement>
53+
<dependencies>
54+
<dependency>
55+
<groupId>org.springframework.cloud</groupId>
56+
<artifactId>spring-cloud-dependencies</artifactId>
57+
<version>${spring-cloud.version}</version>
58+
<type>pom</type>
59+
<scope>import</scope>
60+
</dependency>
61+
</dependencies>
62+
</dependencyManagement>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
74+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.coderqian.feginserver;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
7+
8+
9+
@SpringBootApplication
10+
@EnableEurekaClient
11+
@EnableFeignClients
12+
public class FeignServerApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(FeignServerApplication.class, args);
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coderqian.feginserver.controller;
2+
3+
import com.coderqian.feginserver.service.TestCustomerService;
4+
import com.coderqian.feginserver.service.TestOrderService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* @author qianliqing
13+
* @date 2018-10-15 上午9:02
14+
15+
*/
16+
17+
@RestController
18+
public class TestController {
19+
20+
@Autowired
21+
private TestCustomerService testCustomerService;
22+
23+
@Autowired
24+
private TestOrderService testOrderService;
25+
26+
@RequestMapping(value = "/customer", method = RequestMethod.GET)
27+
public String getCustomer(@RequestParam(value = "text") String text) {
28+
return testCustomerService.testCustomer(text);
29+
}
30+
31+
@RequestMapping(value = "/order", method = RequestMethod.GET)
32+
public String getOrder(@RequestParam(value = "text") String text) {
33+
return testOrderService.testOrder(text);
34+
}
35+
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderqian.feginserver.service;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
8+
/**
9+
* @author qianliqing
10+
* @date 2018-10-15 上午9:03
11+
12+
*/
13+
14+
@FeignClient(value = "eureka-customer")
15+
public interface TestCustomerService {
16+
17+
@RequestMapping(value = "/test/result", method = RequestMethod.GET)
18+
String testCustomer(@RequestParam(value = "text") String text);
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderqian.feginserver.service;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
8+
/**
9+
* @author qianliqing
10+
* @date 2018-10-15 上午10:28
11+
12+
*/
13+
14+
@FeignClient(value = "eureka-order")
15+
public interface TestOrderService {
16+
17+
@RequestMapping(value = "/test/result", method = RequestMethod.GET)
18+
String testOrder(@RequestParam(value = "text") String text);
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.application.name=feign-server
2+
server.port=8765
3+
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderqian.feginserver;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class FeginServerApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<module>eureka-customer</module>
1616
<module>api-gateway</module>
1717
<module>config-server</module>
18+
<module>feign-server</module>
1819
</modules>
1920
</project>

0 commit comments

Comments
 (0)