MicroServices with Spring: Difference between revisions

From My Limbic Wiki
Line 6: Line 6:
{| class="wikitable"
{| class="wikitable"
|-
|-
! scope="col"| Fichiers
! scope="col"| /
! scope="col"| Server
! scope="col"| Main
! scope="col"| Zuul
! scope="col"| Pom.xml
! scope="col"| Image
! scope="col"| Properties
|-
|-
! scope="row"| Main
! scope="row"| Server
|  
|  
* com.microservices.server.EurekaServerApplication.java
* com.microservices.server.EurekaServerApplication.java
Line 17: Line 17:
  '''@EnableEurekaServer''' // Enable eureka server
  '''@EnableEurekaServer''' // Enable eureka server
|
|
* Dependencies
spring-'''cloud'''-starter-'''netflix-eureka-server'''
spring-boot : web, test & devtools
com.sun.xml.bind: jaxb-'''core,api,impl'''
* dependencyManagement
org.springframework.'''cloud''': spring-'''cloud-dependencies'''
* Repositories
https://repo.spring.io/'''milestone'''
|
spring.application.'''name'''=eureka-server
server.'''port'''=8761
eureka.'''client.register-with-eureka=false'''
eureka.'''client.fetch-registry=false'''
|-
! scope="row"| Zuul
|
* com.microservices.zuul.EurekaZuulApplication.java
* com.microservices.zuul.EurekaZuulApplication.java
  @SpringBootApplication
  @SpringBootApplication
Line 36: Line 52:
  }
  }
|
|
* com.microservices.image.'''EurekaImageApplication.java'''
server.'''port'''=8762
  @SpringBootApplication
  spring.application.'''name'''=zuul-server
'''@EnableEurekaClient'''
eureka.'''client.service-url.default-zone'''=http://localhost:8761/eureka/
* com.microservices.image.'''controllers.ImageController.java'''
  #zuul.'''prefix'''=/api
  '''@RestController'''
* Disable accessing services using service name (i.e. gallery-service).
'''@RequestMapping("/")'''
  zuul.'''ignored-services=*'''
  '''@Autowired'''
* Map paths to services
    private Environment env;
