spring cloud 注册中心之Eureka Server和Client的搭建与使用

2022-09-01 10:38:20

一、server 端搭建

1.配置maven 依赖

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

2.启动类上添加开启注解@EnableEurekaServer

package springcloud.helloworld.eureka.server;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

3.配置

server:
   port: 8761

eureka:
   instance:
       hostname: localhost
   client:
       registerWithEureka: false
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

二、client端配置

1.添加maven 依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 添加以下web依赖是为了确保应用启动后以web应用的方式长时间存在,否则应用启动成功后即立刻正常退出-->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.启动类上添加启用注解@EnableEurekaClient

3.配置注册中心url地址和实例名称

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

spring:
    application:
        name: service-helloworld

三、启动与验证

分别启动server 与cliennt,在浏览器中访问服务器端口, 可以看到Service Helloworld已经自动注册到之前的server中

  • 作者:万道归原始
  • 原文链接:https://blog.csdn.net/tmdcda/article/details/88787500
    更新时间:2022-09-01 10:38:20