Ribbon的架构原理-SpringCloudRibbon-自定义RestTemplate-《Java笔记》

admin 2025-10-19 05:48:52 编程 来源:ZONE.CI 全球网 0 阅读模式

    Java SpringCloud BibbonRestTemplate.java

    1. package com.fcant.userservice.controller;
    2. import com.fcant.userservice.bean.Country;
    3. import com.fcant.userservice.bean.User;
    4. import org.slf4j.Logger;
    5. import org.slf4j.LoggerFactory;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.web.bind.annotation.GetMapping;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.web.bind.annotation.RestController;
    10. import org.springframework.web.client.RestClientException;
    11. import org.springframework.web.client.RestTemplate;
    12. import java.util.ArrayList;
    13. import java.util.Collections;
    14. import java.util.List;
    15. /**
    16. * RestTemplateController
    17. * <p>
    18. * encoding:UTF-8
    19. *
    20. * @author Fcant 10:06 2019/12/9
    21. */
    22. @RestController
    23. @RequestMapping("/user")
    24. public class RestTemplateController {
    25. private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateController.class);
    26. private static final List<User> USER_LIST;
    27. static {
    28. List<User> userList = new ArrayList<>();
    29. userList.add(new User(1L, "User 01", "[email protected]", "男", 1L, ""));
    30. userList.add(new User(2L, "User 02", "[email protected]", "女", 2L, ""));
    31. USER_LIST = Collections.unmodifiableList(userList);
    32. }
    33. private RestTemplate restTemplate;
    34. @Autowired
    35. public RestTemplateController(RestTemplate restTemplate) {
    36. this.restTemplate = restTemplate;
    37. }
    38. @GetMapping("/rest-template")
    39. public List<User> queryUser(){
    40. List<User> userList = new ArrayList<>(USER_LIST);
    41. userList.forEach(user -> {
    42. Country country = null;
    43. try {
    44. country = restTemplate.getForObject("http://area-service/country/{countryId}",
    45. Country.class,
    46. user.getCountryId());
    47. user.setCountryName(country.getCountryName());
    48. } catch (RestClientException e) {
    49. e.printStackTrace();
    50. }
    51. });
    52. return userList;
    53. }
    54. }
    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  8