springcloud 服务消费方未注册到注册中心

图片说明
图片说明

## **配置 application.yml**
server:
  port: 9001

eureka:
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defualtZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/

package com.itcodai.springcloud.controller;

import com.itcodai.springcloud.entity.TOrder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer/order")
public class OrderConsumerController {

    private static final String ORDER_PROVIDER_URL_PREFIX = "http://MICROSERVICE-ORDER";

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/get/{id}")
    public TOrder getOrder(@PathVariable Long id) {

        return restTemplate.getForObject(ORDER_PROVIDER_URL_PREFIX + "/provider/order/get/" + id, TOrder.class);
    }

    @RequestMapping("/get/list")
    public List<TOrder> getAll() {

        return restTemplate.getForObject(ORDER_PROVIDER_URL_PREFIX + "/provider/order/get/list", List.class);
    }
}

## _ RestTemplate 配置_****
 package com.itcodai.springcloud.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;

@Configuration
public class RestTmplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTmplate(){
        return new RestTemplate();
    }
}

2个回答

  1. 消费端不需要配register-with-eureka: false,fetch-registry: false
  2. Application上加上@EnableDiscoveryClient
guangcaiwudong
Kevin.Y.K 回复qq_42914617: 服务提供者和服务消费者对eureka来说都是eurekaclient,配置好service-url加上加上@EnableDiscoveryClient注解就可以了
3 个月之前 回复
guangcaiwudong
Kevin.Y.K 回复qq_42914617: 检查你的eurekaserver有没有启动
3 个月之前 回复
qq_42914617
qq_42914617 没作用,反而报com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
3 个月之前 回复

我们现在注册中心和配置中心都用的阿里的nacos.eureka已经不更新了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问