Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Last active August 24, 2022 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijay-shrestha/fd67375d5c771797765240611ca89cd6 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/fd67375d5c771797765240611ca89cd6 to your computer and use it in GitHub Desktop.
Microservices Setup

Eureka Server

YAML

server:
  port: 9091

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:9090/eureka

Maven dependency

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

Eureka Client

YAML

server:
  port: 8081
spring:
  application:
    name: ORDER-SERVICE
  datasource:
    url: jdbc:h2:mem:testdb
  h2:
    console:
      enabled: true

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:9090/eureka

Maven dependency

<spring-cloud.version>2021.0.3</spring-cloud.version>

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Api Gateway

YAML configuration

server:
  port: 8080
spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      routes:
        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/orders/**
        - id: payment-service
          uri: lb://PAYMENT-SERVICE
          predicates:
            - Path=/payments/**
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:9090/eureka
  instance:
    hostname: localhost

MAVEN

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Hystrix Gateway

YAML configuration

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

MAVEN

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.2.5.RELEASE</version>
</dependency>

Code snippet

@Override
@HystrixCommand(fallbackMethod = "orderFallBack")
public OrderResponse findOrderById(Integer orderId) {
        Optional<Order> order = orderRepository.findById(orderId);
        if (!order.isPresent()) {
            throw new OrderNotFoundException("Order Not Found for provided id");
        }
        PaymentResponse paymentResponse = restTemplate.getForObject("http://PAYMENT-SERVICE/payments/" + orderId, PaymentResponse.class);
        return parseOrderToOrderResponse(order.get(), paymentResponse);
    }

public OrderResponse orderFallBack(Integer orderId) {
        //TODO: RETURN SOMETHING GOOD HERE
        return null;
    }

Config Server

Application YAML in config server

spring:
  application:
    name: CONFIG-SERVER
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bijay-shrestha/SpringBootMicroservicesCloud.git
server:
  port: 7070

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:9090/eureka
  instance:
    hostname: localhost

Bootstrap YAML in config server

spring:
  cloud:
    config:
      uri:
        - http://localhost:7070

MAVEN configuration in config server

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

MAVEN configuration in client

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

MAVEN Open Feign

@EnableFeignClients

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Feign Interface

@Component
@FeignClient("order-service")
public interface OrderFeignInterface {

    @PostMapping("/api/v1/orders/checkout")
    String checkout(@RequestBody OrderRequestDTO orderRequestDTO);

}

@PutMapping("/api/v1/stock/{productNumber}")
StockResponseDTO updateProduct(@PathVariable String productNumber, @RequestBody StockRequestDTO stockRequestDTO);

Autowiring requirement

private final OrderFeignInterface orderFeignInterface;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment