Spring Cloud Alibaba Sidecar 多语言微服务异构

码界探索家
• 阅读 4723

Spring Cloud Alibaba Sidecar 介绍

Spring Cloud Alibaba 2.1.1 版本后增加了 spring-cloud-alibaba-sidecar 模块作为作为一个代理的服务来间接性的让其他语言可以使用spring cloud alibaba等相关组件。通过与网关的来进行路由的映射,从而可以做到服务的获取,然后可以使用Ribbon间接性调用。

Spring Cloud Alibaba Sidecar 多语言微服务异构

如上图, Spring Cloud 应用 请求 sidercar 然后转发给其他语言的模块,优势是对于异构服务代码 零侵入,不需要直接根据 nacos 或其他注册中心 api 注册等

使用入门

构建其他语言接口服务

  • 基于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)
}

构建 sidercar 应用

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

# 配置异构服务
sidecar:
  ip: localhost
  port: 8089
  health-check-url: http://localhost:8089/health

构建 nacos consumer应用

  • application.yml
server:
  port: 8087
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: nacos-consumer
  • consumer 逻辑
@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);
    }

}

测试使用

  • 访问spring cloud consumer 应用
curl http://localhost:8087/test   
  • 输出 go-provider应用
hello spring cloud alibaba sidecar
项目推荐: Spring Cloud 、Spring Security OAuth2的RBAC权限管理系统 欢迎关注
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
springcloud eureka.instance
1.在springcloud中服务的 InstanceID默认值是:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance\_id:${server.port}},也就是:主机名:应用名:应用端口。如图1
Stella981 Stella981
3年前
Spring Cloud Alibaba系列(二)nacos作为服务配置中心
SpringCloudAlibaba系列(二)nacos作为服务配置中心Nacos提供用于存储配置和其他元数据的key/value存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用SpringCloudAlibabaNacosConfig,您可以在NacosServer集中管理你SpringCloud应用的外部
Easter79 Easter79
3年前
SpringCloud consul 微服务(注册到主机名的问题)
目前项目在使用consul做服务注册与发现,做SpringSecurityOAuth2权限认证的authorization\_code模式的时候发现一个异常坑爹的问题这是开始的服务注册代码块bootstrap.yml:spring:cloud:consul:port:8500
Stella981 Stella981
3年前
Spring Cloud Alibaba Sidecar 多语言微服务异构
SpringCloudAlibabaSidecar介绍自SpringCloudAlibaba2.1.1版本后增加了springcloudalibabasidecar模块作为作为一个代理的服务来间接性的让其他语言可以使用springclouda
Stella981 Stella981
3年前
Spring Cloud Alibaba的nacos作为注册中心
一 官网参考https://github.com/alibaba/springcloudalibaba/blob/master/springcloudalibabaexamples/nacosexample/nacosdiscoveryexample/readmezh.md(https://www.oschina.ne
Wesley13 Wesley13
3年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Stella981 Stella981
3年前
Spring Cloud Alibaba 发布毕业后的首个版本
相信大家对上周的《来自SpringCloud官方的消息,SpringCloudAlibaba即将毕业》(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fmp.weixin.qq.com%2Fs%3F__biz%3DMzU4NzU0MDIzOQ%3D%3D%26mid%3D22
Wesley13 Wesley13
3年前
TARS为SpringCloud提供高性能的RPC能力
传统HTTP存在的瓶颈Spring Cloud 是一个优秀的开源微服务解决方案,通常采用 HTTP  json 的 REST 接口对外提供服务,简洁易用部署方便,很多公司也基于 Spring Cloud 作为基础架构去构建自身的微服务架构。但是随着业务规模和用户规模的增长,传统基于的 HTTP 的服务会逐步暴露出一些问题。首先是性能
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(