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

2020-03-10  本文已影响0人  sstraybitd

在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两个微服务添加如下内容

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
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实例

观察效果

通过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,
}
上一篇下一篇

猜你喜欢

热点阅读