springColde常用组件

2021年3月14日 10点热度 0条评论 来源: 马希望

1.注册中心Eureka

1.1.什么是注册中心Eureka

Eureka 是 Netflix 开发的服务发现组件,本身是一个基于 REST 的服务。Spring Cloud将它集成在其子项目 spring-cloud-netflix 中,以实现 Spring Cloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。

1.2.Eureka三大角色

Eureka Server:注册中心
​ 通过 Register、Get、Renew 等接口提供服务的注册和发现。
Service Provider:服务提供方
​ 把自身的服务实例注册到 Eureka Server 中
Service Consumer:服务调用方
​ 通过 Eureka Server 获取服务列表,消费服务。

.1.3.创建工程springcloud_eureka_server

1.3.1.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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <groupId>com.usian</groupId>
    <artifactId>springcloud_eureka_server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- springBoot的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

1.3.2.App

package com.usian;

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

@EnableEurekaServer//EurekaServer服务器端启动类,接受其他微服务注册进来
@SpringBootApplication
public class EurekaApp { 
	public static void main(String[] args) { 
		SpringApplication.run(EurekaApp.class, args);
	}
}

1.3.3.application.properties

server.port=8761
#设置eureka不注册自己
eureka.client.register-with-eureka=false
#设置eureka不获取其他服务的注册信息
eureka.client.fetch-registry=false

1.4.1.创建工程springcloud_eureka_provider

1.4.2.提供者配置文件application.properties

#向注册中心注册的名字
spring.application.name=eureka-provider
server.port=8777
#设置注册中心的地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/

1.4.3.提供者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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <groupId>com.usian</groupId>
    <artifactId>springcloud_eureka_provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- springBoot的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka-server客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.usian</groupId>
            <artifactId>springcloud_common_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

1.4.4.App

@EnableEurekaClient//允许向服务端注册服务
@SpringBootApplication
public class ProviderApp { 
	public static void main(String[] args) { 
		SpringApplication.run(ProviderApp.class, args);
	}
}

1.5.1.创建工程springcloud_eureka_consumer

1.5.2.消费者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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <groupId>com.usian</groupId>
    <artifactId>springcloud_eureka_consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- springBoot的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka-server客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.usian</groupId>
            <artifactId>springcloud_common_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

1.5.3.App

package com.usian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
//允许向注册中心注册该服务,并可以获取其他服务的调用地址
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApp { 
    public static void main(String[] args) { 
        SpringApplication.run(ConsumerApp.class, args);
    }
}

1.5.4.消费者配置文件application.properties

#注册中心挂个名字
spring.application.name=eureka-consumer
server.port=8666
#设置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
    原文作者:马希望
    原文地址: https://blog.csdn.net/weixin_50604126/article/details/114801146
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。