Skip to content

Latest commit

 

History

History
152 lines (97 loc) · 4.72 KB

demo.adoc

File metadata and controls

152 lines (97 loc) · 4.72 KB

Java Microservices with Spring Boot and Spring Cloud

The brackets at the end of each step indicate the alias’s or IntelliJ Live Templates to use. You can find the template definitions at mraible/idea-live-templates.

Create Eureka Server, Car Service, and API Gateway

  1. Create a Eureka Server

    http https://start.spring.io/starter.zip javaVersion==11 \
      artifactId==discovery-service name==eureka-service \
      dependencies==cloud-eureka-server baseDir==discovery-service | tar -xzvf -
  2. Create a Car Service

    http https://start.spring.io/starter.zip \
      artifactId==car-service name==car-service baseDir==car-service \
      dependencies==actuator,cloud-eureka,data-jpa,h2,data-rest,web,devtools,lombok | tar -xzvf -
  3. Create an API Gateway

    http https://start.spring.io/starter.zip \
      artifactId==api-gateway name==api-gateway baseDir==api-gateway \
      dependencies==cloud-eureka,cloud-feign,data-rest,web,cloud-hystrix,lombok | tar -xzvf -
  4. Install Java 11 with SDKMAN! https://sdkman.io/

    sdk list java
    sdk install java 11.0.2-open
    sdk default java 11.0.2-open
  5. Add @EnableEurekaServer and properties to set port and turn off discovery

    server.port=8761
    eureka.client.register-with-eureka=false
  6. Add @EnableDiscoveryClient to main classes in car-service and api-gateway

  7. Configure car-service to run on 8090 and set its name

    server.port=8090
    spring.application.name=car-service
  8. Add an application name to the api-gateway project

    spring.application.name=api-gateway
  9. Create an API with Spring Boot and Spring Data [boot-entity-lombok, boot-repo, boot-data]

  10. Show Lombok plugin is installed in IntelliJ

  11. Configure gateway to enable resilient server-to-server communication

    @EnableFeignClients
    @EnableCircuitBreaker
  12. Create a Car class with @Data

  13. Create a feign client and cool car controller [feign-client, cool-car-adapter]

  14. Add @HystrixCommand for failover [hystrix-fallback]

  15. Start all servers, view Eureka server, and https://localhost:8080/cool-cars endpoint

Secure Java Microservices with OAuth 2.0 and OIDC

  1. Add Okta Spring Boot starter to api-gateway and car-service [okta-maven-boot]

    <dependency>
      <groupId>com.okta.spring</groupId>
      <artifactId>okta-spring-boot-starter</artifactId>
      <version>1.4.0</version>
    </dependency>
  2. Create a web app on Okta, use http://localhost:8080/login/oauth2/code/okta for redirect URI

  3. Populate Okta properties in application.properties

    okta.oauth2.issuer=$issuer
    okta.oauth2.client-id=$clientId
    okta.oauth2.client-secret=$clientSecret
  4. Create a SecurityConfiguration class, and enable OAuth Login and a Resource Server [ss-resource-config]

    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http
              .authorizeRequests().anyRequest().authenticated()
                  .and()
              .oauth2Login()
                  .and()
              .oauth2ResourceServer().jwt();
          // @formatter:on
      }
    }
  5. Enable Resource Server in the car-service application [ss-resource-config]

  6. Create UserFeignClientInterceptor to add Authorization header in api-gateway [feign-interceptor]

  7. Register interceptor as a bean [feign-bean]

    @Bean
    public RequestInterceptor getUserFeignClientInterceptor(OAuth2AuthorizedClientService clientService) {
        return new UserFeignClientInterceptor(clientService);
    }
  8. Make Feign Spring Security-aware

    feign.hystrix.enabled=true
    hystrix.shareSecurityContext=true
  9. Restart all apps and show with security enabled

Use Netflix Zuul for Routing

  1. Add Zuul as a dependency and @EnableZuulProxy

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
  2. Create an AuthorizationHeaderFilter to pass the access token to proxied routes [zuul-auth-header]

  3. Register AuthorizationHeaderFilter filter as a bean [zuul-bean]

    @Bean
    public AuthorizationHeaderFilter authHeaderFilter(OAuth2AuthorizedClientService clientService) {
        return new AuthorizationHeaderFilter(clientService);
    }
  4. Add Zuul routes for /cars and /home [zuul-routes]

  5. Add HomeController to the car-service [zuul-home]

  6. Restart and confirm http://localhost:8080/cars and http://localhost:8080/home routes work

  7. Fin! 🏁