手把手教你搭建SpringCloud项目(三)集成Eureka服务注册中心

Spring Cloud全文目录

源码

什么是微服务?有手就行

SpringCloud简介与5大常用组件

一、手把手教你搭建SpringCloud项目(一)搭建Maven父工程,傻瓜式操作

二、手把手教你搭建SpringCloud项目(二)生产者与消费者

三、手把手教你搭建SpringCloud项目(三)集成Eureka服务注册中心

四、手把手教你搭建SpringCloud项目(四)EurekaServer集群版搭建

五、手把手教你搭建SpringCloud项目(五)生产者集群版搭建

六、手把手教你搭建SpringCloud项目(六)actuator微服务信息完善与Eureka自我保护机制

七、手把手教你搭建SpringCloud项目(七)Eureka实现服务发现(Discovery)

八、手把手教你搭建SpringCloud项目(八)集成Consul服务注册中心

九、手把手教你搭建SpringCloud项目(九)集成Ribbon负载均衡器

十、手把手教你搭建SpringCloud项目(十)集成OpenFeign服务接口调用

十一、手把手教你搭建SpringCloud项目(十一)集成Hystrix之服务降级

十二、手把手教你搭建SpringCloud项目(十二)集成Hystrix之服务熔断

十三、手把手教你搭建SpringCloud项目(十三)集成Hystrix之图形化Dashboard实时监控

十四、手把手教你搭建SpringCloud项目(十四)集成Gateway新一代服务网关

十五、手把手教你搭建SpringCloud项目(十五)集成Config分布式配置中心

十六、手把手教你搭建SpringCloud项目(十六)集成Bus消息总线

十七、手把手教你搭建SpringCloud项目(十七)集成Stream消息驱动

十八、手把手教你搭建SpringCloud项目(十八)集成Sleuth分布式链路跟踪

文章持续更新中,欢迎关注!
我们首先了解 Eureka:官方介绍

了解Eureka

1、什么是服务治理?

Springcloud 封装了Netfix公司开发的Eureka模块来实现服务治理。在传统的RPC远程调用中,管理每个服务于服务之间依赖关系复杂,管理复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。

2、什么是服务注册与发现?

Eureka采用了CS的设计架构,Eureka Server服务端作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用Eureka Client客户端连接到Eureka Server服务端并维持心跳的连接,这样系统的维护人员就可以通过ureka Server来监控系统中的各个微服务是否正常运行。

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的的信息,比如:服务地址通讯等以别名方式注册到注册中心上,另一方(消费者|生产者)以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用和RPC远程调用框架核心设计思想。这里的重点就是注册中心,因为注册中心管理每个服务于服务之间的依赖关系。在任何的RPC框架中,都会有一个注册中心,存放服务地址相关信息,也就是接口地址。

下图为Eureka的架构图:
在这里插入图片描述

搭建Eureka服务端微服务模块

1.建立module: springcloud-eureka-server7001

在这里插入图片描述

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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.dyh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-server7001</artifactId>


    <dependencies>
        <!--eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--引入自己定义的api通用包,可以使用payment支付Entity-->
        <dependency>
            <groupId>com.dyh.springcloud</groupId>
            <artifactId>springcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

3.application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


4.主启动类 EurekaMain7001

package com.dyh.springcloud;

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

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

这是个服务注册中心,主要干的活就是服务注册,不需要写业务类。
但是注意:Eureka有两个组件,一定要标清楚哪个是Server,哪个是Client。@EnableEurekaServer代表服务注册中心

5.启动7001服务,测试

在这里插入图片描述
出现上面图标,表示Eureka 服务端安装成功。No instances available表示当前没有服务注册进来

将生产者支付微服务8001注册到eurekaServer中

1.将 Eureka-client 依赖引入,便于使用注解@EnableEurekaClient标注这是个Eureka Client端

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

2.在application.yml添加Eureka相关配置

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/

3.主启动类添加注解@EnableEurekaClient

package com.dyh.springcloud;

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

@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }

}

4.启动7001、8001服务,测试

注意: 要先启动EurekaServer
在这里插入图片描述

将消费者订单微服务81注册到eurekaServer中

1.将 Eureka-client 依赖引入,便于使用注解@EnableEurekaClient标注这是个Eureka Client端

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

2.在application.yml添加Eureka相关配置

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/

3.主启动类添加注解@EnableEurekaClient

package com.dyh.springcloud;

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

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

4.启动7001、8001、81服务,测试

注意: 要先启动EurekaServer
在这里插入图片描述
cloud-order-server服务以入住,查询功能也可以正常执行
在这里插入图片描述
我们成功的将生产者和消费者成功注册到了注册中心, 也可以通过接口获取信息。so easy。

在这里插入图片描述

下一篇文章继续搭建集群版的Eureka,持续关注、点赞。我们持续更新中。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值