Skip to content

Commit cc05e21

Browse files
committed
Spring Cloud Alibaba基础教程:Nacos下使用Feign实现负载均衡消费
1 parent e386135 commit cc05e21

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

4-Finchley/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
**Spring Cloud Aliabab专题**
3030

3131
- [Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现](http://blog.didispace.com/spring-cloud-alibaba-1)
32+
- [Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)](http://blog.didispace.com/spring-cloud-alibaba-2)
3233

3334
**Spring Cloud Stream专题补充**
3435

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.didispace</groupId>
7+
<artifactId>alibaba-nacos-discovery-client-feign</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.5.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-openfeign</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<version>1.18.2</version>
42+
<optional>true</optional>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<dependencyManagement>
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.springframework.cloud</groupId>
55+
<artifactId>spring-cloud-dependencies</artifactId>
56+
<version>Finchley.SR1</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
63+
<version>0.2.1.RELEASE</version>
64+
<type>pom</type>
65+
<scope>import</scope>
66+
</dependency>
67+
</dependencies>
68+
</dependencyManagement>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-maven-plugin</artifactId>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.didispace.alibaba.nacos.discovery.client;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
8+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
9+
import org.springframework.cloud.openfeign.EnableFeignClients;
10+
import org.springframework.cloud.openfeign.FeignClient;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RequestParam;
14+
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.client.RestTemplate;
16+
17+
18+
@EnableDiscoveryClient
19+
@SpringBootApplication
20+
@EnableFeignClients
21+
public class TestApplication {
22+
23+
public static void main(String[] args) {
24+
SpringApplication.run(TestApplication.class, args);
25+
}
26+
27+
@Slf4j
28+
@RestController
29+
static class TestController {
30+
31+
@Autowired
32+
Client client;
33+
34+
@GetMapping("/test")
35+
public String test() {
36+
String result = client.hello("didi");
37+
return "Return : " + result;
38+
}
39+
}
40+
41+
42+
@FeignClient("alibaba-nacos-discovery-server")
43+
interface Client {
44+
45+
@GetMapping("/hello")
46+
String hello(@RequestParam(name = "name") String name);
47+
48+
}
49+
50+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.application.name=alibaba-nacos-discovery-client-feign
2+
server.port=9000
3+
4+
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

4-Finchley/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
<!-- spring cloud alibaba专题-->
1313
<module>alibaba-nacos-discovery-server</module> <!-- 使用Nacos实现服务注册与发现:服务提供方-->
1414
<module>alibaba-nacos-discovery-client-common</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(spring cloud common接口实现负载均衡)-->
15+
<module>alibaba-nacos-config-client</module> <!-- 使用Nacos作为配置中心 -->
16+
1517
<module>alibaba-nacos-discovery-client-resttemplate</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(RestTemplate实现)-->
1618
<module>alibaba-nacos-discovery-client-webclient</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(WebClient实现)-->
17-
<!--<module>alibaba-nacos-discovery-client-feign</module>-->
19+
<module>alibaba-nacos-discovery-client-feign</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(Feign实现)-->
1820

1921
<!--spring cloud stream专题-->
2022
<module>stream-consumer-self</module> <!-- 消费自己的消息 -->

0 commit comments

Comments
 (0)