【java深入学习第4章】掌握 Java 微服务:Spring Boot 和 Spring Cloud 的关键技术及设计原则

免费多模型AI网站,支持豆包、GPT-4o、谷歌Gemini等AI模型,无限制使用,快去白嫖👉海鲸AI🔥🔥🔥

在现代软件开发中,微服务架构因其灵活性和可扩展性而备受青睐。本文将探讨Java微服务架构中的关键技术和设计原则,并通过Spring Boot和Spring Cloud提供代码示例,展示如何构建一个简单的微服务应用。

关键技术和设计原则
  1. 服务拆分:将单体应用拆分为多个独立的微服务,每个服务负责特定的业务功能。
  2. 独立部署:每个微服务可以独立部署和扩展,减少了服务之间的耦合。
  3. API网关:提供统一的入口,管理和路由请求到相应的微服务。
  4. 服务发现:动态发现微服务实例,支持负载均衡和故障转移。
  5. 配置管理:集中管理微服务的配置,支持配置的动态更新。
  6. 容错和监控:实现服务的高可用性和可观测性,确保系统的稳定运行。
使用Spring Boot和Spring Cloud构建微服务

我们将构建一个简单的微服务应用,包括以下组件:

  1. 服务注册中心(Eureka Server)
  2. 配置中心(Spring Cloud Config Server)
  3. API网关(Spring Cloud Gateway)
  4. 两个业务微服务
1. 服务注册中心(Eureka Server)

创建一个Spring Boot项目,添加以下依赖:

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

在主类中启用Eureka Server:

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

application.yml中配置Eureka Server:

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
2. 配置中心(Spring Cloud Config Server)

创建一个Spring Boot项目,添加以下依赖:

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

在主类中启用Config Server:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

application.yml中配置Config Server:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/config-repo
3. API网关(Spring Cloud Gateway)

创建一个Spring Boot项目,添加以下依赖:

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

application.yml中配置API网关:

server:
  port: 8080

spring:
  cloud:
    gateway:
      routes:
        - id: service1
          uri: lb://SERVICE1
          predicates:
            - Path=/service1/**
        - id: service2
          uri: lb://SERVICE2
          predicates:
            - Path=/service2/**

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
4. 业务微服务

创建两个Spring Boot项目,分别命名为service1service2,并添加以下依赖:

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

service1的主类中:

@SpringBootApplication
@EnableEurekaClient
public class Service1Application {
    public static void main(String[] args) {
        SpringApplication.run(Service1Application.class, args);
    }
}

service2的主类中:

@SpringBootApplication
@EnableEurekaClient
public class Service2Application {
    public static void main(String[] args) {
        SpringApplication.run(Service2Application.class, args);
    }
}

service1service2application.yml中分别配置:

spring:
  application:
    name: service1
  cloud:
    config:
      uri: http://localhost:8888

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: service2
  cloud:
    config:
      uri: http://localhost:8888

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

service1service2中分别创建一个简单的REST控制器:

@RestController
@RequestMapping("/service1")
public class Service1Controller {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Service 1";
    }
}
@RestController
@RequestMapping("/service2")
public class Service2Controller {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Service 2";
    }
}
运行和测试
  1. 启动Eureka Server。
  2. 启动Config Server。
  3. 启动API网关。
  4. 启动Service1和Service2。

访问以下URL进行测试:

  • http://localhost:8080/service1/hello 应返回 “Hello from Service 1”
  • http://localhost:8080/service2/hello 应返回 “Hello from Service 2”

通过以上步骤,我们成功构建了一个简单的微服务架构应用,展示了如何使用Spring Boot和Spring Cloud实现服务注册、配置管理和API网关等关键功能。希望这篇文章对你理解和实践Java微服务架构有所帮助。

免费多模型AI网站,支持豆包、GPT-4o、谷歌Gemini等AI模型,无限制使用,快去白嫖👉海鲸AI🔥🔥🔥

  • 21
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值