springboot集成eureka注册

1.导入依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

配置文件中

eureka:
  instance:
    hostname: eureka7001.com
  client:
    fetch-registry: false
    service-url:
      #集群指向其它eureka
      defaultZone: http://eureka7002.com:7002/eureka/
      #单机就是7001自己
    register-with-eureka: false
#      defaultZone: http://eureka7001.com:7001/eureka/
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
       SpringApplication.run(EurekaMain7001.class,args);
    }
}

2.再创建一个 mudel (相同部分省掉)

server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com
  client:
    fetch-registry: false
    service-url:
      #集群指向其它eureka
      defaultZone: http://eureka7001.com:7001/eureka/
      #单机就是7001自己
    register-with-eureka: false
#      defaultZone: http://eureka7001.com:7001/eureka/
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
实现注册互看 联通



在hosts文件中添加

127.0.0.1 eureka7001.com

127.0.0.1 eureka7002.com

127.0.0.1 eureka7003.com

3.消费者调用
首先导入配置文件

<dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                </dependency>

2。进行注册

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true

#      单机版
#      defaultZone: http://localhost:7001/eureka

在启动类上添加 @EnableEurekaClient 注解

package com.atguigu.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig
{
    @Bean
//    集群配置
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地集成Eureka,实现服务注册和发现。以下是集成步骤: 1. 添加Eureka依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> ``` 2. 配置Eureka Server 在Spring Boot应用的配置文件中添加以下配置: ``` server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false ``` 其中,server.port指定Eureka Server的端口号,eureka.client.register-with-eurekaeureka.client.fetch-registry设置为false表示该应用不会向Eureka Server注册自己,也不会从Eureka Server获取服务列表。 3. 启动Eureka Server 在Spring Boot应用的启动类上添加@EnableEurekaServer注解,启动Eureka Server。 ``` @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ``` 4. 配置Eureka Client 在Spring Boot应用的配置文件中添加以下配置: ``` server.port=808 spring.application.name=my-service eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ ``` 其中,server.port指定应用的端口号,spring.application.name指定应用的名称,eureka.client.service-url.defaultZone指定Eureka Server的地址。 5. 启动Eureka Client 在Spring Boot应用的启动类上添加@EnableDiscoveryClient注解,启动Eureka Client。 ``` @SpringBootApplication @EnableDiscoveryClient public class MyServiceApplication { public static void main(String[] args) { SpringApplication.run(MyServiceApplication.class, args); } } ``` 6. 测试 启动Eureka Server和Eureka Client后,可以在Eureka Server的管理界面(http://localhost:8761/)上看到注册的服务。可以通过Eureka Client的应用名称访问该服务,例如:http://localhost:808/hello。 以上就是Spring Boot集成Eureka的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值