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

Spring Cloud Alibaba Sidecar 介绍

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

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

使用入门

构建其他语言接口服务

  • 基于go 写个简单的服务接口

http://127.0.0.1:8089/sidecar

1package main 2 3import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "net/http" 8) 9 10func main() { 11 http.HandleFunc("/sidecar", sidecar) 12 http.HandleFunc("/heath", health) 13 log.Fatal(http.ListenAndServe(":8089", nil)) 14} 15func sidecar(w http.ResponseWriter, r *http.Request) { 16 _, _ = fmt.Fprintf(w, "hello spring cloud alibaba sidecar") 17} 18 19func health(w http.ResponseWriter, r *http.Request) { 20 w.Header().Set("Content-Type", "application/json") 21 actuator := make(map[string]string) 22 actuator["status"] = "UP" 23 _ = json.NewEncoder(w).Encode(actuator) 24}

构建 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 {

    1public static void main(String[] args) { 2 SpringApplication.run(NacosConsumerApplication.class, args); 3} 4 5@Bean 6@LoadBalanced 7public RestTemplate restTemplate() { 8 return new RestTemplate(); 9} 10 11@Autowired 12private RestTemplate restTemplate; 13 14@GetMapping("/test") 15public String test() { 16 return restTemplate.getForObject("http://go-provider/sidecar", String.class); 17}

    }

测试使用

> 项目推荐: Spring Cloud 、Spring Security OAuth2的RBAC权限管理系统 欢迎关注

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )