package com.kwan.springcloud.controller; import com.alibaba.fastjson.JSONArray; import com.kwan.springcloud.response.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j @RestController public class HelloController { @Autowired private DiscoveryClient client; @Autowired private Registration registration; /** * produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE * 解决不是json显示问题 */ @RequestMapping(value = "/hello", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) public Result index() { List instances = client.getInstances(registration.getServiceId()); log.info(JSONArray.toJSONString(instances)); return Result.ok(instances); } }