SpringCloud-1.搭建微服务demo

本文详细介绍了如何使用SpringBoot 2.1.5.RELEASE和SpringCloud GreenwichSR1搭建微服务架构,包括配置Eureka Server作为服务注册中心,以及如何让Eureka Client注册到该中心。

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

0.开发环境

  • jdk1.8
  • idea
  • SpringBoot 2.1.5.RELEASE
  • SpringCloud >Greenwich SR

1.使用idea新建一个空的maven工程

在这里插入图片描述

1.1.修改pom.xml添加以下内容
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich SR1</spring-cloud.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-build-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.eureka-server

2.1 新建module

在这里插入图片描述

2.2 选择SpringInitializr建一个SpringBoot项目

在这里插入图片描述

2.3 添加eureka-server依赖

在这里插入图片描述

2.4 启动类添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
2.5 修改application.yml
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.6 启动eureka-server

在浏览器中输入127.0.0.1:8761
在这里插入图片描述

3.添加eureka-client

3.1添加moudle

步骤与前面eureka-server一致,区别就说选择依赖的时候选择client
在这里插入图片描述

3.2 启动类添加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
3.3修改application.yml
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  server:
    port: 8762
spring:
  application:
    name: eureka-client
3.4启动eureka-client

刷新刚刚的网页
在这里插入图片描述
服务被注册进来了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值