Skip to content

Commit

Permalink
consul
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed May 10, 2018
1 parent 9cc8f9f commit 346b225
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 92 deletions.
54 changes: 25 additions & 29 deletions sds-delivery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
<description>sds delivery framework</description>
<url>https://github.com/1991wangliang/sds</url>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<licenses>
<license>
<name>The Apache License, Version 2.0</name>
Expand Down Expand Up @@ -51,12 +60,12 @@
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compile.source>1.7</maven.compile.source>
<maven.compile.target>1.7</maven.compile.target>
<maven.compile.source>1.8</maven.compile.source>
<maven.compile.target>1.8</maven.compile.target>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>

<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<org.springframework-version>4.3.7.RELEASE</org.springframework-version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>


</properties>

Expand All @@ -82,19 +91,6 @@
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -104,26 +100,26 @@

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>


</dependencies>



<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

Expand Down Expand Up @@ -61,7 +60,7 @@ public void channelInactive(ChannelHandlerContext ctx) {
private ServiceInstance getInstance(String ipPort) {
List<ServiceInstance> instances = discoveryClient.getInstances(DeliveryServerService.SOCKET_SERVER_KEY);
for (ServiceInstance instance : instances) {
String ip = ((EurekaDiscoveryClient.EurekaServiceInstance) instance).getInstanceInfo().getIPAddr();
String ip = instance.getHost();
int port = instance.getPort();
String instancesIpPort = String.format("%s:%d", ip, port);
if (instancesIpPort.equals(ipPort)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import com.lorne.sds.server.model.DeliveryModel;
import com.lorne.sds.server.service.RedisService;
import com.lorne.sds.server.service.SettingService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand All @@ -24,14 +21,16 @@
@Service
public class SettingServiceImpl implements SettingService{

private Logger logger = LoggerFactory.getLogger(SettingServiceImpl.class);

@Autowired
private EurekaRegistration registration;
private DiscoveryClient discoveryClient;

@Autowired
private RedisService redisService;

@Autowired
private Registration registration;


@Value("${delivery.port}")
private int deliveryPort;
Expand All @@ -41,22 +40,9 @@ public class SettingServiceImpl implements SettingService{
private int maxCheckTime;


@Autowired
private EurekaClient eurekaClient;

public List<InstanceInfo> getConfigServiceInstances(String key) {
Application application = eurekaClient.getApplication(key);
if (application == null) {
logger.error("获取eureka服务失败!");
}
return application!=null?application.getInstances():new ArrayList<InstanceInfo>();
}



@Override
public String getDeliveryIp() {
return registration.getInstanceConfig().getIpAddress();
return registration.getHost();
}

@Override
Expand All @@ -81,10 +67,10 @@ private boolean isIp(String ipAddress) {

private List<String> getServices(String key){
List<String> urls = new ArrayList<>();
List<InstanceInfo> instanceInfos =getConfigServiceInstances(key);
for (InstanceInfo instanceInfo : instanceInfos) {
String url = instanceInfo.getHomePageUrl();
String address = instanceInfo.getIPAddr();
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(key);
for (ServiceInstance instanceInfo : serviceInstances) {
String url = instanceInfo.getUri().toString();
String address = instanceInfo.getHost();
if (isIp(address)) {
urls.add(url);
}else{
Expand Down
47 changes: 21 additions & 26 deletions sds-socket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<description>sds socket framework</description>
<url>https://github.com/1991wangliang/sds</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
Expand Down Expand Up @@ -55,7 +62,8 @@
<maven.compile.target>1.7</maven.compile.target>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>

<spring-cloud.version>1.3.1.RELEASE</spring-cloud.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>

<org.springframework-version>4.3.7.RELEASE</org.springframework-version>

</properties>
Expand All @@ -76,46 +84,33 @@
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>${spring-cloud.version}</version>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>${spring-cloud.version}</version>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.lorne.sds.server;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServerConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
Expand All @@ -20,22 +20,22 @@
@Service
public class SocketControlImpl implements SocketControl {


private AttributeKey attributeKey = AttributeKey.valueOf(SocketControlImpl.class.getName());

@Autowired
private EurekaRegistration registration;

@Autowired
private DeliveryClient deliveryClient;

@Autowired
private Registration registration;

private String ipPort = null;

@Override
public String getIpPort() {
if(ipPort==null) {
String ipAddress = registration.getInstanceConfig().getIpAddress();
int port = registration.getNonSecurePort();
String ipAddress = registration.getHost();
int port = registration.getPort();
ipPort = String.format("%s:%d", ipAddress, port);
}
return ipPort;
Expand All @@ -48,7 +48,7 @@ public String getServiceId() {

@Override
public String getInstanceId() {
return registration.getInstanceConfig().getInstanceId();
return registration.getServiceId();
}

@Override
Expand Down

0 comments on commit 346b225

Please sign in to comment.