资讯专栏INFORMATION COLUMN

SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如

caozhijian / 2780人阅读

摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。包含了客户端注解,同时也包含了断路器模块注解,还包含了网关模块。

SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)

-

一、大致介绍
1、在一些稍微复杂点系统中,往往都不是单一代码写的服务,而恰恰相反集成了各种语言写的系统,并且我们还要很好的解耦合集成到自己的系统中;
2、出于上述现状,SpringCloud 生态圈中给我们提供了很好的插件式服务,利用 sidecar 我们也可以轻松方便的集成异构系统到我们自己的系统来;
3、而本章节目的就是为了解决轻松简便的集成异构系统到自己的微服务系统中来的;
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-sidecar
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    

    
        
        
            org.springframework.cloud
            spring-cloud-netflix-sidecar
        

        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
    

2.2 添加应用配置文件(springms-sidecarsrcmainresourcesapplication.yml)
spring:
  application:
    name: springms-sidecar
server:
  port: 8210
eureka:
  datacenter: SpringCloud   # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Data center 显示信息
  environment: Test         # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Environment 显示信息
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka
    healthcheck:  # 健康检查
      enabled: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}



#####################################################################################################
# 打印日志
logging:
  level:
    root: INFO
    com.springms: DEBUG
#####################################################################################################





#####################################################################################################
# 异构微服务的配置, port 代表异构微服务的端口;health-uri 代表异构微服务的操作链接地址
sidecar:
  port: 8205
  health-uri: http://localhost:8205/health.json
#####################################################################################################
2.3 添加sidecar微服务启动类(springms-sidecarsrcmainjavacomspringmscloudMsSideCarApplication.java)
package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;

/**
 * 集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)。
 *
 * 注意 EnableSidecar 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。
 *
 * @EnableSidecar --> { @EnableCircuitBreaker、@EnableDiscoveryClient、@EnableZuulProxy } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解,还包含了 zuul API网关模块。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/28
 *
 */
@SpringBootApplication
@EnableSidecar
public class MsSideCarApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsSideCarApplication.class, args);
        System.out.println("【【【【【【 SideCar 微服务 】】】】】】已启动.");
    }
}
三、测试
/****************************************************************************************
 一、集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)(正常情况测试):

 1、编写 application.yml 文件,添加应用程序的注解 EnableSidecar 配置;
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-gateway-zuul-fallback 模块服务,启动1个端口;
 4、启动 springms-sidecar 模块服务,启动1个端口;
 5、启动 springms-node-service 微服务,启动1个端口;

 6、新起网页页签,输入 http://localhost:8205/ 正常情况下是能看到 "欢迎来到简单异构系统之 nodejs 服务首页" 信息被打印出来;
 7、新起网页页签,然后输入 http://localhost:8205/health.json,正常情况下是能看到 "{"status":"UP"}" 信息被打印出来;

 总结一:nodejs 微服务,自己访问自己都是正常的;

 8、新起网页页签,输入 http://localhost:8200/springms-sidecar/ 正常情况下是能看到 "欢迎来到简单异构系统之 nodejs 服务首页" 信息被打印出来;
 9、新起网页页签,然后输入 http://localhost:8200/springms-sidecar/health.json,正常情况下是能看到 "{"status":"UP"}" 信息被打印出来;

 总结二:通过在yml配置文件中添加 sidecar 属性,就可以将异构系统添加到SpringCloud生态圈中,完美无缝衔接;
 ****************************************************************************************/

