package com.sincere.ribbon.control; import com.sincere.ribbon.model.Login; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping(value = "/ribbon/*") public class RibbonConsumeControl { private static final Logger LOG = LoggerFactory.getLogger(RibbonConsumeControl.class); @Autowired RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @RequestMapping(value = "login", method = RequestMethod.GET) public String login(@RequestParam("account") String account, @RequestParam("password") String password, @RequestParam("userType") String userType) { LOG.error("登录中:" + account + password + userType); HttpHeaders headers = new HttpHeaders(); Login login = new Login(); login.setAccount(account); login.setPassword(password); login.setUserType(userType); String result = restTemplate.postForEntity("http://authserver/login", login, String.class).getBody(); LOG.error("登录结果:" + result); return result; } @GetMapping("serviceurl") public Map> serviceUrl() { Map> msl = new HashMap<>(); List services = discoveryClient.getServices(); for (String service : services) { List sis = discoveryClient.getInstances(service); msl.put(service, sis); } return msl; } }