美文网首页
spring boot微服务-第五部分:使用Eureka

spring boot微服务-第五部分:使用Eureka

作者: sstraybitd | 来源:发表于2020-03-10 16:36 被阅读0次

    在2和3部分,我们创建了FS和CCS两个微服务并建立了通信
    在第4部分,我么使用Ribbon在两个FS实例之间实现了负载
    但是,在CCS配置文件中,我们硬编码了两个FS实例的URL

    forex-service.ribbon.listOfServers=localhost:8000,localhost:8001
    

    这意味着每次启动一个新的FS实例,我们需要改变CCS的配置文件。
    在这部分,我么使用Eureka解决这个问题

    image.png

    创建Eureka服务

    @SpringBootApplication
    @EnableEurekaServer
    public class SpringBootMicroserviceEurekaNamingServerApplication {
    

    在application.properties中,配置应用名词和端口

    spring.application.name=netflix-eureka-naming-server
    server.port=8761
    
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    

    访问Eureka服务

    访问地址:http://localhost:8761

    image.png

    使用Eureka连接FS和CCS

    对FS和CCS两个微服务添加如下内容

    • pom文件
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    
    • application.properties配置文件
    eureka.client.service-url.default-zone=http://localhost:8761/eureka
    

    重启CCS和FS服务,可以看到CSS和FS服务注册到Eureka


    image.png

    在端口8081,启动另一个实例FS,可以看到一个CCS实例和两个FS实例注册到Eureka


    image.png

    使用Eureka路由Ribbon的请求

    删除CCS配置文件中的配置项

    forex-service.ribbon.listOfServers=localhost:8000,localhost:8001
    

    重启CCS实例

    观察效果

    • CCS服务运行于8100
    • 两个FS实例分别运行于8000和8001
    • Eureka服务启动着

    通过Eureka提供,路由Ribbon请求,当两次请求CCS时分别看到如下效果:

    {
      id: 10002,
      from: "EUR",
      to: "INR",
      conversionMultiple: 75,
      quantity: 10000,
      totalCalculatedAmount: 750000,
      port: 8000,
    }
    
    {
      id: 10002,
      from: "EUR",
      to: "INR",
      conversionMultiple: 75,
      quantity: 10000,
      totalCalculatedAmount: 750000,
      port: 8001,
    }
    

    相关文章

      网友评论

          本文标题:spring boot微服务-第五部分:使用Eureka

          本文链接:https://www.haomeiwen.com/subject/knzwdhtx.html