【第6章】Spring Cloud之Nacos服务调用(含RestTemplate 和 OpenFeign)

[复制链接]
发表于 2026-2-22 05:32:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

前言

本章我们通过RestTemplate 和 OpenFeign来完成微服务中斲丧者对提供者服务的远程调用。

一、准备

   这里我们在provider-service增长EchoService作为服务端,对外提供服务
  1. service

  1. package org.example.nacos.provider.service;
  2. /**
  3. * Create by zjg on 2024/7/16
  4. */
  5. public interface EchoService {
  6.     String echo(String str);
  7. }
复制代码
  1. package org.example.nacos.provider.service.impl;
  2. import org.example.nacos.provider.service.EchoService;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * Create by zjg on 2024/7/16
  6. */
  7. @Service
  8. public class EchoServiceImpl implements EchoService {
  9.     @Override
  10.     public String echo(String str) {
  11.         return "Hello "+str;
  12.     }
  13. }
复制代码
2. controller

  1. package org.example.nacos.provider.controller;
  2. import org.example.nacos.provider.service.EchoService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * Create by zjg on 2024/7/16
  9. */
  10. @RestController
  11. public class EchoController {
  12.     @Autowired
  13.     EchoService echoService;
  14.     @RequestMapping("/echo/{str}")
  15.     public String echo(@PathVariable("str")String str) {
  16.         return echoService.echo(str);
  17.     }
  18. }
复制代码
二、RestTemplate

1. 打开开关

  1. spring:
  2.   cloud:
  3.     loadbalancer:
  4.       nacos:
  5.         enabled: true
复制代码
2. 引入库

  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  4. </dependency>
复制代码
3. 设置负载均衡

  1. package org.example.nacos.consumer.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. /**
  7. * Create by zjg on 2024/7/16
  8. */
  9. @Configuration
  10. public class ConsumerConfig {
  11.     @Bean
  12.     @LoadBalanced
  13.     public RestTemplate restTemplate() {
  14.         return new RestTemplate();
  15.     }
  16. }
复制代码
4. 测试类

  1. package org.example.nacos.consumer.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * Create by zjg on 2024/7/16
  9. */
  10. @RestController
  11. public class TestController {
  12.     @Autowired
  13.     private RestTemplate restTemplate;
  14.     @GetMapping(value = "/echo-rest/{str}")
  15.     public String rest(@PathVariable("str") String str) {
  16.         return restTemplate.getForObject("http://provider-service/echo/" + str, String.class);
  17.     }
  18. }
复制代码
localhost:9003/echo-rest/RestTemplate
   Hello RestTemplate
  三、FeignClient

1. 引入库

  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
复制代码
2. 打开开关

  1. package org.example.nacos.consumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @EnableFeignClients
  7. @EnableDiscoveryClient
  8. @SpringBootApplication
  9. public class NacosDiscoveryConsumerApplication {
  10.     public static void main(String[] args) {
  11.         SpringApplication.run(NacosDiscoveryConsumerApplication.class, args);
  12.     }
  13. }
复制代码
3. feign客户端

  1. package org.example.nacos.consumer.feign;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. /**
  6. * Create by zjg on 2024/7/16
  7. */
  8. @FeignClient(name = "provider-service")
  9. public interface EchoClient {
  10.     @GetMapping(value = "/echo/{str}")
  11.     String echo(@PathVariable("str") String str);
  12. }
复制代码
4. 测试类

  1. package org.example.nacos.consumer.controller;
  2. import org.example.nacos.consumer.feign.EchoClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * Create by zjg on 2024/7/16
  9. */
  10. @RestController
  11. public class TestController {
  12.     @Autowired
  13.     private EchoClient echoClient;
  14.     @GetMapping(value = "/echo-feign/{str}")
  15.     public String feign(@PathVariable("str") String str) {
  16.         return echoClient.echo(str);
  17.     }
  18. }
复制代码
localhost:9003/echo-rest/OpenFeign
   Hello OpenFeign
  
总结

回到顶部

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表