非SpringBoot项目接入SpringCloud之SideCar

非SpringBoot项目接入SpringCloud之SideCar

首先服务注册中心肯定要有的。
服务注册中心搭建参考博客:https://blog.csdn.net/forezp/article/details/69696915
需要注意的是版本问题,可能存在NoSuchMethod报错。
我是改成1.5.9.RELEASE了。
还有就是Dalston.RC1 改成 Dalston.SR5。
会的直接跳过以上就行了。
需要说的是本文使用的是IntelliJ IDEA
SideCar:

①创建一个SpringBoot项目,

新建Project -> Spring Initializr -> 命名 -> Web(Web) -> NEXT…

或者也可以整合到一个项目,看你需求:
在你的项目右键新建module-> Spring Initializr -> 命名 -> NEXT…


②导入SideCar依赖:

Spring版本为1.5.9.RELEASE,过高存在不兼容现象。

  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-sidecar</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
<!--    如果配置@EnableSidecar找不到包,加入以下 -->
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


③配置application.yml:

server:
  port: 5678
spring:
  application:
    name: sidecar

sidecar:
  port: 8080
     health-uri: http://localhost:${sidecar.port}/health

server.port为sidecar监听的端口,sidecar.port是程序监听的端口号,
sidecar.health-uri是程序的可访问的uri地址,它需要返回以下json

{“status”:”UP”}

④springboot程序入口配置 @EnableSidecar


到此为止,SideCar就搭建好了。我们回到项目上。

在Controller中:
非JAVA项目也是一个道理,程序中收到SideCar发送的请求,你返回一个json就行了。

@RequestMapping("/health")
@ResponseBody
public @ResponseBody Map<String, Object> status(){
     Map<String, Object> map = new HashMap();
     //@ResponseBody将map转为json
     map.put("status", "UP");
     return map;
}

注:通过返回一个json,其用status字段来标识你的应用的服务状态,是up还是down,sidecar会将该状态报告给eureka注册中心从而实现你的服务的状态可用情况。

运行非SpringBoot项目、服务注册中心以后,再运行SideCar项目,
打开http://localhost:8761
Eureka检测到SideCar程序的状态为UP时,项目接入服务注册中心成功。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值