/****************************************************************************************
 二、集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)(除了包含异构微服务外,还添加 Ribbon 模块电影微服务):

 1、编写 application.yml 文件,添加应用程序的注解 EnableSidecar 配置;
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-gateway-zuul-fallback 模块服务,启动1个端口;
 4、启动 springms-sidecar 模块服务,启动1个端口;
 5、启动 springms-consumer-movie-ribbon 模块服务,启动1个端口;
 6、启动 springms-node-service 微服务,启动1个端口;

 7、新起网页页签,输入 http://localhost:8205/ 正常情况下是能看到 "欢迎来到简单异构系统之 nodejs 服务首页" 信息被打印出来;
 8、新起网页页签,然后输入 http://localhost:8205/health.json 正常情况下是能看到 "{"status":"UP"}" 信息被打印出来;

 总结一:nodejs 微服务,自己访问自己都是正常的;

 9、新起网页页签,输入 http://localhost:8200/springms-sidecar/ 正常情况下是能看到 "欢迎来到简单异构系统之 nodejs 服务首页" 信息被打印出来;
 10、新起网页页签,然后输入 http://localhost:8200/springms-sidecar/health.json 正常情况下是能看到 "{"status":"UP"}" 信息被打印出来;

 总结二:通过 Zuul 代理模块,统一入口路径,也可以从 zuul 上成功访问异构系统;

 11、新起网页页签,输入 http://localhost:8010/sidecar/ 正常情况下是能看到 "欢迎来到简单异构系统之 nodejs 服务首页" 信息被打印出来;
 12、新起网页页签,然后输入 http://localhost:8010/sidecar/health.json 正常情况下是能看到 "{"status":"UP"}" 信息被打印出来;

 总结三:给 springms-consumer-movie-ribbon 微服务添加几个方法,也可以成功访问以异构系统,可见利用 SpringCloud 来集成异构系统简便了很多;
 ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67622.html

相关文章

  • SpringCloud 005 )电影微服务,注册 EurekaServer 中,通过 Ht

    摘要:第篇电影微服务,也注册到中,通过协议访问已注册到生态圈中的用户微服务一大致介绍在服务治理框架中,微服务与微服务之间通过协议进行通信用户微服务作为消费方电影微服务作为提供方都注册到中在电影微服务层通过的硬编码配置方式实现服务之间的通信二实现 SpringCloud(第 005 篇)电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务 -...

    nifhlheimr 评论0 收藏0
  • SpringCloud 026 )简单异构系统之 nodejs 微服务

    摘要:第篇简单异构系统之微服务一大致介绍因为在后面要利用集成异构系统,所以才有了本章节的微服务本章节使用了最简单的请求截取的方式,截取不同的后缀做不同的响应处理,简直二实现步骤添加服务端文件引入模块创建获得请求的路径访问,将会返回欢迎 SpringCloud(第 026 篇)简单异构系统之 nodejs 微服务 - 一、大致介绍 1、因为在后面要利用 SpringCloud 集成异构系统,所...

    raledong 评论0 收藏0
  • SpringCloud 003 服务发现服务端EurekaServer微服务

    摘要:第篇服务发现服务端微服务一大致介绍众所周知,在现在互联网开发中,访问地址的和端口号是动态的,一个服务停掉再重新启用后和端口就可能发生了改变,所以用硬编码是肯定不行了。再对外提供服务的时候便不再使用挂掉的服务提供者的和端口。 SpringCloud(第 003 篇)服务发现服务端EurekaServer微服务 - 一、大致介绍 1、众所周知,在现在互联网开发中,访问地址的IP和端口号是...

    wuyumin 评论0 收藏0
  • SpringCloud 046 )注解式Schedule配置定时任务,不支持任务调度

    摘要:当前时间打印当前时间定时任务触发,操作多个添加数据,事务中任一异常,都可以正常导致数据回滚。当前时间当前时间添加微服务启动类注解式配置定时任务,不支持任务调度。 SpringCloud(第 046 篇)注解式Schedule配置定时任务,不支持任务调度 - 一、大致介绍 1、很多时候我们需要隔一定的时间去执行某个任务,为了实现这样的需求通常最普通的方式就是利用多线程来实现; 2、但是有...

    masturbator 评论0 收藏0
  • SpringCloud 012 )微服务接入 Feign 负载均衡通过 FeignClient

    摘要:添加电影微服务启动类电影微服务接入进行客户端负载均衡,通过调用远程微服务。注解表示该电影微服务已经接入模块。 SpringCloud(第 012 篇)电影微服务接入 Feign 进行客户端负载均衡,通过 FeignClient 调用远程 Http 微服务 - 一、大致介绍 1、本章节主要介绍在 SpringCloud 生态圈中,使用一个类似于 Java HTTP 客户端的工具 Feig...

    Cobub 评论0 收藏0

发表评论

0条评论

caozhijian

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<