|
1 | 1 |
|
2 | 2 | # Java Microservices with Spring Boot and Spring Cloud 🍃☁️ |
3 | 3 |
|
4 | | -This repository contains examples of how to build a microservices architecture with Spring Boot 2.1, Spring Cloud Greenwich, Netflix Eureka, and Netflix Zuul. |
| 4 | +This repository contains examples of how to build a Java microservices architecture with Spring Boot 2.1, Spring Cloud Greenwich, and Netflix Eureka. |
5 | 5 |
|
6 | | -This repository has two examples in it. One is a bare-bones microservices architecture with Spring Boot, Spring Cloud, Eureka Server, and Zuul. The second is one that's built with JHipster and configured centrally with Spring Cloud Config. |
| 6 | +This repository has three examples in it. The first is a bare-bones microservices architecture with Spring Boot, Spring Cloud, Eureka Server, and Zuul. The second is one that's built with JHipster and configured centrally with Spring Cloud Config. The third uses Spring Cloud Gateway and Spring WebFlux to show reactive microservices. |
7 | 7 |
|
8 | 8 | We think you'll enjoy them both! |
9 | 9 |
|
10 | 10 | * See [Java Microservices with Spring Boot and Spring Cloud](https://developer.okta.com/blog/2019/05/22/java-microservices-spring-boot-spring-cloud) for an overview of the first example. |
11 | 11 | * Read [Java Microservices with Spring Cloud Config and JHipster](https://developer.okta.com/blog/2019/05/23/java-microservices-spring-cloud-config) to learn about microservices with JHipster. |
| 12 | +* Refer to [Secure Reactive Microservices with Spring Cloud Gateway](https://developer.okta.com/blog/2019/09/28/reactive-microservices-spring-cloud-gateway) to learn about Spring Cloud Gateway and reactive microservices. |
12 | 13 |
|
13 | | -**Prerequisites:** [Java 11](https://sdkman.io/sdks#java). |
| 14 | +**Prerequisites:** [Java 11](https://sdkman.io/sdks#java) and an internet connection. |
14 | 15 |
|
15 | 16 | * [Spring Boot + Spring Cloud Example](#spring-boot--spring-cloud-example) |
16 | 17 | * [JHipster + Spring Cloud Config Example](#jhipster--spring-cloud-config-example) |
| 18 | +* [Spring Cloud Gateway Example](#spring-cloud-gateway-example) |
17 | 19 | * [Links](#links) |
18 | 20 | * [Help](#help) |
19 | 21 | * [License](#license) |
@@ -47,7 +49,7 @@ okta.oauth2.client-secret=$clientSecret |
47 | 49 |
|
48 | 50 | Then, run all the projects with `./mvnw` in separate terminal windows. You should be able to navigate to `http://localhost:8761` and see the apps have been registered with Eureka. |
49 | 51 |
|
50 | | -Then, navigate to `http://localhost:8080/cool-bars` in your browser, log in with Okta, and see the resulting JSON. |
| 52 | +Then, navigate to `http://localhost:8080/cool-cars` in your browser, log in with Okta, and see the resulting JSON. |
51 | 53 |
|
52 | 54 | ## JHipster + Spring Cloud Config Example |
53 | 55 |
|
@@ -119,6 +121,57 @@ Create a `ROLE_ADMIN` group (**Users** > **Groups** > **Add Group**) and add you |
119 | 121 |
|
120 | 122 | Now when you hit `http://localhost:8761` or `http://localhost:8080`, you should be able to log in with Okta! |
121 | 123 |
|
| 124 | +## Spring Cloud Gateway Example |
| 125 | + |
| 126 | +To install this example, run the following commands: |
| 127 | + |
| 128 | +```bash |
| 129 | +git clone https://github.com/oktadeveloper/java-microservices-examples.git |
| 130 | +cd java-microservices-examples/spring-cloud-gateway |
| 131 | +``` |
| 132 | + |
| 133 | +The `api-gateway` and `car-service` projects are already pre-configured to be locked down with OAuth 2.0 and Okta. That means if you try to run them, you won't be able to login until you create an account, and an application in it. |
| 134 | + |
| 135 | +If you already have an Okta account, see the **Create a Web Application in Okta** section below. Otherwise, we created a Maven plugin that configures a free Okta developer account + an OIDC app (in under a minute!). |
| 136 | + |
| 137 | +To use it, add the following plugin repository to the gateway project's `pom.xml`: |
| 138 | + |
| 139 | +[source,xml] |
| 140 | +---- |
| 141 | +<pluginRepositories> |
| 142 | + <pluginRepository> |
| 143 | + <id>ossrh</id> |
| 144 | + <releases><enabled>false</enabled></releases> |
| 145 | + <snapshots><enabled>true</enabled></snapshots> |
| 146 | + <url>https://oss.sonatype.org/content/repositories/snapshots</url> |
| 147 | + </pluginRepository> |
| 148 | +</pluginRepositories> |
| 149 | +---- |
| 150 | + |
| 151 | +Then run `./mvnw com.okta:okta-maven-plugin:setup` to create an account and configure the gateway to work with Okta. |
| 152 | + |
| 153 | +Copy the `okta.*` properties from the gateway's `src/main/resources/application.properties` to the same file in the `car-service` project. |
| 154 | + |
| 155 | +Then, run all the projects with `./mvnw` in separate terminal windows. You should be able to navigate to `http://localhost:8761` and see the apps have been registered with Eureka. |
| 156 | + |
| 157 | +Then, navigate to `http://localhost:8080/cars` in your browser, log in with Okta, and see the resulting JSON. |
| 158 | + |
| 159 | +### Create a Web Application in Okta |
| 160 | + |
| 161 | +Log in to your Okta Developer account (or [sign up](https://developer.okta.com/signup/) if you don't have an account). |
| 162 | + |
| 163 | +1. From the **Applications** page, choose **Add Application**. |
| 164 | +2. On the Create New Application page, select **Web**. |
| 165 | +3. Give your app a memorable name, add `http://localhost:8080/login/oauth2/code/okta` as a Login redirect URI and click **Done**. |
| 166 | + |
| 167 | +Copy the issuer (found under **API** > **Authorization Servers**), client ID, and client secret into the `application.properties` of the `api-gateway` and `car-service` projects. |
| 168 | + |
| 169 | +```properties |
| 170 | +okta.oauth2.issuer=https://{yourOktaDomain}/oauth2/default |
| 171 | +okta.oauth2.client-id=$clientId |
| 172 | +okta.oauth2.client-secret=$clientSecret |
| 173 | +``` |
| 174 | + |
122 | 175 | ## Links |
123 | 176 |
|
124 | 177 | These examples uses the following open source libraries: |
|
0 commit comments