首页 > 其他分享 >Eureka+SpringCloud微服务(入门一)

Eureka+SpringCloud微服务(入门一)

时间:2023-01-12 21:46:32浏览次数:53  
标签:服务 入门 SpringCloud eureka 注册 Eureka public cloud

项目地址

[CloudStudy项目](zko0/cloudStudy: Springcloud学习工程 (github.com))

1.依赖版本

  • cloud:Hoxton.SR1
  • boot:2.2.2.RELEASE
  • cloud alibaba 2.1.0RELEASE
  • Java 8
  • Maven>3.5
  • Mysql>5.7

image-20230111135900417

2.工程编写

  1. 创建父工程
  2. 编写payment工程

1.热部署插件(不推荐使用)

  1. 添加依赖

    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
  2. 添加插件到Pom.xml

    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.4.1</version>
          <configuration>
            <addResources>true</addResources>
          </configuration>
        </plugin>
      </plugins>
    </build>
    
  3. 修改Idea设置

    image-20230111170757202

  4. 配置,(我使用的为idea2022,2021.2 之前方法不同,需要百度)

    image-20230111171331167

  5. image-20230111172354560

2.RestTemplate

提供多种便携访问远程Http服务的方法,是一种简单辨析的restful服务模板类。Spring提供的用于访问Rest服务端客户端模板工具集。

@Configuration
public class ApplicationContextConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
@RestController
@Slf4j
public class OrderController {
    private static final String PAYMENT_URL="http://localhost:8001";
    @Resource
    private RestTemplate restTemplate;
   
   @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

剩余部分在阳哥CLoud2.0的前15P,自行编写。

3.服务注册中心

1.Eureka单机

一.EurekaServer

image-20230112102031919

新建项目cloud-eureka-server7001

pom.xml:

<dependencies>
    <!--eureka server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!--引入自己定义的api通用包,可以使用payment支付Entity-->
    <dependency>
        <groupId>cn.zko0.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</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>

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/

入口类:

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

二.EurekaClient-Provider

在provider项目上添加eureka-client依赖

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

在入口类加入Enable

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

配置文件添加配置:

这里的service-url需要参考Server中配置文件的service-url

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

测试:

Eureka自我保护机制(暂时忽略)

image-20230112104638495

其中Eureka注册服务名称为Spring.application.name名称

image-20230112104813448

image-20230112104828106

三.EurekaClient-Consuemr

1.添加eureka-client依赖:

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

2.入口类添加Enable

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

3.配置文件添加配置信息

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

4.测试

image-20230112105839224

eureka注册开关:

image-20230112105815024

2.Eureka集群+LoadBalanced服务调用

image-20230112134801765

1.创建Eureka-Server7002

cloud-eureka-server7002

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

2.相互注册,相互守望

修改hostname,不能7001和7002都为localhost

修改配置文件:

image-20230112132914574

127.0.0.1       eureka7001.com
127.0.0.2       eureka7002.com
127.0.0.3       eureka7003.com

修改项目配置文件,相互注册,相互守望

cloud-eureka-server7002的pom.xml文件修改:

server:
  port: 7002

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

cloud-eureka-server7001的pom.xml文件修改:

server:
  port: 7001

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

3.测试集群

image-20230112134002298

4.将微服务配置进集群

将payment和order项目的配置文件修改service-url的地址

image-20230112134520698

5.测试微服务的配置

image-20230112134735398

6.多个提供者集群搭建

新建cloud-provider-payment-8002

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

pom.xml:

server:
  port: 8002

提取port信息到controller,方法后面方法调用显示对应的信息:

image-20230112140300301

简单测试服务是否注册:

image-20230112140834452

order调用只会调用8001,因为在order中已经写死了调用地址

image-20230112141018298

修改Controller调用地址

image-20230112141246244

重启Order调用,出错:

image-20230112141527611

只有@LoadBalanced修饰的restTemplate才能通过服务名调用,否则只能通过ip+port调用

image-20230112141801464

7.最后的测试

image-20230112143130008

4.细节完善

1.去除服务主机名

eureka可以看到主机名,让它不显示

image-20230112143635605

修改8001和8002的application.yml:

image-20230112143837800

image-20230112143919301

测试:

image-20230112144119002

2.鼠标悬停显示Ip+port

如果你已经显示了,则不需要配置(我的项目悬停默认显示Ip和端口号)

image-20230112144437439

测试:

image-20230112144529987

5.服务发现

能够通过接口调用,获取到Eureka上的所有服务信息

在provider-payment-8001上添加discovery()方法:

@GetMapping(value = "/payment/discovery")
public Object discovery() {
    //获取在Eureka中注册的services有哪些
    List<String> services = discoveryClient.getServices();
    for (String element:services) {
        log.info("*****element:{}",element);
    }
    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    for (ServiceInstance instance : instances) {
        log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
    }
    return this.discoveryClient;
}

在启动类上添加注解:

image-20230112151339981

测试:

image-20230112151648378

image-20230112151705320

6.Eureka自我保护机制

1.保护模式现象

Eureka进入保护模式:

image-20230112151759252

2.保护模式是什么?

Eureka Server会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何的微服务。

翻译成人话:某个微服务不可用,Eureka不会立刻清理,依然会对该微服务的信息进行保存

CAP中的AP

这句话没搞懂,后面慢慢学

3.为什么需要保护模式

默认情况下,EurekaServer在一段事件里没有接收到某个微服务实例的心跳,EurekaServer会注销该实例。(90s)

但是当网络分区故障,EurekaServer和微服务之间无法正常通信了,上面的行为就会很危险。

因为这时:微服务是健康的(这个微服务不应该被注销掉

Eureka自我保护模式如何启动:当EurekaServer节点短时间内有很多Client丢失,那么这个节点就会进入保护模式。

4.设计理念

宁可保留错误的服务注册信息,也不盲目的注销掉任何健康的服务实例。

5.如何关闭自我保护

Eureka默认开启自我保护机制

首先关闭集群搭建,只开启7001

image-20230112154111174

Eureka-Server-7001关闭自我保护模式,同时将心跳时间修改为2s(2s内没有收到就销毁)

image-20230112154301586

Eureka-Client关闭自我保护:

image-20230112172201705

测试:自我保护模式已关闭

image-20230112154542021

image-20230112172425210

这时关闭8001项目,Eureka会立马删除服务:

image-20230112172500849

标签:服务,入门,SpringCloud,eureka,注册,Eureka,public,cloud
From: https://www.cnblogs.com/zko0/p/17048001.html

相关文章