1
resposta

Não consigo subir a loja

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

No console, apresenta um erro indicando que preciso importar e injetar a classe DiscoveryClient, porém eu já diz isso como mostra na imagem acima, porém por algum motivo não está reconhecendo.

package br.com.alura.microservice.loja.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.discovery.DiscoveryClient;

import br.com.alura.microservice.loja.controller.dto.CompraDto;
import br.com.alura.microservice.loja.controller.dto.InfoFornecedorDto;

@Service
public class CompraService {

    @Autowired
    private RestTemplate client;
    @Autowired
    private DiscoveryClient eurekaClient;

    public void realizaCompra(CompraDto compra) {

        ResponseEntity<InfoFornecedorDto> exchange =
                client.exchange("http://fornecedor/info/" + compra.getEndereco().getEstado(),
                HttpMethod.GET,
                null,
                InfoFornecedorDto.class);

        eurekaClient.getInstancesById("fornecedor")
                .forEach(fornecedor -> System.out.println("localhost: " + fornecedor.getPort()));

        System.out.println(exchange.getBody().getEndereco());
    }
}
Description:

Field eurekaClient in br.com.alura.microservice.loja.service.CompraService required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.netflix.discovery.DiscoveryClient' in your configuration.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>br.com.alura.microservice.loja</groupId>
    <artifactId>loja</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>loja</name>
    <description>Loja</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <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>


        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>3.0.4</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
1 resposta

Olá Camila, tente importar o DiscoveryClient do Spring Cloud:

org.springframework.cloud.client.discovery.DiscoveryClient

https://stackoverflow.com/questions/42845084/cannot-find-discoveryclient-bean-error-in-spring-boot