Spring Cloud入门探索

Spring Cloud

使用为服务的优势

Spring Cloud 基于 Spring Boot 使得整体的开发、配置、部署都非常方便,Spring Cloud 相当于微服务各个组件的整合。

Spring Boot 和 Spring Cloud 的关系

Spring Boot 快速搭建基础系统,Spring Cloud 在此基础上实现分布式系统中的公共组件,服务间的调用基于 REST 结构。

  • 服务提供者
  • 服务消费者
  • 注册中心

三者之间的关系

1、启动注册中心。

2、服务提供者启动时,在注册中心注册可以提供的服务。

3、服务消费者启动时,在注册中心订阅需要调用的服务。

4、注册中心将服务提供者的信息推送给服务调用者。

5、服务调用者通过相关信息(IP、端口)调用服务提供者的服务。

Eureka组件

Spring Cloud 的服务治理可以使用 Eureka 组件。
Spring Cloud Eureka 主要包含了服务端和客户端组件:Eureka Server 服务端,Eureka Client 客户端

具体实现过程

创建maven工程

在这里插入图片描述

添加pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.smartaotao</groupId>
    <artifactId>20190309</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eurekaserver</module>
        <module>provider</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- JDK9以上版本需要配置如下内容 -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <!-- 管理Spring Cloud依赖 -->
    <dependencyManagement>
        <dependencies>
            <!-- 组件 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

在父工程下创建一个 Module,实现 Eureka Server,并添加其pom依赖

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

在 resources 路径下创建配置文件 application.yml,添加 Eureka Server 相关配置

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/

创建启动类 Application

package com.smartaotao;

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

/**
 * @author SMARTAOTAO
 * @create 2020-03-09 21:29
 */
@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

注意:注解(细节决定成败)

创建服务提供者

添加pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>20190309</artifactId>
        <groupId>com.smartaotao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

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

</project>

resources/application.yml

server:
  port: 8010
spring:
  application:
    name: provider
eureka:
  client:
    service-url: 
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

创建 Application

package com.smartaotao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author SMARTAOTAO
 * @create 2020-03-09 22:08
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

RestTemplate

RestTemplate 可以实现不同微服务之间的调用。

服务消费者 -----》 服务提供者

服务治理

  • 注册中心 Eureka Server
  • 服务注册 Eureka Client (服务提供者、服务消费者)
  • 服务发现 消费者调用提供者的服务(RestTemplate)
	@Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
发布了10 篇原创文章 · 获赞 0 · 访问量 127

猜你喜欢

转载自blog.csdn.net/weixin_38425605/article/details/104796533