Spring Cloud Alibaba Sidecar multilingual microservices heterogeneous

Keywords: Java Spring JSON encoding curl

Introduction to Spring Cloud Alibaba Sidecar

Since spring cloud Alibaba version 2.1.1, the spring cloud Alibaba sidecar module has been added as a proxy service to indirectly let other languages use spring cloud Alibaba and other related components. Through the route mapping with the gateway, the service can be obtained, and then the Ribbon can be used for indirect calls.

As shown in the figure above, the Spring Cloud application requests sidercar and then forwards it to modules in other languages. The advantage is that there is no invasion for heterogeneous service code, and it does not need to register directly according to nacos or other registration center APIs

Getting Started

Building other language interface services

  • Write a simple service interface based on go

http://127.0.0.1:8089/sidecar

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/sidecar", sidecar)
    http.HandleFunc("/heath", health)
    log.Fatal(http.ListenAndServe(":8089", nil))
}
func sidecar(w http.ResponseWriter, r *http.Request) {
    _, _ = fmt.Fprintf(w, "hello spring cloud alibaba sidecar")
}

func health(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    actuator := make(map[string]string)
    actuator["status"] = "UP"
    _ = json.NewEncoder(w).Encode(actuator)
}

Building sidercar application

  • Increase sidecar dependency
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sidecar</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
  • Configure application.yml
server:
  port: 8088
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: go-provider

# Configure heterogeneous services
sidecar:
  ip: localhost
  port: 8089
  health-check-url: http://localhost:8089/health

Building nacos consumer application

  • application.yml
server:
  port: 8087
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: nacos-consumer
  • consumer logic
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public String test() {
        return restTemplate.getForObject("http://go-provider/sidecar", String.class);
    }

}

Test use

  • Visit the spring cloud consumer app
curl http://localhost:8087/test   
  • Output go provider application
hello spring cloud alibaba sidecar

Project recommendation: Welcome to RBAC permission management system of Spring Cloud and Spring Security OAuth2

Posted by ngubie on Mon, 18 Nov 2019 11:22:19 -0800