初识Spring Cloud(二)快速搭建Spring Cloud

本文介绍如何搭建SpringCloud Eureka注册中心及其客户端,包括配置依赖、启动类及YAML配置等步骤,实现服务注册与发现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,创建SpringCloud项目

创建完成之后的pom.xml

<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>

application.yml文件:

server:
  port: 8761  #指定端口号

eureka:
  instance:
    hostname: localhost #指定eureka注册中心地址
  client:
    registerWithEureka: false #是否把自己注册到eureka 默认是 true
    fetchRegistry: false      #是否从uereka server 中获取信息
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

然后启动项目.看看效果 http://localhost:8761/

二,创建Client 项目

创建项目如上相同,不贴图了

pom.xml的依赖

<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>

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ #使用eureka的注册中心地址
server:
  port: 8762  #端口号
spring:
  application:
    name: service-client #注册成功的服务名称

启动类

启动项目

打开刚才启动的springcloud 注册中心的网页 http://localhost:8761/

打开新窗口 输入 http://localhost:8762/hi?name=cloud

可以按照上述方式多创建几个项目.

一个简单的Spring Cloud 就搭建成功了.本人正在学习Spring Cloud的一切.会不定期更新一些.自己学到的东西.欢迎大家多多点评.

后面会更新RIBBON方式调用其它服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值