第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

SpringCloud微服務(wù)學(xué)習(xí):從入門到實(shí)踐

概述

本文详细介绍了SpringCloud微服务学习的基础知识,涵盖环境搭建、核心组件及实战案例。文章还深入讲解了SpringCloud的配置与部署方法,并提供了相关的配置示例。希望通过这些内容,读者能够快速掌握SpringCloud微服务开发的关键技能。

SpringCloud简介与环境搭建

什么是SpringCloud

SpringCloud是一系列框架的有序集合,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如配置中心、服务发现、断路器、路由、微代理、集群状态管理等。SpringCloud为开发人员提供了快速构建分布式系统的工具,简单的接口和抽象使得分布式系统更容易实现。

必要的开发环境与工具介绍

  • Java环境:SpringCloud基于Spring Boot,因此需要JDK 1.8及以上版本。
  • IDE:推荐使用IntelliJ IDEA或Eclipse。
  • 构建工具:Maven或Gradle。
  • 版本控制:Git。
  • 容器化部署:Docker和Docker Compose。
  • API测试工具:Postman。

快速搭建SpringBoot项目

使用Spring Initializr快速创建Spring Boot项目。

  1. 访问Spring Initializr
  2. 选择Maven项目类型,Java版本为1.8,Spring Boot版本为2.5.4。
  3. 设置项目元数据,如Group为com.example,Artifact为hello
  4. 选择技术依赖,如Spring Web
  5. 下载并导入到IDE中。
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
微服务架构基础

微服务架构的基本概念

微服务架构是一种将大型复杂应用拆分为小型服务的方法,每个服务负责特定的业务功能,可以独立开发、部署和运行。

微服务与传统单体应用的区别

  • 可伸缩性:微服务可以按需进行扩展,而单体应用需要整体扩展。
  • 部署频率:微服务支持更频繁的部署,而单体应用的部署周期较长。
  • 维护性:微服务易于维护和更新,而单体应用则更复杂。
  • 技术栈:微服务可以使用不同的技术栈,而单体应用通常使用单一技术栈。
  • 团队规模:微服务支持小团队独立开发,而单体应用可能需要大型团队协同开发。
SpringCloud核心组件介绍

Eureka服务注册与发现

Eureka是一个基于REST的服务,用于服务注册与发现。它是一个服务注册表,可以用来管理服务实例,并提供服务发现和负载均衡功能。

  1. 服务提供者注册到Eureka
  2. 服务消费者从Eureka获取服务列表
# eureka-server.yml
spring:
  application:
   name: eureka-server
 eureka:
   instance:
      hostname: localhost
   client:
      register-with-eureka: false
      fetch-registry: false
      server: true
# eureka-client.yml
spring:
 application:
   name: eureka-client
 eureka:
   instance:
      hostname: localhost
   client:
      register-with-eureka: true
      fetch-registry: true
      service-url:
         defaultZone: http://localhost:8761/eureka/

Ribbon负载均衡

Ribbon是Netflix开发的一个基于HTTP和TCP的客户端负载均衡工具,提供了一系列配置项,如轮询、随机、最少活跃请求数等,用于控制对后端多个服务实例的调用。

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Feign声明式服务调用

Feign是Netflix开源的一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加简单。Feign允许开发者定义接口而不用关心接口实现,它会处理HTTP请求和响应的细节。

@FeignClient(value = "eureka-client")
public interface HelloService {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String hello();
}
实战案例:构建简单的微服务应用

创建服务提供者与消费者

服务提供者暴露服务,服务消费者调用服务。

服务提供者

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

@RestController
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello World";
    }
}

服务消费者

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

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return helloService.hello();
    }
}

使用Eureka注册服务

服务提供者和消费者需要注册到Eureka服务注册中心。

实现服务间的调用

服务消费者通过Feign或RestTemplate调用服务提供者。

配置与部署

配置中心SpringCloud Config

SpringCloud Config为微服务提供集中化的外部配置,可以基于Git等存储配置文件,并通过Spring Cloud Config Server提供配置中心。

配置中心服务器端

spring:
  application:
    name: config-server
 cloud:
    config:
       server:
          git:
             uri: https://github.com/your-repo

配置中心客户端

spring:
  cloud:
    config:
       name: application
       profile: dev
       label: master
       password: yourPassword
       username: yourUsername
       server:
          host: localhost
          port: 8888

服务网关SpringCloud Gateway

SpringCloud Gateway是基于Spring Boot 2.0开发的一个基于Spring WebFlux上的API网关。它基于Spring Framework 5 Actuator和Project Reactor。

@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
    return builder.routes()
        .route(r -> r.path("/api/**")
            .uri("lb://SERVICE"))
        .build();
}

微服务的打包与部署

使用Docker进行微服务的打包与部署。

  1. Dockerfile

    FROM openjdk:8-jdk-alpine
    VOLUME /tmp
    ADD target/*.jar app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
  2. Docker Compose配置
    version: '3.7'
    services:
    provider:
    build: ./provider
    ports:
      - "8080:8080"
    consumer:
    build: ./consumer
    ports:
      - "8081:8081"
日志与监控

使用SpringCloud Sleuth进行日志跟踪

Sleuth用于生成唯一ID来关联所有微服务之间的请求,从而进行日志跟踪。

@Configuration
public class SleuthConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

SpringCloud Zipkin监控

Zipkin是一个分布式追踪系统,可以帮助开发者查看在生产环境中运行的分布式应用程序的数据流,从而快速发现和解决问题。

Zipkin服务端配置

spring:
  application:
    name: zipkin-server
 server:
    port: 9411
 zipkin:
    storage:
       type: elasticsearch

Zipkin客户端配置

spring:
 cloud:
    sleuth:
       sampler:
          probability: 1.0
 zipkin:
    baseUrl: http://localhost:9411

通过以上步骤和示例代码,我们已经详细介绍了SpringCloud微服务的基础知识、核心组件、实战案例以及配置与部署,希望对初学者有所帮助。

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消