Spring Boot实现Eureka注册功能

一、新建两个子模块,我这边的命名Eureka和Client,在父级的pom.xml

<!-- 集成web方式的开发 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服务与注册中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

二、在Eureka中新建package、Application服务需要在包下才能正常加载启动,根目录下启动会报异常

package eureka;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer --作为服务端注解
public class EurekaServer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServer.class).web(true).run(args);
    }
}

Eureka下的application.yml

server:
  port: 9001
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false #不作为服务注册
    fetch-registry: false #单点服务不需要同步其他服务数据
    service-url:
      defaultZone: http://localhost:9001/eureka/ #服务注册地址

三、Client下新建package和Application

package redis;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient --作为客户端注解
public class RedisServer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(RedisServer.class).web(true).run(args);
    }
}

Client下application.yml

server:
  port: 9009
spring:
  application:
    name: springCloud-redis
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:9001/eureka/

四、启动Eureka、Client,浏览器访问http://localhost:9001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值