Skip to content

Commit 953af7b

Browse files
committed
测试demo
1 parent f4ca74a commit 953af7b

File tree

9 files changed

+290
-2
lines changed

9 files changed

+290
-2
lines changed

spring-cloud-gray-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<module>spring-cloud-gray-btrace-sample</module>
2424
<module>spring-cloud-gray-consumer-nacos-config-sample</module>
2525
<module>spring-cloud-gray-apollo-config-sample</module>
26+
<module>spring-cloud-gray-server2-sample</module>
2627
</modules>
2728

2829
<build>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ eureka:
5959

6060
gray:
6161
server:
62+
cluster:
63+
peerNodes:
64+
- host: localhost
65+
port: 20202
66+
- host: localhost
67+
port: 20203
6268
evictionIntervalTimerInMs: 30000
6369
instance:
6470
normalInstanceStatus: STARTING,UP,OUT_OF_SERVICE
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-cloud-gray-samples</artifactId>
7+
<groupId>cn.springcloud.gray</groupId>
8+
<version>A.2.0.0.RC1</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-cloud-gray-server2-sample</artifactId>
13+
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-test</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.cloud</groupId>
26+
<artifactId>spring-cloud-starter-eureka</artifactId>
27+
<exclusions>
28+
<exclusion>
29+
<artifactId>bcpkix-jdk15on</artifactId>
30+
<groupId>org.bouncycastle</groupId>
31+
</exclusion>
32+
</exclusions>
33+
</dependency>
34+
<dependency>
35+
<groupId>cn.springcloud.gray</groupId>
36+
<artifactId>spring-cloud-starter-gray-server</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>cn.springcloud.gray</groupId>
40+
<artifactId>spring-cloud-gray-server-plugin-eureka</artifactId>
41+
</dependency>
42+
43+
<!-- nacos -->
44+
<!-- <dependency>-->
45+
<!-- <groupId>cn.springcloud.gray</groupId>-->
46+
<!-- <artifactId>spring-cloud-gray-server-plugin-nacos-discovery</artifactId>-->
47+
<!-- </dependency>-->
48+
<!-- <dependency>-->
49+
<!-- <groupId>com.alibaba.cloud</groupId>-->
50+
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
51+
<!-- </dependency>-->
52+
53+
54+
<!-- <dependency>-->
55+
<!-- <groupId>cn.springcloud.gray</groupId>-->
56+
<!-- <artifactId>spring-cloud-gray-server-plugin-event-stream</artifactId>-->
57+
<!-- </dependency>-->
58+
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<!--skip deploy -->
65+
<artifactId>maven-deploy-plugin</artifactId>
66+
<configuration>
67+
<skip>true</skip>
68+
</configuration>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.springcloud.gray.server.app2;
2+
3+
import cn.springcloud.gray.server.EnableGrayServer;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.builder.SpringApplicationBuilder;
7+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
8+
import org.springframework.core.env.Environment;
9+
10+
import java.net.InetAddress;
11+
import java.net.UnknownHostException;
12+
13+
@SpringBootApplication
14+
@EnableGrayServer
15+
@EnableDiscoveryClient
16+
public class GrayServer2Application {
17+
18+
private static final org.slf4j.Logger log = LoggerFactory.getLogger(GrayServer2Application.class);
19+
20+
21+
public static void main(String[] args) throws UnknownHostException {
22+
Environment env = new SpringApplicationBuilder(GrayServer2Application.class).web(true).run(args).getEnvironment();
23+
log.info(
24+
"\n----------------------------------------------------------\n\t"
25+
+ "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://127.0.0.1:{}\n\t"
26+
+ "External: \thttp://{}:{}\n----------------------------------------------------------",
27+
env.getProperty("spring.application.name"), env.getProperty("server.port"),
28+
InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"));
29+
30+
String configServerStatus = env.getProperty("configserver.status");
31+
log.info(
32+
"\n----------------------------------------------------------\n\t"
33+
+ "Config Server: \t{}\n----------------------------------------------------------",
34+
configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.springcloud.gray.server.app2.web.domain.fo;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
@Data
7+
@NoArgsConstructor
8+
public class LoginFO {
9+
private String username;
10+
private String password;
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.springcloud.gray.server.app2.web.domain.vo;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
@Data
10+
@NoArgsConstructor
11+
public class UserInfoVO {
12+
private List<String> roles = Arrays.asList("admin");
13+
private String introduction = "I am a super administrator";
14+
private String avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif";
15+
private String name = "Super Admin";
16+
private String account;
17+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cn.springcloud.gray.server.app2.web.rest;
2+
3+
import cn.springcloud.gray.api.ApiRes;
4+
import cn.springcloud.gray.server.app2.web.domain.fo.LoginFO;
5+
import cn.springcloud.gray.server.app2.web.domain.vo.UserInfoVO;
6+
import cn.springcloud.gray.server.oauth2.Oauth2Service;
7+
import cn.springcloud.gray.server.oauth2.TokenRequestInfo;
8+
import com.google.common.collect.ImmutableBiMap;
9+
import org.apache.commons.lang3.StringUtils;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.security.core.GrantedAuthority;
14+
import org.springframework.security.core.userdetails.User;
15+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
16+
import org.springframework.web.bind.annotation.*;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
@RequestMapping("/gray/user")
23+
//@RestController
24+
public class UserResource {
25+
26+
private static final Logger log = LoggerFactory.getLogger(UserResource.class);
27+
28+
@Autowired
29+
private Oauth2Service oauth2Service;
30+
31+
@PostMapping(value = "/login")
32+
public ApiRes<Map<String, String>> login(@RequestBody LoginFO fo) {
33+
if (!StringUtils.equals("admin", fo.getUsername())
34+
|| !StringUtils.equals("abc~!@345", fo.getPassword())) {
35+
return ApiRes.<Map<String, String>>builder()
36+
.code("1")
37+
.message("用户名或密码不正确")
38+
.build();
39+
}
40+
41+
List<GrantedAuthority> authorities = new ArrayList<>();
42+
User user = new User(fo.getUsername(), fo.getPassword(), authorities);
43+
OAuth2AccessToken oAuth2AccessToken = oauth2Service.getAccessToken(
44+
TokenRequestInfo.builder().userDetails(user).build());
45+
return ApiRes.<Map<String, String>>builder()
46+
.code("0")
47+
.data(ImmutableBiMap.of("token", oAuth2AccessToken.getValue()))
48+
.build();
49+
}
50+
51+
@RequestMapping(value = "/info", method = {RequestMethod.OPTIONS, RequestMethod.GET})
52+
public ApiRes<UserInfoVO> info(@RequestParam(required = false, value = "token") String token) {
53+
log.info("accpect token is {}", token);
54+
return ApiRes.<UserInfoVO>builder().code("0").data(new UserInfoVO()).build();
55+
}
56+
57+
@PostMapping(value = "/logout")
58+
public ApiRes<Map<String, String>> logout() {
59+
return ApiRes.<Map<String, String>>builder()
60+
.code("0")
61+
.data(ImmutableBiMap.of())
62+
.build();
63+
}
64+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
server:
2+
port: 20203
3+
spring:
4+
application:
5+
name: gray-Server
6+
7+
#通用数据源配置
8+
datasource:
9+
driver-class-name: com.mysql.jdbc.Driver
10+
url: jdbc:mysql://localhost:3306/gray_server01?charset=utf8mb4&useSSL=false
11+
username: root
12+
password: root
13+
# Hikari 数据源专用配置
14+
hikari:
15+
maximum-pool-size: 20
16+
minimum-idle: 5
17+
# JPA 相关配置
18+
jpa:
19+
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
20+
show-sql: true
21+
generate-ddl: true
22+
hibernate:
23+
ddl-auto: update
24+
rabbitmq:
25+
addresses: 127.0.0.1:5672
26+
username: admin
27+
password: admin
28+
cloud:
29+
nacos:
30+
discovery:
31+
server-addr: 127.0.0.1:8848
32+
stream:
33+
bindings:
34+
# GrayEventInput:
35+
# group: service-a
36+
# destination: test
37+
# consumer:
38+
# concurrency: 1 #并发数
39+
# max-attempts: 1
40+
GrayEventOutput:
41+
destination: gray_event
42+
# rabbit:
43+
# bindings:
44+
# GrayEventInput:
45+
# consumer:
46+
# maxConcurrency: 1 #并发数
47+
# prefetch: 1 #从mq一次获取消息的数量
48+
# requeueRejected: true #spring cloud stream 如果出现异常, 是否需要重新投递消息, false表示丢弃。 也有相应的Exception, true-MessageRejectedWhileStoppingException false-AmqpRejectAndDontRequeueException
49+
# auto-bind-dlq: true
50+
# acknowledgeMode: AUTO
51+
# acknowledgeMode: MANUAL
52+
eureka:
53+
client:
54+
register-with-eureka: true
55+
fetch-registry: true
56+
serviceUrl:
57+
defaultZone: http://localhost:20001/eureka/
58+
registry-fetch-interval-seconds: 5
59+
60+
gray:
61+
server:
62+
cluster:
63+
peer-nodes:
64+
- host: localhost
65+
port: 20202
66+
- host: localhost
67+
port: 20203
68+
evictionIntervalTimerInMs: 30000
69+
instance:
70+
normalInstanceStatus: STARTING,UP,OUT_OF_SERVICE
71+
eviction:
72+
enabled: true
73+
evictionIntervalTimerInMs: 86400000
74+
evictionInstanceStatus: DOWN,UNKNOWN
75+
lastUpdateDateExpireDays: 1

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ gray:
6666
hystrix:
6767
enabled: true
6868
server:
69-
url: http://localhost:20202
70-
loadbalanced: false
69+
url: http://gray-Server
70+
loadbalanced: true
7171
retryable: true
7272
retryNumberOfRetries: 3
7373
client:
@@ -119,4 +119,9 @@ gray:
119119
# client:
120120
# instance:
121121
# grayEnroll: false #是否在启动后自动注册成灰度实例
122+
logging:
123+
level:
124+
cn:
125+
springcloud:
126+
gray: debug
122127
# serverUrl: http://localhost:20202 #灰度服务端的url

0 commit comments

Comments
 (0)