Nacos使用restTemplate调用其他服务提示找不到host(根据服务名调用),已解决

1、检查服务是否在nacos中注册成功

进入nacos后台查看服务是否注册进去了,如果没有注册进去去官网查看详细配置Nacos使用

在这里插入图片描述

2、检查Springboot版本

由于Netflix的组件从2020年就开始停止维护了,而Ribbon恰恰是Netflix开源的负载均衡工具,针对Netflix停止维护后,SpringCloud也开始去慢慢的去除Netflix的组件功能,因此在SpringCloud新版本中都将ribbon弃用了,导致程序报错找不到 host。如果你的springboot版本相对较新(如果2021年后的版本存在这个问题,那大概率是没有ribbon组件)。只需要在pom.xml中引入依赖就可以完美解决了。

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.1.0</version>
        </dependency>

以下是我使用SpringCloud3.1.0实现方式:

服务被调用者:
pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

application.properties

server.port=8080
spring.application.name=producter
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

启动类
这一部分跟官网提供的案例是一样的

package com.example.invoker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ProducterApplication {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(ProducterApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://invoker/echo/" + str, String.class);
        }
    }
}

服务调用者

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

application.properties

server.port=8070
spring.application.name=invoker
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

启动类

package com.example.invoker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
public class InvokerApplication {

    public static void main(String[] args) {
        SpringApplication.run(InvokerApplication.class, args);
    }
    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }
}

以上就是我解决Nacos使用restTemplate调用其他服务提示找不到host的解决方案了,如果有不对的地方请指正

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Nacos 是一个动态服务发现和配置管理的开源项目,而 RestTemplateSpring Framework 提供的一个用于发送 HTTP 请求的工具类。你可以使用 RestTemplate调用 Nacos 提供的服务。 首先,你需要在项目中添加 Nacos 的依赖,例如: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` 然后,你可以通过 `@LoadBalanced` 注解为 RestTemplate 添加负载均衡能力,以便在调用服务时自动实现负载均衡。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class MyComponent { @Autowired private RestTemplate restTemplate; public void doSomething() { String url = "http://nacos-service-provider/hello"; String response = restTemplate.getForObject(url, String.class); // 处理响应数据 } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 在上述代码中,`nacos-service-provider` 是 Nacos 注册的服务,`/hello` 是服务提供者的接口路径。通过调用 `restTemplate.getForObject()` 方法可以发送 GET 请求获取服务提供者的响应数据。 希望这个例子对你有帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值