2020import com .google .common .collect .Lists ;
2121import com .google .gson .Gson ;
2222import com .google .gson .GsonBuilder ;
23- import com .google .gson .reflect .TypeToken ;
2423import jakarta .annotation .Resource ;
2524import jakarta .validation .constraints .NotNull ;
26- import org .apache .commons .lang3 .StringUtils ;
2725import org .apache .servicecomb .config .BootStrapProperties ;
2826import org .apache .servicecomb .registry .api .Discovery ;
2927import org .apache .servicecomb .registry .consul .config .ConsulDiscoveryProperties ;
3735import org .kiwiproject .consul .option .Options ;
3836import org .slf4j .Logger ;
3937import org .slf4j .LoggerFactory ;
40- import org .springframework .beans .factory .annotation .Value ;
4138import org .springframework .core .env .Environment ;
4239import org .springframework .util .CollectionUtils ;
4340
44- import java .lang .reflect .Type ;
4541import java .util .ArrayList ;
4642import java .util .List ;
4743import java .util .Map ;
4844
4945public class ConsulDiscovery implements Discovery <ConsulDiscoveryInstance > {
5046
51- private static final Logger logger = LoggerFactory .getLogger (ConsulDiscovery .class );
47+ private static final Logger LOGGER = LoggerFactory .getLogger (ConsulDiscovery .class );
5248
5349 @ Resource
5450 private ConsulProperties consulProperties ;
@@ -62,11 +58,6 @@ public class ConsulDiscovery implements Discovery<ConsulDiscoveryInstance> {
6258 @ Resource
6359 private Environment environment ;
6460
65- @ Value ("${servicecomb.rest.address:127.0.0.1:8080}" )
66- private String restAddress ;
67-
68- private String serverPort = "" ;
69-
7061 private List <ConsulDiscoveryInstance > consulDiscoveryInstanceList ;
7162
7263 private InstanceChangedListener <ConsulDiscoveryInstance > instanceChangedListener ;
@@ -85,14 +76,14 @@ public boolean enabled(String application, String serviceName) {
8576
8677 @ Override
8778 public List <ConsulDiscoveryInstance > findServiceInstances (String application , String serviceName ) {
88- logger .info ("findServiceInstances application:{}, serviceName:{}" , application , serviceName );
79+ LOGGER .info ("findServiceInstances application:{}, serviceName:{}" , application , serviceName );
8980 consulDiscoveryInstanceList = getInstances (serviceName );
9081 return consulDiscoveryInstanceList ;
9182 }
9283
9384 @ Override
9485 public List <String > findServices (String application ) {
95- logger .info ("ConsulDiscovery findServices(application={})" , application );
86+ LOGGER .info ("ConsulDiscovery findServices(application={})" , application );
9687 Map <String , Service > services = consulClient .agentClient ().getServices ();
9788 return Lists .newArrayList (services .keySet ());
9889 }
@@ -109,17 +100,12 @@ public boolean enabled() {
109100
110101 @ Override
111102 public void init () {
112- logger .info ("ConsulDiscovery init" );
113- if (restAddress .contains ("?" )) {
114- serverPort = restAddress .substring (restAddress .indexOf (":" ) + 1 , restAddress .indexOf ("?" ));
115- } else {
116- serverPort = restAddress .substring (restAddress .indexOf (":" ) + 1 );
117- }
103+ LOGGER .info ("ConsulDiscovery init" );
118104 }
119105
120106 @ Override
121107 public void run () {
122- logger .info ("ConsulDiscovery run" );
108+ LOGGER .info ("ConsulDiscovery run" );
123109 String serviceName = BootStrapProperties .readServiceName (environment );
124110 HealthClient healthClient = consulClient .healthClient ();
125111 svHealth = ServiceHealthCache .newCache (healthClient , serviceName , true , Options .BLANK_QUERY_OPTIONS , consulDiscoveryProperties .getWatchSeconds ());
@@ -133,11 +119,10 @@ public void destroy() {
133119 if (svHealth != null ) {
134120 svHealth .stop ();
135121 }
136- String serviceName = BootStrapProperties .readServiceName (environment );
137- String serviceId = serviceName + "-" + serverPort ;
138- logger .info ("ConsulDiscovery destroy consul service id={}" , serviceId );
122+ String serviceId = consulDiscoveryProperties .getServiceId ();
123+ LOGGER .info ("ConsulDiscovery destroy consul service id={}" , serviceId );
139124 if (consulClient != null ) {
140- logger .info ("ConsulDiscovery consulClient destroy" );
125+ LOGGER .info ("ConsulDiscovery consulClient destroy" );
141126 consulClient .agentClient ().deregister (serviceId );
142127 consulClient .destroy ();
143128 }
@@ -154,24 +139,8 @@ private void addInstancesToList(List<ConsulDiscoveryInstance> instances, String
154139 if (!CollectionUtils .isEmpty (healthServices )) {
155140 for (ServiceHealth serviceHealth : healthServices ) {
156141 Service service = serviceHealth .getService ();
157- logger .info ("healthService:{}" , service .getId ());
158- Map <String , String > meta = service .getMeta ();
159- ConsulInstance consulInstance = new ConsulInstance ();
160- consulInstance .setServiceId (service .getId ());
161- consulInstance .setServiceName (meta .get ("serviceName" ));
162- consulInstance .setInstanceId (meta .get ("instanceId" ));
163- consulInstance .setApplication (meta .get ("application" ));
164- consulInstance .setEnvironment (meta .get ("env" ));
165- consulInstance .setAlias (meta .get ("alias" ));
166- consulInstance .setVersion (meta .get ("version" ));
167142 Gson gson = new GsonBuilder ().disableHtmlEscaping ().create ();
168- String [] endpoints = gson .fromJson (meta .get ("endpoints" ), String [].class );
169- consulInstance .setEndpoints (Lists .newArrayList (endpoints ));
170- if (StringUtils .isNotBlank (meta .get ("properties" ))) {
171- Type type = new TypeToken <Map <String , String >>() {
172- }.getType ();
173- consulInstance .setProperties (gson .fromJson (meta .get ("properties" ), type ));
174- }
143+ ConsulInstance consulInstance = gson .fromJson (service .getMeta ().get ("meta" ), ConsulInstance .class );
175144 instances .add (new ConsulDiscoveryInstance (consulInstance ));
176145 }
177146 }
0 commit comments