【无标题】

转载@paper

Spring Cloud服务注册-Eureka介绍和部署

1、Spring-Cloud Euraka介绍

Spring-Cloud Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的,同样,Spring-Cloud 还集成了Zookeeper和Consul。

 

在项目中使用Spring Cloud Euraka的原因是它可以利用Spring Cloud Netfilix中其他的组件,如zull等,因为Euraka是属于Netfilix的。

 

2、Euraka介绍

Eureka由多个instance(服务实例)组成,这些服务实例可以分为两种:Eureka Server和Eureka Client。为了便于理解,我们将Eureka client再分为Service Provider和Service Consumer。

 

Eureka Server 提供服务注册和发现

Service Provider 服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到

Service Consumer服务消费方,从Eureka获取注册服务列表,从而能够消费服务

 

3. Eureka与Zookeeper比较

P:Partition tolerance,网络分区容错。类似多机房部署,保证服务稳定性。

A: Availability,可用性。

C:Consistency ,一致性。

CAP定理:CAP三个属性对于分布式系统不同同时做到。如AP/CP/AC。再来看Zookeepr区别:

(1)Zookeeper是CP,分布式协同服务,突出一致性。对ZooKeeper的的每次请求都能得到一致的数据结果,但是无法保证每次访问服务可用性。如请求到来时,zookeer正在做leader选举,此时不能提供服务,即不满足A可用性。

 

(2)Eureka是AP,高可用与可伸缩的Service发现服务,突出可用性。相对于Zookeeper而言,可能返回数据没有一致性,但是保证能够返回数据,服务是可用的。

 

4. 项目搭建

在这里插入图片描述

spring_cloud:为总项目(父工程)

eureka-server:注册中心的服务(启动注册中心)

user-consumer:消费者(通过注册中心去访问提供者的API接口)

user-provider:提供者(提供访问接口到注册中心)

 

spring_cloud(父工程)搭建

pom.xml引入

<!--springboot起步依赖-->

<parent>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>2.6.2</version>

</parent>

 

<!--依赖管理-->

<dependencyManagement>

       <dependencies>

       <!--springcloud依赖-->

           <dependency>

               <groupId>org.springframework.cloud</groupId>

               <artifactId>spring-cloud-dependencies</artifactId>

               <version>2021.0.0</version>

               <type>pom</type>

               <scope>import</scope>

           </dependency>

       </dependencies>

   </dependencyManagement>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

eureka-server(注册中心)搭建

pom.xml引入

<dependencies>

  <!--注册中心服务-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

        </dependency>

</dependencies>

1

2

3

4

5

6

7

application.yml

server:

  port: 7001

spring:

  application:

    name: eureka-server

eureka:

  client:

    register-with-eureka: false

    fetch-registry: false

    serviceUrl:

      defaultZone: http://localhost:7001/eureka

# server:

# enable-self-preservation: false

# eviction-interval-timer-in-ms: 5000

1

2

3

4

5

6

7

8

9

10

11

12

13

14

启动类配置

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(EurekaServerApplication.class,args);

    }

}

1

2

3

4

5

6

7

user-provider(提供者)搭建

在这里插入图片描述

 

pom.xml引入

<dependencies>

        <!--mybatis-plus包-->

        <dependency>

            <groupId>com.baomidou</groupId>

            <artifactId>mybatis-plus-boot-starter</artifactId>

            <version>3.1.1</version>

        </dependency>

        <!--web起步包-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <!--MySQL驱动包-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

        <!--测试包-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

        <!--eureka客户端-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

        <!--pojo依赖-->

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

        </dependency>

</dependencies>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

application.yml配置

server:

  port: 18081

spring:

  datasource:

    password: 123456

    username: root

    driver-class-name: com.mysql.cj.jdbc.Driver

    url: jdbc:mysql://127.0.0.1:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

  application:

    name: user-provider

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:7001/eureka

    instance:

      ip-address: 127.0.0.1

      prefer-ip-address: true

      #若超过90s,eureka服务器还没收到provider发送的http请求,则认为服务停止运行。若关闭了eureka的自我保护机制,则eurekaServer会将无效的服务移除列表

      lease-expiration-duration-in-seconds: 90

      #服务正常情况下,每个30s,发送一个http请求给eurekaServer,证明自己可以正常运行

      lease-renewal-interval-in-seconds: 30

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

启动类配置

@SpringBootApplication

@EnableEurekaClient

@MapperScan(basePackages = "com.guigu.dao")

public class UserProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(UserProviderApplication.class,args);

    }

}

1

2

3

4

5

6

7

8

user-consumer(消费者)搭建

在这里插入图片描述

 

pom.xml引入

<dependencies>

        <!--springWeb依赖-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <!--eureka客户端-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

</dependencies>

1

2

3

4

5

6

7

8

9

10

11

12

application.yml配置

server:

  port: 18082

spring:

  application:

    name: user-consumer

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:7001/eureka

1

2

3

4

5

6

7

8

9

启动类配置

@SpringBootApplication

//@EnableDiscoveryClient适用于多种注册中心 @EnableEurekaClient

@EnableDiscoveryClient

public class UserConsumerApplication {

    public static void main(String[] args) {

        SpringApplication.run(UserConsumerApplication.class,args);

    }

 

    @Bean

    public RestTemplate restTemplate(){

        return new RestTemplate();

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

controller配置

@RestController

@RequestMapping("/consumer")

public class UserController {

    @Autowired

    private RestTemplate restTemplate;

 

    @Autowired

    private DiscoveryClient discoveryClient;

 

    @RequestMapping("/{id}")

    public Object findById(@PathVariable Integer id){

        System.out.println("id = " + id);

 

        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("user-provider");

        ServiceInstance serviceInstance = serviceInstances.get(0);

 

        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/find/"+id;

        System.out.println(url);

 

        //"http://localhost:18081/user/find/"+id

        String result = restTemplate.getForObject(url,String.class);

        System.out.println(result);

 

        return result;

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

入门搭建项目参考

 

eurekaspring cloudjava

来自专栏

微服务_Netflix组件

 prprprpr. 3篇文章 0人订阅

 

发布于2022-01-06

著作权归作者所有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值