Skip to content

Commit b5a155c

Browse files
committed
Simplify cloud examples
1 parent f0302bd commit b5a155c

File tree

22 files changed

+87
-319
lines changed

22 files changed

+87
-319
lines changed

examples/cloud-grpc-client-consul/build.gradle

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@ plugins {
22
id 'org.springframework.boot'
33
}
44

5+
def discoveryProvider = project.findProperty('discovery') ?: 'consul';
6+
57
dependencies {
6-
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
8+
switch (discoveryProvider) {
9+
case "nacos": {
10+
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
11+
break;
12+
}
13+
case "consul": {
14+
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
15+
break;
16+
}
17+
}
18+
19+
implementation project(':grpc-client-spring-boot-starter') // Replace with actual dependency "net.devh:grpc-client-spring-boot-starter:${springBootGrpcVersion}"
20+
implementation project(':examples:grpc-lib') // Replace with your grpc interface spec
21+
22+
// For demonstration only
723
implementation 'org.springframework.boot:spring-boot-starter-actuator'
824
implementation 'org.springframework.boot:spring-boot-starter-web'
25+
}
926

10-
implementation project(':examples:grpc-lib')
11-
implementation project(':grpc-client-spring-boot-starter') // replace to implementation "net.devh:grpc-server-spring-boot-starter:${springBootGrpcVersion}"
12-
13-
testImplementation('org.springframework.boot:spring-boot-starter-test'){
14-
exclude module: 'junit-vintage-engine'
15-
exclude module: 'junit'
16-
}
27+
bootRun {
28+
args = ["--spring.profiles.active=" + discoveryProvider]
1729
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@
2121
import org.springframework.boot.autoconfigure.SpringBootApplication;
2222
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
2323

24-
/**
25-
* @author zhaochunlin ([email protected])
26-
* @since 2021/1/9
27-
*/
2824
@EnableDiscoveryClient
2925
@SpringBootApplication
30-
public class ConsulClientApplication {
26+
public class CloudClientApplication {
3127

32-
public static void main(String[] args) {
33-
SpringApplication.run(ConsulClientApplication.class, args);
28+
public static void main(final String... args) {
29+
SpringApplication.run(CloudClientApplication.class, args);
3430
}
3531

3632
}

examples/cloud-grpc-client-consul/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import org.springframework.web.bind.annotation.RestController;
2424

2525
/**
26-
* @author zhaochunlin ([email protected])
27-
* @since 2021/1/9
26+
* Optional demo controller making the grpc service accessible via browser.
2827
*/
2928
@RestController
3029
public class GrpcClientController {
@@ -33,8 +32,8 @@ public class GrpcClientController {
3332
private GrpcClientService grpcClientService;
3433

3534
@RequestMapping("/")
36-
public String printMessage(@RequestParam(defaultValue = "Michael") String name) {
37-
return grpcClientService.sendMessage(name);
35+
public String printMessage(@RequestParam(defaultValue = "Michael") final String name) {
36+
return this.grpcClientService.sendMessage(name);
3837
}
3938

4039
}

examples/cloud-grpc-client-consul/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@
2626
import net.devh.boot.grpc.examples.lib.HelloRequest;
2727
import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub;
2828

29-
/**
30-
* @author zhaochunlin ([email protected])
31-
* @since 2021/1/9
32-
*/
3329
@Service
3430
@Slf4j
3531
public class GrpcClientService {
3632

37-
@GrpcClient("cloud-grpc-server-consul")
33+
@GrpcClient("cloud-grpc-server")
3834
private SimpleBlockingStub simpleStub;
3935

4036
public String sendMessage(final String name) {
4137
try {
4238
final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
4339
return response.getMessage();
4440
} catch (final StatusRuntimeException e) {
45-
log.error(e.getMessage());
41+
log.error("Request failed", e);
4642
return "FAILED with " + e.getStatus().getCode();
4743
}
4844
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring:
2+
cloud:
3+
consul:
4+
discovery:
5+
register: false
6+
# hostname: localhost
7+
# port: 8500
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
cloud:
3+
nacos:
4+
discovery:
5+
register-enabled: false
6+
# server-addr: localhost:8848
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
server:
2-
port: 8082
2+
port: 8080
33
spring:
44
application:
5-
name: cloud-grpc-client-consul
6-
cloud:
7-
consul:
8-
discovery:
9-
registry: false
10-
port: 8500
11-
hostname: 127.0.0.1
5+
name: cloud-grpc-client
126
grpc:
137
client:
14-
cloud-grpc-server-consul:
8+
cloud-grpc-server:
9+
address: 'discovery:///cloud-grpc-server'
1510
enableKeepAlive: true
1611
keepAliveWithoutCalls: true
1712
negotiationType: plaintext

examples/cloud-grpc-client-nacos/build.gradle

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/cloud-grpc-client-nacos/src/main/java/net/devh/boot/grpc/examples/cloud/client/CloudGrpcNacosClientApplication.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/cloud-grpc-client-nacos/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientController.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)