马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
前言
本章我们通过RestTemplate 和 OpenFeign来完成微服务中斲丧者对提供者服务的远程调用。
一、准备
这里我们在provider-service增长EchoService作为服务端,对外提供服务
1. service
- package org.example.nacos.provider.service;
- /**
- * Create by zjg on 2024/7/16
- */
- public interface EchoService {
- String echo(String str);
- }
复制代码- package org.example.nacos.provider.service.impl;
- import org.example.nacos.provider.service.EchoService;
- import org.springframework.stereotype.Service;
- /**
- * Create by zjg on 2024/7/16
- */
- @Service
- public class EchoServiceImpl implements EchoService {
- @Override
- public String echo(String str) {
- return "Hello "+str;
- }
- }
复制代码 2. controller
- package org.example.nacos.provider.controller;
- import org.example.nacos.provider.service.EchoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * Create by zjg on 2024/7/16
- */
- @RestController
- public class EchoController {
- @Autowired
- EchoService echoService;
- @RequestMapping("/echo/{str}")
- public String echo(@PathVariable("str")String str) {
- return echoService.echo(str);
- }
- }
复制代码 二、RestTemplate
1. 打开开关
- spring:
- cloud:
- loadbalancer:
- nacos:
- enabled: true
复制代码 2. 引入库
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-loadbalancer</artifactId>
- </dependency>
复制代码 3. 设置负载均衡
- package org.example.nacos.consumer.config;
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
- /**
- * Create by zjg on 2024/7/16
- */
- @Configuration
- public class ConsumerConfig {
- @Bean
- @LoadBalanced
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
复制代码 4. 测试类
- package org.example.nacos.consumer.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- 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;
- /**
- * Create by zjg on 2024/7/16
- */
- @RestController
- public class TestController {
- @Autowired
- private RestTemplate restTemplate;
- @GetMapping(value = "/echo-rest/{str}")
- public String rest(@PathVariable("str") String str) {
- return restTemplate.getForObject("http://provider-service/echo/" + str, String.class);
- }
- }
复制代码 localhost:9003/echo-rest/RestTemplate
Hello RestTemplate
三、FeignClient
1. 引入库
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
复制代码 2. 打开开关
- package org.example.nacos.consumer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.openfeign.EnableFeignClients;
- @EnableFeignClients
- @EnableDiscoveryClient
- @SpringBootApplication
- public class NacosDiscoveryConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(NacosDiscoveryConsumerApplication.class, args);
- }
- }
复制代码 3. feign客户端
- package org.example.nacos.consumer.feign;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- /**
- * Create by zjg on 2024/7/16
- */
- @FeignClient(name = "provider-service")
- public interface EchoClient {
- @GetMapping(value = "/echo/{str}")
- String echo(@PathVariable("str") String str);
- }
复制代码 4. 测试类
- package org.example.nacos.consumer.controller;
- import org.example.nacos.consumer.feign.EchoClient;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * Create by zjg on 2024/7/16
- */
- @RestController
- public class TestController {
- @Autowired
- private EchoClient echoClient;
- @GetMapping(value = "/echo-feign/{str}")
- public String feign(@PathVariable("str") String str) {
- return echoClient.echo(str);
- }
- }
复制代码 localhost:9003/echo-rest/OpenFeign
Hello OpenFeign
总结
回到顶部
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |