springcloud-01-eureka注册中心

1、创建Eureka-Server服务端

引入项目依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

启动类上开启注册中心注解:将项目作为SpringCloud中的注册中心
@EnableEurekaServer
public class EurekaServerAppRun {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerAppRun.class, args);
    }
}
配置文件:
spring:
  application:
    name: eureka-server #指定服务名称
server:
  port: 7001 #指定运行端口
eureka:
  instance:
    hostname: localhost #指定主机地址
  client:
    fetch-registry: false #表示是否将自己注册到Eureka Server -注册中心不需要开启
    register-with-eureka: false #表示是否从Eureka Server获取注册的服务信息 -注册中心不需要开启
  server:
    enable-self-preservation: false #关闭保护模式
启动服务:进入Eureka注册中心的界面
http://localhost:7001/

在这里插入图片描述

2、创建Eureka-Client端

引入依赖:
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
启动类注解:@EnableDiscoveryClient 表示这是一个Eureka客户端,另外从Spring Cloud Edgware开始@EnableDiscoveryClient@EnableEurekaClient 可省略,只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientAppRun {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientAppRun.class, args);
    }
}
配置文件:客户端需要向注册中心进行注册,并且需要配置 注册中心的 地址
spring:
  application:
    name: eureka-client #服务名称
server:
  port: 7002 #运行端口号
eureka:
  client:
    register-with-eureka: true #表示是否将自己注册到Eureka Server
    fetch-registry: true #表示是否从Eureka Server获取注册的服务信息
    service-url:
      defaultZone: http://localhost:7001/eureka/ #配置注册中心地址

启动服务:此时客户端服务已经被注册中心集成进去

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值