zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package com.codingapi.tm.manager.service.impl;
Z 2
3 import com.codingapi.tm.Constants;
4 import com.codingapi.tm.config.ConfigReader;
5 import com.codingapi.tm.framework.utils.SocketManager;
6 import com.codingapi.tm.manager.service.MicroService;
7 import com.codingapi.tm.model.TxServer;
8 import com.codingapi.tm.model.TxState;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.cloud.client.ServiceInstance;
11 import org.springframework.cloud.client.discovery.DiscoveryClient;
12 import org.springframework.stereotype.Service;
13 import org.springframework.web.client.RestTemplate;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 /**
21  * create by lorne on 2017/11/11
22  */
23 @Service
24 public class MicroServiceImpl implements MicroService {
25
26
27     @Autowired
28     private RestTemplate restTemplate;
29
30     @Autowired
31     private ConfigReader configReader;
32
33
34     @Autowired
35     private DiscoveryClient discoveryClient;
36
37
38
39     private boolean isIp(String ipAddress) {
40         String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
41         Pattern pattern = Pattern.compile(ip);
42         Matcher matcher = pattern.matcher(ipAddress);
43         return matcher.matches();
44     }
45
46
47
48     @Override
49     public TxState getState() {
50         TxState state = new TxState();
51         String ipAddress = discoveryClient.getLocalServiceInstance().getHost();
52         if(!isIp(ipAddress)){
53             ipAddress = "127.0.0.1";
54         }
55         state.setIp(ipAddress);
56         state.setPort(Constants.socketPort);
57         state.setMaxConnection(SocketManager.getInstance().getMaxConnection());
58         state.setNowConnection(SocketManager.getInstance().getNowConnection());
59         state.setRedisSaveMaxTime(configReader.getRedisSaveMaxTime());
60         state.setTransactionNettyDelayTime(configReader.getTransactionNettyDelayTime());
61         state.setTransactionNettyHeartTime(configReader.getTransactionNettyHeartTime());
62         state.setNotifyUrl(configReader.getCompensateNotifyUrl());
63         state.setCompensate(configReader.isCompensateAuto());
64         state.setCompensateTryTime(configReader.getCompensateTryTime());
65         state.setCompensateMaxWaitTime(configReader.getCompensateMaxWaitTime());
66         state.setSlbList(getServices());
67         return state;
68     }
69
70     private List<String> getServices(){
71         List<String> urls = new ArrayList<>();
72         List<ServiceInstance>  serviceInstances = discoveryClient.getInstances(tmKey);
73         for (ServiceInstance instanceInfo : serviceInstances) {
74             urls.add(instanceInfo.getUri().toASCIIString());
75         }
76         return urls;
77     }
78
79     @Override
80     public TxServer getServer() {
81         List<String> urls= getServices();
82         List<TxState> states = new ArrayList<>();
83         for(String url:urls){
84             try {
85                 TxState state = restTemplate.getForObject(url + "/tx/manager/state", TxState.class);
86                 states.add(state);
87             } catch (Exception e) {
88             }
89
90         }
91         if(states.size()<=1) {
92             TxState state = getState();
93             if (state.getMaxConnection() > state.getNowConnection()) {
94                 return TxServer.format(state);
95             } else {
96                 return null;
97             }
98         }else{
99             //找默认数据
100             TxState state = getDefault(states,0);
101             if (state == null) {
102                 //没有满足的默认数据
103                 return null;
104             }
105             return TxServer.format(state);
106         }
107     }
108
109     private TxState getDefault(List<TxState> states, int index) {
110         TxState state = states.get(index);
111         if (state.getMaxConnection() == state.getNowConnection()) {
112             index++;
113             if (states.size() - 1 >= index) {
114                 return getDefault(states, index);
115             } else {
116                 return null;
117             }
118         } else {
119             return state;
120         }
121     }
122
123 }