• Netflix SpringCloud-Eureka


    Netflix(奈飞或网飞,这家公司的企业文化值得学习),推出的SpringCloud版本,可以说是早期比较成熟的微服务解决方案。

    后来Netflix放弃了SpringCloud相关服务组件的维护更新,Alibaba版SpringCloud就顺势而为。

    本篇简单记录下Netflix版SpringCloud的工程结构,权当编程思想借鉴,不作技术参考(可以说已是过时了的技术)。

    1. 单机版eureka

    eureka,意为找到了,发现了,作为服务注册中心,就类似zookeeper的角色,或者AlibabaCloud中的nacos。

    1.1 pom

    1. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    2. 4.0.0
    3. com.demo
    4. eureka-server
    5. 0.0.1
    6. jar
    7. eureka-server
    8. Demo project for Spring Boot
    9. org.springframework.boot
    10. spring-boot-starter-parent
    11. 1.5.12.RELEASE
    12. UTF-8
    13. UTF-8
    14. 1.8
    15. Edgware.SR3
    16. org.springframework.cloud
    17. spring-cloud-starter-netflix-eureka-server
    18. org.springframework.boot
    19. spring-boot-starter-test
    20. test
    21. org.springframework.cloud
    22. spring-cloud-dependencies
    23. ${spring-cloud.version}
    24. pom
    25. import
    26. org.springframework.boot
    27. spring-boot-maven-plugin
    28. spring-milestones
    29. Spring Milestones
    30. https://repo.spring.io/milestone
    31. false

    1.2 yml

    1. server:
    2. port: 8888
    3. spring:
    4. application:
    5. name: eureka-server
    6. ## 默认情况下eureka server也是一个eureka client ,必须要指定一个 server
    7. ## 通过eureka.client.registerWithEureka:false和fetchRegistry:false
    8. ## 来表明自己是一个eureka server
    9. eureka:
    10. instance:
    11. hostname: localhost
    12. client:
    13. registerWithEureka: false
    14. fetchRegistry: false
    15. serviceUrl:
    16. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    1.3 application

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    4. /**
    5. * 启动服务注册中心
    6. * 访问 http://localhost:8888可以查看server服务注册情况
    7. */
    8. @EnableEurekaServer
    9. @SpringBootApplication
    10. public class EurekaServerApplication {
    11. public static void main(String[] args) {
    12. SpringApplication.run(EurekaServerApplication.class, args);
    13. }
    14. }

    1.4 monitor

    2. 集群版eureka

    作为服务注册中心,如果是单机版,就会存在单点故障。因此,实际生产环境中,eureka也是集群部署的。

    eureka集群,其实也就是eureka服务部署多个节点,相互注册发现,关联起来即可。下面以两个eureka服务节点集群为例。

    2.1 pom (同1.1)

    2.2 yml

    准备工作(本地环境域名解析,实际生产环境为真实域名DNS解析):

    ## 修改hosts,
    ## [windows] c:/windows/System32/drivers/etc/hosts

    ## [linux] vim /etc/hosts,加上:

    ## 127.0.0.1 peer1
    ## 127.0.0.1 peer2

    application.yml

    1. spring:
    2. application:
    3. name: eureka-server
    4. ## 启动不同的服务节点时,对应激活节点配置文件即可
    5. ## java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2
    6. profiles:
    7. active: peer1

    application-peer1.yml

    1. server:
    2. port: 8888
    3. eureka:
    4. instance:
    5. hostname: peer1
    6. client:
    7. serviceUrl:
    8. defaultZone: http://peer2:8889/eureka/

    application-peer2.yml

    1. server:
    2. port: 8889
    3. eureka:
    4. instance:
    5. hostname: peer2
    6. client:
    7. serviceUrl:
    8. defaultZone: http://peer1:8888/eureka/

    2.3 application

    # 进入pom文件所在目录位置执行cmd打包(或直接使用IDEA工具打包

    mvn package 

    java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1

    java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2

    1. /**
    2. * 启动服务注册中心
    3. * http://localhost:8888/ 可以查看第一个server服务注册情况
    4. * http://localhost:8889/ 可以查看第二个server服务注册情况
    5. */
    6. @EnableEurekaServer
    7. @SpringBootApplication
    8. public class EurekaServerApplication {
    9. public static void main(String[] args) {
    10. SpringApplication.run(EurekaServerApplication.class, args);
    11. }
    12. }

    先后启动peer1, peer2两个节点服务,发现peer1刚启动时会报错,这是因为peer1会到peer2上去注册服务,发现peer2服务尚未启动,所以报错了,可以忽略不计,待peer2服务节点启动后,peer1就会发现peer2并相互注册服务,关联到一起成为集群的注册中心服务。

    2.4 monitor

    访问peer1 :

    http://peer1:8888

    访问peer2 :

    http://peer2:8889

    3. eureka client (micro-service)

    这里所说的eureka client就是所谓的具有专属业务功能模块划分的微服务了

    3.1 pom (同1.1)

    3.2 yml

    1. server:
    2. port: 8887
    3. ## 需要指明spring.application.name,这个很重要,
    4. ## 这在以后的服务与服务之间相互调用一般都是根据这个name
    5. spring:
    6. application:
    7. name: eureka-client
    8. eureka:
    9. client:
    10. serviceUrl:
    11. defaultZone: http://peer1:8888/eureka/,http://peer2:8889/eureka/

    3.3 application

    启动eureka-client 微服务​​‍

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    4. @SpringBootApplication
    5. @EnableEurekaClient
    6. public class EurekaClientApplication {
    7. public static void main(String[] args) {
    8. SpringApplication.run(EurekaClientApplication.class, args);
    9. }
    10. }

    3.4 monitor

    任意访问peer1 或 peer2注册中心服务节点,都可以看到刚启动的eureka-client微服务已经注册上来了。

  • 相关阅读:
    通信原理板块——时分复用
    windows中怎样使用nginx?
    面试突击72:输入URL之后会执行什么流程?
    剑指 Offer II 070(力扣540):排序数组中只出现一次的数字(Java二分查找)
    什么是 JVM ?
    BetterZip for Mac2024最新mac解压缩软件
    第七版教材下的PMP考试有多难?
    ES (ElasticSearch) 简易解读(二)ES安装及集群的搭建
    electron 基础项目搭建 &&主线程和渲染线程的通信
    湖北大学计算机考研资料汇总
  • 原文地址:https://blog.csdn.net/CaBCo/article/details/126021158