irpas技术客

Eureka进行服务调用服务报 java.lang.IllegalStateException: No instances available for XXX

大大的周 6372

使用RestTemplate进行微服务调用,报了一个java.lang.IllegalStateException异常

第一个可能原因

服务提供者 我这里导致的错误原因是没有进行服务发现,导致使用RestTemplate的时候始终找不到服务地址。 解决方式就是在服务提供者的启动类上加上@EnableDiscoveryClient注解然后重启服务提供者,目的就是让这个服务能够被发现,也就是消费者使用RestTemplate去调用服务提供者的时候,去注册中心找这个服务。

第二可能原因

消费者

使用@AutoWired注入的RestTemplate类,并且这个类需要我们通过@Bean注入加上@LoadBanlance 将下面的代码加入到启动类或者Configuration类中,将其注入到容器中。

@Bean @LoadBalanced RestTemplate initRestTemplate() { return new RestTemplate(); } 第三可能原因

服务提供者和消费者的yml配置文件说明 服务直接的调用,如果要使用服务名进行调用,应该使用下面这个spring.application.name的值进行访问 也就是 http://pay-provider/ 代替 http://127.0.0.1:8001


完整的案例

eureka注册中心

pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- spring-cloud-starter-netflix-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>3.0.1</version> </dependency> <!-- springboot监控器 辅助监控和调试来使用的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.3</version> </plugin> </plugins> </build>

启动类 加上@EnableEurekaServer注解

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class YjhApplication { public static void main(String[] args) { SpringApplication.run(YjhApplication.class, args); } }

yml配置文件

server: port: 8888 eureka: server: eviction-interval-timer-in-ms: 2000 # 超时时间2s enable-self-preservation: false # 不将自己注册到注册中心 client: service-url: defaultZone: http://localhost:8888/eureka/ register-with-eureka: false spring: application: name: eureka-server
服务提供者

pom.xml

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot监控器 辅助监控和调试来使用的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.0.1</version> </dependency>

启动类 加上@EnableEurekaClient和@EnableDiscoveryClient注解

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class ServerProviderApplication { public static void main(String[] args) { SpringApplication.run(ServerProviderApplication.class, args); } }

服务

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/") public String index(){ return "hello world! I am provider"; } }

yml配置文件

server: port: 8001 eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8888/eureka/ instance: prefer-ip-address: true #显示ip instance-id: server-provider spring: application: name: pay-provider # RestTemplate进行服务调用的名称地址
服务消费者

pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot监控器 辅助监控和调试来使用的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.0.1</version> </dependency>

启动类

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.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class ServerConsumerApplication { @Bean @LoadBalanced RestTemplate initRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServerConsumerApplication.class, args); } }

进行服务调用

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired RestTemplate restTemplate; @GetMapping("/hello") public String hello(){ return restTemplate.getForObject("http://pay-provider/",String.class); } @GetMapping("/") public String index(){ return "consumer"; } }

效果是通过消费者去调用服务提供者进行消费

也就是访问http://localhost:8002/hello 去调用http://localhost:8001/ 返回hello world! I am provider


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Eureka进行服务调用服务报 #no #instances #available #for #xxx