Eureka

Eureka,第1张

Eureka 1.eureka-server 1.1 pom文件

构建eureka-server微服务时,需要先在pom文件中引入相应的坐标

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
1.2 配置文件

需要在配置文件中,对相应的参数进行配置application.yml

spring.application.name: eureka-server

server.port: 8761
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
  1. spring.application.name:定义服务名称
  2. server.port: 定义服务端口号为8761
  3. eureka.client.fetch-registry: 设置为false表示不要再本地缓存注册表信息
  4. eureka.client.registerWithEureka 属性会告知服务,在应用程序启动时不要通过Eureka服务注册,因为它本身就是Eureka服务
  5. 每次服务注册需要30s的时间才能显示在eureka服务中,因为eureka需要从服务接收3次连续心跳包ping,每次心跳包ping间隔10s,然后才能使用这个服务。
1.3 添加注解

在建立eureka服务时, 需要进行的最后一项工作是在服务启动类上添加注解

@SpringBootApplication
@EnableEurekaServer---Spring服务中启用Eureka服务器
public class EurekaServerApplication {
    public static void main(String[] args) {
    	SpringApplication.run(EurekaServerApplication.class, args);
    }
}
2.eureka-client 2.1pom文件

构建eureka-client服务的时候,在pom文件中引入相应坐标

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
  1. spring-cloud-starter-netflix-eureka-client:eureka客户端包
  2. spring-boot-starter-web:spring的web包,需要引入此包,才能注册成功
2.2配置文件
eureka:
  instance:
    prefer-ip-address: true ⇽--- 注册服务的IP,
  client:
    register-with-eureka: true ⇽---向Eureka注册服务
    fetch-registry: true ⇽---拉取注册表的本地副本
    service-url:
      defaultZone: http://localhost:8761/eureka ⇽---Eureka服务的位置
2.3添加注解
@SpringBootApplication
@EnableDiscoveryClient---eureka客户端注解
public class EurekaClient1Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClient1Application.class, args);
    }

}
3.注册成功

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/742241.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-28
下一篇2022-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存