Skip to content

Commit ed6a0e5

Browse files
authored
Merge pull request #539 from yidongnan/examples/condense-cloud-discovery
Condense/Simplify examples
2 parents f0302bd + 34941e6 commit ed6a0e5

File tree

64 files changed

+278
-1585
lines changed

Some content is hidden

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

64 files changed

+278
-1585
lines changed

examples/README.md

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# gRPC spring boot starter Examples
22

3-
## Local Mode
3+
## Local Setup
44

55
1. Try the local-grpc-server example first run:
66

@@ -16,28 +16,41 @@
1616

1717
3. Visit http://localhost:8080/ to see the result.
1818

19-
## Cloud Mode
19+
## Cloud Discovery Setup
2020

21-
1. Try the cloud-eureka-server example first run:
21+
1. Choose a cloud discovery implementation:
22+
23+
- `consul`
24+
- `eureka`
25+
- `nacos`
26+
27+
> **Note:** In your actual application you are free to choose any cloud discovery implementation,
28+
> but only the above and `zookeeper` will automatically register the server at the discovery service.
29+
> So you might have to write a few extra lines in your server application.
30+
> Generic registration support is planned for a future release.
31+
> No additional configuration is required for clients.
32+
33+
2. Start the discovery server (only the chosen one):
2234

2335
````sh
36+
# Consul
37+
docker run --name=consul -p 8500:8500 consul
38+
# Eureka
2439
./gradlew :example:cloud-eureka-server:bootRun
40+
# Nacos
41+
docker run --env MODE=standalone --name nacos -d --rm -p 8848:8848 nacos/nacos-server
2542
````
2643

27-
2. Run zipkin-server
28-
29-
https://github.com/openzipkin/zipkin#quick-start
30-
31-
3. In a different terminal window run:
44+
3. Insert the selected implementation and start the server application (in a new terminal window):
3245

3346
````sh
34-
./gradlew :example:cloud-grpc-server:bootRun
47+
./gradlew -Pdiscovery=$discovery :example:cloud-grpc-server:bootRun
3548
````
3649

37-
4. In a different terminal window run:
50+
4. Insert the selected implementation and start the client application (in a new terminal window):
3851

3952
````sh
40-
./gradlew :example:cloud-grpc-client:bootRun
53+
./gradlew -Pdiscovery=$discovery :example:cloud-grpc-client:bootRun
4154
````
4255

4356
5. Visit http://localhost:8080/ to see the result.
@@ -59,71 +72,3 @@
5972
3. Visit http://localhost:8080/ to see the result.
6073

6174
*You can configure the client's username in the application.yml.*
62-
63-
## With Bearer auth security
64-
65-
1. Try the security-grpc-bearerAuth-server example first run:
66-
67-
````sh
68-
./gradlew :example:security-grpc-bearerAuth-server:bootRun
69-
````
70-
71-
2. In a different terminal window run:
72-
73-
````sh
74-
./gradlew :example:security-grpc-bearerAuth-client:bootRun
75-
````
76-
77-
3. Visit http://localhost:8080/ to see the result.
78-
79-
This will not run out of the box since one needs to set up an identity provider service, like
80-
for example Keycloak. Keycloak provides an endpoint to retrieve the necessary configuration (Public RSA key, etc).
81-
The URI to this endpoint needs to be provided in the server's `SecurityConfiguration.java` in the `jwtDecoder()` method.
82-
83-
Additionally you will need to obtain a valid access token from the Keycloak server. This token has to be provided in
84-
the client's `SecurityConfiguration.java`
85-
86-
To obtain an access token you can use Postman and perform an HTTP POST call to:
87-
`http://127.0.0.1:8080/auth/realms/YOURREALM/protocol/openid-connect/token`
88-
with basic authentication. Username and password are the client id and secret of the client you configured in the
89-
Keycloak admin panel (http://127.0.0.1:8080/).
90-
91-
*You can configure the bearer token in the `SecurityConfiguration.java`*
92-
93-
**Advice for testing/development:**
94-
95-
When testing/developing it is not always possible to have an IDP service ready. In that case you can add the following
96-
line:
97-
98-
````java
99-
providers.add(anonymousAuthenticationProvider());
100-
````
101-
102-
right above (your actual authentication providers)
103-
104-
````java
105-
providers.add(jwtAuthenticationProvider());
106-
````
107-
108-
in the `authenticationManager()` bean method of the server's `SecurityConfiguration.java`
109-
This will of course require an appropriate Bean like such:
110-
111-
````java
112-
@Bean
113-
AnonymousAuthenticationProvider anonymousAuthenticationProvider() {
114-
return new AnonymousAuthenticationProvider("dev");
115-
}
116-
````
117-
118-
and in the authenticationReader() Bean replace the return with:
119-
120-
````java
121-
return new AnonymousAuthenticationReader("dev", "developer", AuthorityUtils.createAuthorityList("ROLE_TEST"));
122-
````
123-
124-
You can add/change the roles there to your liking.
125-
126-
Overall what happens here is that the BearerAuthenticationReader is replaced by AnonymousAuthenticationReader, which
127-
ignores the Bearer token and creates an AnonymousAuthenticationToken which is processed by the
128-
`AnonymousAuthenticationProvider`. This way you can temporarily bypass the bearer token auth.
129-
You might want to toggle this behavior with a `dev` or `debug` property.

examples/cloud-eureka-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/EurekaServerApplication.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
import org.springframework.boot.autoconfigure.SpringBootApplication;
2222
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
2323

24+
/**
25+
* Eureka cloud discovery server application.
26+
*/
2427
@EnableEurekaServer
2528
@SpringBootApplication
2629
public class EurekaServerApplication {
2730

28-
public static void main(String[] args) {
31+
/**
32+
* Starts the Eureka cloud discovery server application.
33+
*
34+
* @param args The arguments to pass to the application.
35+
*/
36+
public static void main(final String... args) {
2937
SpringApplication.run(EurekaServerApplication.class, args);
3038
}
3139

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

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

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

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

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

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

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

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

examples/cloud-grpc-client-consul/src/main/resources/application.yml

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

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/GrpcClientController.java

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

0 commit comments

Comments
 (0)