-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add spring-cloud-starter-cosky-discovery-ribbon
- Loading branch information
Showing
8 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
.../java/me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/ConditionalOnCoskyRibbon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@ConditionalOnProperty(value = "ribbon.cosky.enabled", matchIfMissing = true) | ||
public @interface ConditionalOnCoskyRibbon { | ||
} |
22 changes: 22 additions & 0 deletions
22
...a/me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/CoskyRibbonAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import me.ahoo.cosky.discovery.spring.cloud.discovery.ConditionalOnCoskyDiscoveryEnabled; | ||
import me.ahoo.cosky.discovery.spring.cloud.discovery.CoskyDiscoveryAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; | ||
import org.springframework.cloud.netflix.ribbon.RibbonClients; | ||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnBean(SpringClientFactory.class) | ||
@ConditionalOnCoskyRibbon | ||
@ConditionalOnCoskyDiscoveryEnabled | ||
@AutoConfigureAfter({RibbonAutoConfiguration.class, CoskyDiscoveryAutoConfiguration.class}) | ||
@RibbonClients(defaultConfiguration = CoskyRibbonClientConfiguration.class) | ||
public class CoskyRibbonAutoConfiguration { | ||
} |
38 changes: 38 additions & 0 deletions
38
...me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/CoskyRibbonClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import com.netflix.client.config.IClientConfig; | ||
import com.netflix.loadbalancer.ServerList; | ||
import lombok.var; | ||
import me.ahoo.cosky.discovery.ServiceDiscovery; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.netflix.ribbon.PropertiesFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnCoskyRibbon | ||
public class CoskyRibbonClientConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public ServerList<?> ribbonServerList(PropertiesFactory propertiesFactory, IClientConfig config, ServiceDiscovery serviceDiscovery) { | ||
|
||
if (propertiesFactory.isSet(ServerList.class, config.getClientName())) { | ||
ServerList serverList = propertiesFactory.get(ServerList.class, config, | ||
config.getClientName()); | ||
return serverList; | ||
} | ||
var serverList = new CoskyServerList(serviceDiscovery); | ||
serverList.initWithNiwsConfig(config); | ||
return serverList; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public CoskyServerIntrospector coskyServerIntrospector() { | ||
return new CoskyServerIntrospector(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...bbon/src/main/java/me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/CoskyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import com.netflix.loadbalancer.Server; | ||
import me.ahoo.cosky.discovery.Instance; | ||
import me.ahoo.cosky.discovery.ServiceInstance; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class CoskyServer extends Server { | ||
private final ServiceInstance instance; | ||
private final MetaInfo metaInfo; | ||
|
||
public CoskyServer(final ServiceInstance instance) { | ||
super(instance.getSchema(), instance.getHost(), instance.getPort()); | ||
this.instance = instance; | ||
this.metaInfo = new MetaInfo() { | ||
@Override | ||
public String getAppName() { | ||
return instance.getServiceId(); | ||
} | ||
|
||
@Override | ||
public String getServerGroup() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getServiceIdForDiscovery() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getInstanceId() { | ||
return instance.getInstanceId(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public MetaInfo getMetaInfo() { | ||
return metaInfo; | ||
} | ||
|
||
public ServiceInstance getInstance() { | ||
return instance; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...n/java/me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/CoskyServerIntrospector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import com.netflix.loadbalancer.Server; | ||
import org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class CoskyServerIntrospector extends DefaultServerIntrospector { | ||
|
||
@Override | ||
public boolean isSecure(Server server) { | ||
if (server instanceof CoskyServer) { | ||
return ((CoskyServer) server).getInstance().isSecure(); | ||
} | ||
return super.isSecure(server); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getMetadata(Server server) { | ||
if (server instanceof CoskyServer) { | ||
return ((CoskyServer) server).getInstance().getMetadata(); | ||
} | ||
return super.getMetadata(server); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../src/main/java/me/ahoo/cosky/discovery/spring/cloud/discovery/ribbon/CoskyServerList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon; | ||
|
||
import com.netflix.client.config.IClientConfig; | ||
import com.netflix.loadbalancer.AbstractServerList; | ||
import lombok.var; | ||
import me.ahoo.cosky.core.util.Futures; | ||
import me.ahoo.cosky.discovery.ServiceDiscovery; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class CoskyServerList extends AbstractServerList<CoskyServer> { | ||
private String serviceId; | ||
private final ServiceDiscovery serviceDiscovery; | ||
|
||
public CoskyServerList(ServiceDiscovery serviceDiscovery) { | ||
this.serviceDiscovery = serviceDiscovery; | ||
} | ||
|
||
@Override | ||
public void initWithNiwsConfig(IClientConfig iClientConfig) { | ||
this.serviceId = iClientConfig.getClientName(); | ||
} | ||
|
||
@Override | ||
public List<CoskyServer> getInitialListOfServers() { | ||
return getCoskyServers(); | ||
} | ||
|
||
private List<CoskyServer> getCoskyServers() { | ||
var getInstancesFuture = serviceDiscovery.getInstances(this.serviceId); | ||
var instances = Futures.getUnChecked(getInstancesFuture, Duration.ofSeconds(2)); | ||
if (instances.isEmpty()) { | ||
Collections.emptyList(); | ||
} | ||
return instances.stream().map(instance -> new CoskyServer(instance)).collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Return updated list of servers. This is called say every 30 secs | ||
* (configurable) by the Loadbalancer's Ping cycle | ||
*/ | ||
@Override | ||
public List<CoskyServer> getUpdatedListOfServers() { | ||
return getCoskyServers(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
spring-cloud-starter-cosky-discovery-ribbon/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Auto Configuration | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
me.ahoo.cosky.discovery.spring.cloud.discovery.ribbon.CoskyRibbonAutoConfiguration | ||
|