Skip to content

Commit 4a2f025

Browse files
committed
优化gray-server
1 parent c4f00a0 commit 4a2f025

File tree

53 files changed

+1088
-357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1088
-357
lines changed

pom.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@
5454
<maven.compiler.target>1.8</maven.compiler.target>
5555
<java.version>1.8</java.version>
5656
<spring-cloud.version>Edgware.SR5</spring-cloud.version>
57-
<springfox.version>2.7.0</springfox.version>
57+
<springfox.version>2.9.2</springfox.version>
5858
<lombok.version>1.18.8</lombok.version>
5959
<slf4j.version>1.7.26</slf4j.version>
6060
<commons-lang3.version>3.5</commons-lang3.version>
6161
<guava.version>27.0.1-jre</guava.version>
6262
<mapstruct.version>1.1.0.Final</mapstruct.version>
63+
6364
</properties>
6465

6566

@@ -210,16 +211,16 @@
210211
<version>3.6.0</version>
211212
<configuration>
212213
<annotationProcessorPaths>
213-
<path>
214-
<groupId>org.mapstruct</groupId>
215-
<artifactId>mapstruct-processor</artifactId>
216-
<version>${mapstruct.version}</version>
217-
</path>
218214
<path>
219215
<groupId>org.projectlombok</groupId>
220216
<artifactId>lombok</artifactId>
221217
<version>${lombok.version}</version>
222218
</path>
219+
<path>
220+
<groupId>org.mapstruct</groupId>
221+
<artifactId>mapstruct-processor</artifactId>
222+
<version>${mapstruct.version}</version>
223+
</path>
223224
</annotationProcessorPaths>
224225
</configuration>
225226
</plugin>

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/communication/HttpInformationClient.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public HttpInformationClient(String baseUrl, RestTemplate rest) {
2828

2929
@Override
3030
public List<GrayInstance> allGrayInstances() {
31-
String url = this.baseUrl + "/gray/instance/enable";
31+
String url = this.baseUrl + "/gray/instances/enable";
3232
ParameterizedTypeReference<List<GrayInstance>> typeRef = new ParameterizedTypeReference<List<GrayInstance>>() {
3333
};
3434

@@ -44,7 +44,7 @@ public List<GrayInstance> allGrayInstances() {
4444

4545
@Override
4646
public void addGrayInstance(GrayInstance grayInstance) {
47-
String url = this.baseUrl + "/grayinstance/";
47+
String url = this.baseUrl + "/gray/instance/";
4848
try {
4949
rest.postForEntity(url, grayInstance, null);
5050
} catch (RuntimeException e) {
@@ -53,9 +53,22 @@ public void addGrayInstance(GrayInstance grayInstance) {
5353
}
5454
}
5555

56+
@Override
57+
public GrayInstance getGrayInstance(String serviceId, String instanceId) {
58+
String url = this.baseUrl + "/gray/instance?serviceId={serviceId}&instanceId={instanceId}";
59+
try {
60+
ResponseEntity<GrayInstance> responseEntity =
61+
rest.getForEntity(url, GrayInstance.class, serviceId, instanceId);
62+
return responseEntity.getBody();
63+
} catch (RuntimeException e) {
64+
log.error("获取灰度实例", e);
65+
throw e;
66+
}
67+
}
68+
5669
@Override
5770
public void serviceDownline(String instanceId) {
58-
String url = this.baseUrl + "/grayinstance/{id}/switchStatus?switch=0";
71+
String url = this.baseUrl + "/gray/instance/{id}/switchStatus?switch=0";
5972
Map<String, String> params = new HashMap<>();
6073
params.put("instanceId", instanceId);
6174
try {

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/communication/InformationClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public interface InformationClient {
2727
*/
2828
void addGrayInstance(GrayInstance grayInstance);
2929

30+
/**
31+
* 获取灰度实例的信息
32+
*
33+
* @param serviceId 服务id
34+
* @param instanceId 实例id
35+
* @return
36+
*/
37+
GrayInstance getGrayInstance(String serviceId, String instanceId);
38+
3039

3140
/**
3241
* 灰度实例下线

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/communication/InformationClientDecorator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public abstract class InformationClientDecorator implements InformationClient {
1414
public enum RequestType {
1515
AddGrayInstance,
1616
ServiceDownline,
17-
AllGrayInstances
17+
AllGrayInstances,
18+
GetGrayInstance
1819
}
1920

2021

@@ -27,6 +28,20 @@ public interface RequestExecutor<R> {
2728

2829
protected abstract <R> R execute(RequestExecutor<R> requestExecutor);
2930

31+
@Override
32+
public GrayInstance getGrayInstance(String serviceId, String instanceId) {
33+
return execute(new RequestExecutor<GrayInstance>() {
34+
@Override
35+
public GrayInstance execute(InformationClient delegate) {
36+
return delegate.getGrayInstance(serviceId, instanceId);
37+
}
38+
39+
@Override
40+
public RequestType getRequestType() {
41+
return RequestType.GetGrayInstance;
42+
}
43+
});
44+
}
3045

3146
@Override
3247
public List<GrayInstance> allGrayInstances() {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cn.springcloud.gray.event;
2+
3+
import java.io.Serializable;
4+
5+
public enum EventType implements Serializable {
6+
DOWN, UPDATE
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.springcloud.gray.event;
2+
3+
import cn.springcloud.gray.exceptions.EventException;
4+
5+
public interface GrayEventListener {
6+
7+
8+
void onEvent(GrayEventMsg msg) throws EventException;
9+
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.springcloud.gray.event;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.io.Serializable;
7+
8+
@Setter
9+
@Getter
10+
public class GrayEventMsg implements Serializable {
11+
private static final long serialVersionUID = -8114806214567175543L;
12+
private String serviceId;
13+
private String instanceId;
14+
private EventType eventType;
15+
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.springcloud.gray.event;
2+
3+
import cn.springcloud.gray.exceptions.EventException;
4+
5+
public interface GrayEventPublisher {
6+
7+
void publishEvent(GrayEventMsg msg) throws EventException;
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.springcloud.gray.exceptions;
2+
3+
public class EventException extends RuntimeException {
4+
5+
private static final long serialVersionUID = -2822193541909545645L;
6+
7+
public EventException() {
8+
}
9+
10+
public EventException(String message) {
11+
super(message);
12+
}
13+
14+
public EventException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public EventException(Throwable cause) {
19+
super(cause);
20+
}
21+
22+
public EventException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
23+
super(message, cause, enableSuppression, writableStackTrace);
24+
}
25+
}

spring-cloud-gray-samples/spring-cloud-gray-server-sample/src/main/resources/config/application.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ server:
33
spring:
44
application:
55
name: gray-server
6+
#通用数据源配置
67
datasource:
78
driver-class-name: com.mysql.jdbc.Driver
89
url: jdbc:mysql://localhost:3306/gray_server01?charset=utf8mb4&useSSL=false
910
username: root
10-
password: xiaoyu
11+
password: root
1112
# Hikari 数据源专用配置
1213
hikari:
1314
maximum-pool-size: 20
@@ -16,8 +17,9 @@ spring:
1617
jpa:
1718
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
1819
show-sql: true
20+
generate-ddl: true
1921
hibernate:
20-
ddl-auto: create
22+
ddl-auto: none
2123
eureka:
2224
client:
2325
register-with-eureka: true

0 commit comments

Comments
 (0)