RibbonConsumeControl.java
2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<String, List<ServiceInstance>> serviceUrl() {
Map<String, List<ServiceInstance>> msl = new HashMap<>();
List<String> services = discoveryClient.getServices();
for (String service : services) {
List<ServiceInstance> sis = discoveryClient.getInstances(service);
msl.put(service, sis);
}
return msl;
}
}