mysql的去重添加ing_详解Spring Cloud Alibaba Sidecar多语言微服务异构

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

a000707a3b71a8beef0a1ed2461f7c52.png

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

使用入门

构建其他语言接口服务

使用go 写个简单的服务接口

http://127.0.0.1:8089/sidecar

package mainimport ("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 依赖

com.alibaba.cloud

spring-cloud-starter-alibaba-sidecar

2.1.1.RELEASE

配置 application.yml

server:

port: 8088spring:

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: 8087spring:

cloud:

nacos:

discovery:

server-addr: localhost:8848

application:

name: nacos-consumer

consumer 逻辑

@RestController@EnableDiscoveryClient@SpringBootApplicationpublic 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持乐购源码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值