Build API Gateway with Spring Cloud Gateway and Consul

Giới Thiệu Tiếp tục với series Java Practice, hôm nay chúng ta tiếp tục tìm hiểu về API Gateway trong triển khai mô hình Microservices, bắt đầu thôi. Consul là gì Consul là một service mesh được phát triển bởi Hashicorp, và service discovery là 1 phần chức năng được phát triển trong Consul và

Giới Thiệu

Tiếp tục với series Java Practice, hôm nay chúng ta tiếp tục tìm hiểu về API Gateway trong triển khai mô hình Microservices, bắt đầu thôi.

Consul là gì

Consul là một service mesh được phát triển bởi Hashicorp, và service discovery là 1 phần chức năng được phát triển trong Consul và chúng ta sẽ sử dụng nó trong bài này. Ngoài chức năng service discovery ra Consul còn tích hợp nhiều tính năng rất hay có thể kể đến như Consul Config có thể sử dụng như một config central, Proxy, … Consul sẽ giúp việc kết nối các ứng dụng lại với nhau một cách dễ dàng.

Consul có 4 thành phần chính

  1. Service Discovery: các service client đăng ký vào, Consul hiện cho phép đăng ký dưới 2 phương thức DNS và HTTP.
  2. Health Checking: Consult HC các Node, instances để đảm bảo các service client còn sống.
  3. KV Store: Bạn có thể quản lý config của mình tại đây.
  4. Secure Service Communication: Thiếp lập bảo mật cho các kết nối tới nhau.
  5. Multi Datacenter: Nơi cấu hình các datacenters.

Cài đặt Consul

Để bài viết không bị quá dài và lan man, nên chúng ta chỉ cấu hình Consul trên Docker nhé. Tải xuống docker image.

docker pull consul

Khởi chạy Consul.

$ docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 consul

Xem thêm tại: https://hub.docker.com/_/consul

Api Gateway trong Consul

Trong ví dụ hôm nay mình sẽ triển khai Api Gateway bằng Spring Cloud Gateway và các service client mình sẽ viết trên Quarkus.

Service client

Trong bài viết này mình sẽ tiến hành dựng 2 service bằng Quarkus nhé. Các bạn có thể khởi tạo source Quarkus tại đây.

Để kết nối tới Consul trong Quarkus, bạn tiếp hành add 2 dependency này vô file POM của 2 Service Client nhé.

<dependency><groupId>com.orbitz.consul</groupId><artifactId>consul-client</artifactId><version>1.5.3</version></dependency><dependency><groupId>io.quarkus</groupId><artifactId>quarkus-consul-config</artifactId></dependency>

Tiếp theo tại service 1 mình tạo ra ProductController để hiện ra thông tin sản phẩm.

@Path("/product")publicclassProductController{@GET@Produces(MediaType.TEXT_PLAIN)publicStringgetProduct(){return"Sản Phẩm 1";}}

Tại service 2 mình tạo ra StockController để hiện ra thông tin tồn kho.

@Path("/stock")publicclassStockController{@GET@Produces(MediaType.TEXT_PLAIN)publicIntegergetStock(){return1;}}

Tại 2 service mình tiến hành kết nối tới Consul để đăng ký. Mình tạo file ServiceLifecycle.java ở cả 2 serivice để đăng ký tới Consul.

@ApplicationScopedpublicclassServiceLifecycle{privatestaticfinalLogger LOGGER =Logger.getLogger(ServiceLifecycle.class);privateString instanceId;Consul consulClient =Consul.builder().withHostAndPort(HostAndPort.fromParts("localhost",8500)).build();@ConfigProperty(name ="quarkus.application.name")String appName;@ConfigProperty(name ="quarkus.application.version")String appVersion;@ConfigProperty(name ="quarkus.profile")String profile;@ConfigProperty(name ="quarkus.http.port")String httpPort;ConfigUtils.ConfigBO config =ConfigUtils.get();voidonStart(@ObservesStartupEvent ev){if(consulClient ==null){
      LOGGER.error("Inject Consul failed......");return;}
    LOGGER.info("The ServiceLifecycle is starting...");try{ScheduledExecutorService executorService =Executors.newSingleThreadScheduledExecutor();
      executorService.schedule(()->{HealthClient healthClient = consulClient.healthClient();List<ServiceHealth> instances = healthClient
                .getHealthyServiceInstances(appName + profile).getResponse();var instancesSize = instances.size();
        instanceId = appName + profile +"-"+ instancesSize;ImmutableRegistration registration =ImmutableRegistration.builder().id(instanceId).name(appName + profile).address("localhost").port(8080).putMeta("version", appVersion).tags(Collections.singletonList("hpi")).serviceWeights(ImmutableServiceWeights.builder().passing(1).warning(5).build()).build();
        consulClient.agentClient().register(registration);
        LOGGER.info("Instance registered: id="+ registration.getId()+ ");},5000,TimeUnit.MILLISECONDS);}catch(Exception e){
      LOGGER.error("ServiceLifecycle "+ e.getMessage());}}voidonStop(@ObservesShutdownEvent ev){if(consulClient ==null){
      LOGGER.info("consulClient is null, can not de-registered...");return;}if(instanceId !=null){
      consulClient.agentClient().deregister(instanceId);
      LOGGER.info("Instance de-registered: id="+ instanceId);}}}

Khi chương trình java của bạn được khởi động, Quarkus sẽ tự động chạy onStart và khởi tạo đăng ký tới Consul sau 5 giây, và chạy hàm onStop để hủy đăng ký tới Consult khi chương trình tắt.

Sau khi chạy 2 service client, bạn sẽ nhận được kết quả như hình dưới.

Spring Cloud Gateway

Tiếp theo bạn tiến hành add các dependency để kết nối Consul trong Spring Cloud Gateway (nếu bạn chưa có project Spring Cloud Gateway, bạn có thể xem qua tại đây.

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-config</artifactId><version>3.1.0</version></dependency>

Sau đó bạn tiếp hành cấu hình ApiGateWay trong application.yml.

management:endpoints:web:exposure:include: info,health,prometheus,metrics

---spring:config:activate.on-profile: dev
  application:name: gwapi
  cloud:gateway:httpclient:pool:type: fixed
          max-connections:3000acquire-timeout:8000max-life-time: 300s
          max-idle-time: 10s
        connect-timeout:30000response-timeout: 60s
      default-filters:- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
      globalcors:cors-configurations:'[/**]':allowedOrigins:"*"allowedMethods:"*"allowedHeaders:"*"discovery:locator:enabled:trueroutes:-id: hpiconsul-s1
          uri: lb://hpiconsul-s1
          predicates:- Path=/service-1/**filters:- RewritePath=/service-1/(?<path>.*), /${path}-id: hpiconsul-s2
          uri: lb://hpiconsul-s2
          predicates:- Path=/service-2/**filters:- RewritePath=/service-2/(?<path>.*), /${path}loadbalancer:ribbon:enabled:falseconsul:host: localhost
      port:8500discovery:instanceZone: gw-service-api
        register:falseregisterHealthCheck:falsetags: gw-service-api-tags
        healthCheckPath: /
        healthCheckInterval: 15s
      config:enabled:falseretry:enabled:trueinitialInterval:2server:port:2060

Trong đó:

  • globalcorsdefault-filters là cấu hình giúp bạn bật/tắt CORS.
  • httpclient để bạn cấu hình tham số như pool khi gọi tcp ( các tham số phía trên đã được HPI điều chỉnh phù hợp và sử dụng.
  • routes giúp bạn khai báo các routes.

Lúc này bạn truy cập trình duyệt của mình localhost:2060/service-1/product Consul sẽ gọi url localhost:8080/product và trả kết quả về cho bạn, kết quả cũng tương tự khi bạn gọi service-2.

Ngoài ra bạn cũng có thể làm nhiều hơn ví dụ như lấy các instances của 1 service, health-check, redirect url… bằng code =)).

Đoạn code phía dưới có thể giúp bạn tìm hiểu rõ và sâu hơn nữa về Consul khi thử.

@RestController@ComponentpublicclassDiscoveryClientController{@AutowiredprivateDiscoveryClient discoveryClient;ConsulServiceInstance service;@AutowiredRestTemplate restTemplate;@GetMapping("/gw-discovery")publicList<ServiceInstance>ServiceInstance(){List<ServiceInstance> list = discoveryClient.getInstances("hpiconsul-s1");return list;}publicOptional<URI>serviceUrl(){return discoveryClient.getInstances("hpiconsul-s2").stream().findFirst().map(si -> si.getUri());}@GetMapping("/gw-api/redirect/{url}")publicStringdiscoveryPing(@PathVariable("url")String url)throwsRestClientException,ServiceUnavailableException{URI service =serviceUrl().map(s -> s.resolve("/"+ url)).orElseThrow(ServiceUnavailableException::new);System.out.println(service.getHost());return restTemplate.getForEntity(service,String.class).getBody();}@GetMapping("/gw-swagger-ui")publicStringswagger()throwsRestClientException,ServiceUnavailableException{URI service =serviceUrl().map(s -> s.resolve("/q/swagger-ui/")).orElseThrow(ServiceUnavailableException::new);return restTemplate.getForEntity(service,String.class).getBody();}}

Kết Luận

Hy vọng qua bài viết ngắn có phần sơ sài này có thể giúp ích các bạn trong việc tìm hiểu và triển khai API Gateway + Consul nhé, chúc các bạn thành công.

Mục tìm kiếm đồng đội

Xí Xí, đừng quên HPI vẫn còn đang tuyển dụng rất nhiều vị trí hot với mức lương hấp dẫn nhé.

Đồng đội Senior Backend Engineer (Java/Go).

Đồng đội Junior Backend Engineer(Java/Go).

Đồng đội Senior Frontend Engineer(Vuejs/React).

Nguồn: viblo.asia

Bài viết liên quan

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,