spring-cloud(7)【Sidecar整合其他服务】

可以使用sidecar来整合其他web项目到springCloud中。

1. 为web项目添加健康检查接口

提供http接口,返回json:{"status" : "up"},status用于描述微服务的状态,常见的取值有UP,DOWN,OUT_OF_SERVICE,UNKNOWN等

2. 编写sidecar微服务

  • 创建项目
  • 添加eureka,sidecar,zuul的依赖
  • 启动类上加上@EnableSidecar注解。这是一个组合注解,它整合了三个注解,分别是@EnableCircuiBreaker,@EnableDiscoveryClient和@EnableZuulProxy
  • 在配置文件中加入端口号,服务名称,eureka地址以及web服务的端口以及健康检查地址,如:
server.port=8887
spring.application.name=sidecar-mylife-service
eureka.client.serviceUrl.defaultZone=http://localhost:8881/eureka/
eureka.client.instance.prefer-ip-address=true
sidecar.port=8080
sidecar.health-uri=http://localhost:8080/health
eureka.instance.hostname=localhost

启动项目,并访问8887接口,就可以访问到web项目中的接口。

3. Sidecar的一些端点

  • / 返回一个测试页面,展示sidecar的常用端点
  • /hosts/{serviceId} 指定微服务在eureka上的实例列表。
  • /ping 返回OK字符串
  • /{serviceId} 由于sidecar整合了zuul,因此使用该将请求转发到service对应的微服务

4. 将sidecar与web服务分开部署

  • 方式一:

    sidecar.hostname : web服务的hostname

    sidecar.ip-address : web服务的ip地址

  • 方式二:

    eureka.instance.hostname=localhost

猜你喜欢

转载自blog.csdn.net/wangzhanzheng/article/details/80596545