Spring Cloud工程搭建及Docker容器部署

本文介绍如何使用SpringCloud搭建微服务架构,包括Eureka服务发现、客户端注册与配置。通过实例演示了微服务项目的初始化过程及其核心组件的配置。

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

微服务搭建及部署

基于Spring Cloud 搭建微服务项目,目前我们用到的微服务组件有EurekaHystrixRibbonZuul
初始化maven结构的Spring Boot 项目,可以进http://start.spring.io/,需要用什么组件直接选择就行。目前主流IDE(STS(eclipse)、Intellij Idea等)都已经集成了这个功能。
下面的例子就是从Idea初始化的目录结构开始,如下图

├─.mvn
│  └─wrapper
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─example
│  │  │          └─demo
│  │  └─resources
│  │      ├─static
│  │      └─templates
│  └─test
│      └─java
│          └─com
│              └─example
│                  └─demo
└─target
    ├─classes
    │  ├─com
    │  │  └─example
    │  │      └─demo
    │  └─templates
    └─test-classes
        └─com
            └─example
                └─demo

Eureka 服务端

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • 然后在Spring Boot入口启动类EurekaServerApplication加入Eureka客户端注解@EnableEurekaClient
package com.example.seven;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  • Eureka Server 配置applications.yml
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      default-zone: http://localhost:${server.port}/eureka/

Eureka 客户端1

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 然后入口启动类加入Eureka客户端注解@EnableEurekaClient注解
package com.example.seven;

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

@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(BussinessApplication.class, args);
    }
}
  • Eureka Client配置applications.yml
server:
  port: 8762
spring:
  application:
    name: microservice-bussiness
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

Eureka 客户端2

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 然后入口启动类加入Eureka客户端注解@EnableEurekaClient注解
package com.example.seven;

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

@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(BussinessApplication.class, args);
    }
}
  • Eureka Client配置applications.yml
server:
  port: 8763
spring:
  application:
    name: microservice-bussiness
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
        <!-- 这里是我遇到的一大坑,项目可以正常启动没有任何问题,但是没有注册到服务中心 -->
<!--         <dependency> -->
<!--             <groupId>org.springframework.cloud</groupId> -->
<!--             <artifactId>spring-cloud-netflix-eureka-client</artifactId> -->
<!--         </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

相继启动Eureka Server、Client1、Client2 三个工程
访问localhost:8761
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sdm_seven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值