Skip to content

Commit b6cec89

Browse files
committed
添加consul注册中心 examples
1 parent bff6c1a commit b6cec89

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
7+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
8+
implementation 'org.springframework.boot:spring-boot-starter-web'
9+
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+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
23+
24+
@EnableDiscoveryClient
25+
@SpringBootApplication
26+
public class ConsulClientApplication {
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(ConsulClientApplication.class, args);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
/**
26+
* @author Michael ([email protected])
27+
* @since 2016/12/4
28+
*/
29+
@RestController
30+
public class GrpcClientController {
31+
32+
@Autowired
33+
private GrpcClientService grpcClientService;
34+
35+
@RequestMapping("/")
36+
public String printMessage(@RequestParam(defaultValue = "Michael") String name) {
37+
return grpcClientService.sendMessage(name);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import io.grpc.StatusRuntimeException;
21+
import lombok.extern.slf4j.Slf4j;
22+
import net.devh.boot.grpc.client.inject.GrpcClient;
23+
import net.devh.boot.grpc.examples.lib.HelloReply;
24+
import net.devh.boot.grpc.examples.lib.HelloRequest;
25+
import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub;
26+
import org.springframework.stereotype.Service;
27+
28+
/**
29+
* @author Michael ([email protected])
30+
* @since 2016/11/8
31+
*/
32+
@Service
33+
@Slf4j
34+
public class GrpcClientService {
35+
36+
@GrpcClient("cloud-grpc-server-consul")
37+
private SimpleBlockingStub simpleStub;
38+
39+
public String sendMessage(final String name) {
40+
try {
41+
final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
42+
return response.getMessage();
43+
} catch (final StatusRuntimeException e) {
44+
log.error(e.getMessage());
45+
return "FAILED with " + e.getStatus().getCode();
46+
}
47+
}
48+
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server:
2+
port: 8082
3+
spring:
4+
application:
5+
name: cloud-grpc-consul-client
6+
cloud:
7+
consul:
8+
discovery:
9+
port: 8500
10+
hostname: 127.0.0.1
11+
12+
#grpc:
13+
# client:
14+
# grpc-server:
15+
# host:
16+
# - ${GRPC-SERVER-HOST:127.0.0.1}
17+
# port:
18+
# - 9090
19+
# enableKeepAlive: true
20+
# keepAliveWithoutCalls: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
7+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
8+
implementation 'org.springframework.boot:spring-boot-starter-web'
9+
10+
implementation project(':examples:grpc-lib')
11+
implementation project(':grpc-server-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+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.server;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
23+
24+
@EnableDiscoveryClient
25+
@SpringBootApplication
26+
public class ConsulServerApplication {
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(ConsulServerApplication.class, args);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.server;
19+
20+
import io.grpc.stub.StreamObserver;
21+
import net.devh.boot.grpc.examples.lib.HelloReply;
22+
import net.devh.boot.grpc.examples.lib.HelloRequest;
23+
import net.devh.boot.grpc.examples.lib.SimpleGrpc;
24+
import net.devh.boot.grpc.server.service.GrpcService;
25+
26+
/**
27+
* @author Michael ([email protected])
28+
* @since 2016/11/8
29+
*/
30+
31+
@GrpcService
32+
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
33+
34+
@Override
35+
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
36+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
37+
responseObserver.onNext(reply);
38+
responseObserver.onCompleted();
39+
}
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server:
2+
port: 8081
3+
4+
spring:
5+
application:
6+
name: cloud-grpc-server-consul
7+
cloud:
8+
consul:
9+
discovery:
10+
port: 8500
11+
hostname: 127.0.0.1
12+
13+
grpc:
14+
server:
15+
port: 9999
16+
# enabled: true
17+
# inProcessServerName: user

settings.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ include "examples:security-grpc-client"
1919
include "examples:security-grpc-server"
2020
include "examples:security-grpc-bearerAuth-server"
2121
include "examples:security-grpc-bearerAuth-client"
22+
include 'cloud-consul-server'
23+
include 'cloud-consul-server'
24+
include 'examples:cloud-consul-server'
25+
findProject(':examples:cloud-consul-server')?.name = 'cloud-consul-server'
26+
include 'examples:cloud-grpc-server-consul'
27+
findProject(':examples:cloud-grpc-server-consul')?.name = 'cloud-grpc-server-consul'
28+
include 'examples:cloud-grpc-client-consul'
29+
findProject(':examples:cloud-grpc-client-consul')?.name = 'cloud-grpc-client-consul'
30+

0 commit comments

Comments
 (0)