zuul.routes.'''gallery-service.path'''=/gallery/**
* com.microservices.image.'''entities.Image.java'''
  zuul.routes.'''gallery-service.service-id'''=gallery-service
|-
zuul.routes.'''auth-service.path'''=/auth/**
! scope="row"| Pom.xml
  zuul.routes.'''auth-service.service-id'''=AUTH-SERVICE$
|
* So, if request is "/gallery/view/1", gallery service will get "/view/1".
* Dependencies
  zuul.routes.'''auth-service.strip-prefix'''=false
  spring-'''cloud'''-starter-'''netflix-eureka-server'''
  zuul.routes.'''auth-service.sensitive-headers'''=Cookie,Set-Cookie  #Exclude authorization from sensitive headers
  spring-boot : web, test & devtools
com.sun.xml.bind: jaxb-'''core,api,impl'''
* dependencyManagement
  org.springframework.'''cloud''': spring-'''cloud-dependencies'''
* Repositories
  https://repo.spring.io/'''milestone'''
|
* Dependencies
* Dependencies
  spring-'''cloud'''-starter-'''netflix-eureka-client'''
  spring-'''cloud'''-starter-'''netflix-eureka-client'''
Line 68: Line 77:
* Repositories  
* Repositories  
  https://repo.spring.io/'''milestone'''
  https://repo.spring.io/'''milestone'''
|
|-
! scope="row"| Image
|
* com.microservices.image.'''EurekaImageApplication.java'''
@SpringBootApplication
'''@EnableEurekaClient'''
* com.microservices.image.'''controllers.ImageController.java'''
'''@RestController'''
'''@RequestMapping("/")'''
'''@Autowired'''
    private Environment env;
* com.microservices.image.'''entities.Image.java'''
|
|
* Dependencies
* Dependencies
Line 77: Line 99:
* Repositories  
* Repositories  
  https://repo.spring.io/'''milestone'''
  https://repo.spring.io/'''milestone'''
|-
! scope="row"| Properties
|
spring.application.'''name'''=eureka-server
server.'''port'''=8761
eureka.'''client.register-with-eureka=false'''
eureka.'''client.fetch-registry=false'''
|
server.'''port'''=8762
spring.application.'''name'''=zuul-server
eureka.'''client.service-url.default-zone'''=http://localhost:8761/eureka/
#zuul.'''prefix'''=/api
* Disable accessing services using service name (i.e. gallery-service).
zuul.'''ignored-services=*'''
* Map paths to services
zuul.routes.'''gallery-service.path'''=/gallery/**
zuul.routes.'''gallery-service.service-id'''=gallery-service
zuul.routes.'''auth-service.path'''=/auth/**
zuul.routes.'''auth-service.service-id'''=AUTH-SERVICE$
* So, if request is "/gallery/view/1", gallery service will get "/view/1".
zuul.routes.'''auth-service.strip-prefix'''=false
zuul.routes.'''auth-service.sensitive-headers'''=Cookie,Set-Cookie  #Exclude authorization from sensitive headers
|
|
   spring.application.'''name'''=image-service
   spring.application.'''name'''=image-service
   server.'''port'''=8200
   server.'''port'''=8200
   eureka.client.service-'''url.default'''-zone=http://localhost:8761/eureka
   eureka.client.service-'''url.default'''-zone=http://localhost:8761/eureka
|-
! scope="row"| Gallery
|}
|
 
|
{| class="wikitable"
|
|-
! scope="row"| Common
! scope="col"| Fichiers
|
! scope="col"| Common
! scope="col"| Auth
|-
! scope="row"| Main
|  
* com.microservices.common
* com.microservices.common
  @SpringBootApplication
  @SpringBootApplication
Line 126: Line 121:
  '''@Value("${security.jwt.secret:JwtSecretKey}")'''
  '''@Value("${security.jwt.secret:JwtSecretKey}")'''
* com.microservices.common.'''security'''.UserRole.java
* com.microservices.common.'''security'''.UserRole.java
|
* Dependencies
spring-cloud-starter-'''netflix-eureka-client'''
'''org.projectlombok''': '''lombok'''
spring-boot : web, test & devtools
* dependencyManagement
org.springframework.'''cloud''': spring-'''cloud-dependencies'''
* Repositories
https://repo.spring.io/'''milestone'''
|
spring.application.'''name'''=common-service
server.'''port'''=9200
eureka.'''client.service-url.default-zone'''=http://localhost:8761/eureka
! scope="row"| Auth
|
|
* com.microservices.'''auth'''.SpringEurekaAuthApp.java
* com.microservices.'''auth'''.SpringEurekaAuthApp.java
Line 152: Line 161:
  '''@Override'''
  '''@Override'''
   public '''UserDetails'''
   public '''UserDetails'''
|-
! scope="row"| Pom.xml
|
* Dependencies
spring-cloud-starter-'''netflix-eureka-client'''
'''org.projectlombok''': '''lombok'''
spring-boot : web, test & devtools
* dependencyManagement
org.springframework.'''cloud''': spring-'''cloud-dependencies'''
* Repositories
https://repo.spring.io/'''milestone'''
|
|
* Dependencies
* Dependencies
Line 174: Line 172:
* Repositories  
* Repositories  
  https://repo.spring.io/'''milestone'''
  https://repo.spring.io/'''milestone'''
|-
! scope="row"| Properties
|
spring.application.'''name'''=common-service
server.'''port'''=9200
eureka.'''client.service-url.default-zone'''=http://localhost:8761/eureka
|
|
  spring.application.'''name'''=auth-service
  spring.application.'''name'''=auth-service

Revision as of 21:35, 21 September 2019

Vulgarization

Specificities in a Spring aborescence

Eureka

Is the registration service, all services will connect to the same adress: http://localhost:9000 for example

/ Main Pom.xml Properties
Server
  • com.microservices.server.EurekaServerApplication.java
@SpringBootApplication
@EnableEurekaServer // Enable eureka server
  • Dependencies
spring-cloud-starter-netflix-eureka-server
spring-boot : web, test & devtools
com.sun.xml.bind: jaxb-core,api,impl
  • dependencyManagement
org.springframework.cloud: spring-cloud-dependencies
  • Repositories
https://repo.spring.io/milestone
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Zuul
  • com.microservices.zuul.EurekaZuulApplication.java
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
  • com.microservices.zuul.security.JwtAuthenticationEntryPoint.java
@Component
  • com.microservices.zuul.security.JwtTokenAuthenticationFilter.java
//import com.eureka.common.security.JwtConfig;
private final JwtConfig jwtConfig;
  • com.microservices.zuul.security.SecurityTokenConfig.java
@EnableWebSecurity
  @Autowired
  //import com.eureka.common.security.JwtConfig;
  private JwtConfig jwtConfig;
@Bean
public JwtConfig jwtConfig() {
    return new JwtConfig();
}
server.port=8762
spring.application.name=zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
#zuul.prefix=/api
  • Disable accessing services using service name (i.e. gallery-service).
zuul.ignored-services=*
  • Map paths to services
zuul.routes.gallery-service.path=/gallery/**
zuul.routes.gallery-service.service-id=gallery-service
zuul.routes.auth-service.path=/auth/**
zuul.routes.auth-service.service-id=AUTH-SERVICE$
  • So, if request is "/gallery/view/1", gallery service will get "/view/1".
zuul.routes.auth-service.strip-prefix=false
zuul.routes.auth-service.sensitive-headers=Cookie,Set-Cookie  #Exclude authorization from sensitive headers
  • Dependencies
spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-netflix-zuul
o.jsonwebtoken: jjwt
com.microservices.common: SpringEurekaCommon
spring-boot : web, test, devtools & security 
com.sun.xml.bind: jaxb-core,api,impl
  • dependencyManagement
org.springframework.cloud: spring-cloud-dependencies
  • Repositories
https://repo.spring.io/milestone
Image
  • com.microservices.image.EurekaImageApplication.java
@SpringBootApplication
@EnableEurekaClient
  • com.microservices.image.controllers.ImageController.java
@RestController
@RequestMapping("/")
@Autowired
   private Environment env;
  • com.microservices.image.entities.Image.java
  • Dependencies
spring-cloud-starter-netflix-eureka-client
spring-boot : web, test & devtools
org.springframework.cloud: spring-cloud-starter-sleuth
  • dependencyManagement
org.springframework.cloud: spring-cloud-dependencies
  • Repositories
https://repo.spring.io/milestone
 spring.application.name=image-service
 server.port=8200
 eureka.client.service-url.default-zone=http://localhost:8761/eureka
Gallery Common
  • com.microservices.common
@SpringBootApplication
@EnableEurekaClient
  • com.microservices.common.security.JwtConfig.java
@Getter
@ToString	
@Value("${security.jwt.uri:/auth/**}")
@Value("${security.jwt.header:Authorization}")
@Value("${security.jwt.prefix:Bearer }")
@Value("${security.jwt.expiration:#{24*60*60}}")
@Value("${security.jwt.secret:JwtSecretKey}")
  • com.microservices.common.security.UserRole.java
  • Dependencies
spring-cloud-starter-netflix-eureka-client
org.projectlombok: lombok
spring-boot : web, test & devtools
  • dependencyManagement
org.springframework.cloud: spring-cloud-dependencies
  • Repositories
https://repo.spring.io/milestone
spring.application.name=common-service
server.port=9200
eureka.client.service-url.default-zone=http://localhost:8761/eureka
Auth
  • com.microservices.auth.SpringEurekaAuthApp.java
@SpringBootApplication
@EnableEurekaClient
  • com.microservices.common.security.SecurityCredentialsConfig.java
@EnableWebSecurity
  @Autowired
 private UserDetailsService userDetailsService;
  @Autowired
 //import com.eureka.common.security.JwtConfig;
 private JwtConfig jwtConfig;
@Bean
public JwtConfig jwtConfig() {
  return new JwtConfig();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
  return new BCryptPasswordEncoder();
}
  • com.microservices.common.security.JwtUsernameAndPasswordAuthenticationFilter.java
  • com.microservices.common.security.UserDetailsServiceImpl.java
@Service
@Autowired
  private BCryptPasswordEncoder encoder;
@Override
  public UserDetails
  • Dependencies
spring-cloud-starter-netflix-eureka-client
spring-boot : web, test, devtools & Security
io.jsonwebtoken: jjwt
com.microservices.common: SpringEurekaCommon
com.sun.xml.bind: jaxb-core,api,impl
  • dependencyManagement
org.springframework.cloud: spring-cloud-dependencies
  • Repositories
https://repo.spring.io/milestone
spring.application.name=auth-service
server.port=9100
eureka.client.service-url.default-zone=http://localhost:8761/eureka