-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
服务消费者使用 @LoadBalanced RestTemplate 实现服务调用
- Loading branch information
Showing
3 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
nacos-discovery-consumer-sample/src/main/java/com/example/demo/RestTemplateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalanced; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* 服务调用 | ||
* | ||
* @author yclimb | ||
* @date 2020/9/30 | ||
*/ | ||
@RestController | ||
public class RestTemplateController { | ||
|
||
@LoadBalanced | ||
@Autowired | ||
public RestTemplate restTemplate; | ||
|
||
@LoadBalanced | ||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
|
||
@GetMapping("/call/echo/{message}") | ||
public String callEcho(@PathVariable String message) { | ||
// 访问应用 nacos-discovery-provider-sample 的 REST "/echo/{message}" | ||
return restTemplate.getForObject("http://nacos-discovery-provider-sample/echo/" + message, String.class); | ||
} | ||
} |