Springboot2.x整合SpringCloud之Eureka服务注册中心

1、 什么是服务注册中心

        服务注册中心是服务实现服务化管理的核心组件,相似于目录服务的做用,主要用来存储服务信息譬如提供者url串、路由信息等。服务注册中心是SOA架构中最基础的设施之一。html

服务注册中心的做用

  1,服务的注册java

  2,服务的发现web

2. 常见的注册中心有哪些

  1,Dubbo 的注册中心Zookeeper算法

  2,Sringcloud的注册中心Eurekaspring

3. 服务注册中心解决了什么问题

  1. 服务管理;
  2. 服务的依赖关系管理;

4. 什么是Eureka注册中心

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

5. Eureka注册中心三种角色

5.1 Eureka Server

  经过RegisterGetRenew等接口提供服务的注册和发现。浏览器

5.2 Application Service (Service Provider)

    服务提供方缓存

 把自身的服务实例注册到Eureka Server安全

5.3 Application Client (Service Consumer)

    服务调用方服务器

 经过Eureka Server 获取服务列表,消费服务。

2、 Eureka入门案例

一、建立项目

 

二、pom.xml文件以下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.9.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.angei</groupId>
12     <artifactId>eurekaserver</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>eurekaserver</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
20     </properties>
21 
22     <dependencies>
23         <dependency>
24             <groupId>org.springframework.cloud</groupId>
25             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
26         </dependency>
27 
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-test</artifactId>
31             <scope>test</scope>
32         </dependency>
33     </dependencies>
34 
35     <dependencyManagement>
36         <dependencies>
37             <dependency>
38                 <groupId>org.springframework.cloud</groupId>
39                 <artifactId>spring-cloud-dependencies</artifactId>
40                 <version>${spring-cloud.version}</version>
41                 <type>pom</type>
42                 <scope>import</scope>
43             </dependency>
44         </dependencies>
45     </dependencyManagement>
46 
47     <build>
48         <plugins>
49             <plugin>
50                 <groupId>org.springframework.boot</groupId>
51                 <artifactId>spring-boot-maven-plugin</artifactId>
52             </plugin>
53         </plugins>
54     </build>
55 
56 </project>

提示:若是IDEA加载pom.xml时一直下载失败,能够在pom.xml中添加以下配置,使其从国内阿里云镜像中下载相关内容,下载速率将会大幅提高。

<repositories>
    <repository>
        <id>aliyun</id>    
        <name>aliyun</name>    
        <url>https://maven.aliyun.com/repository/public</url>    
    </repository>    
</repositories>

三、添加application.yml全局配置文件

 1 server:
 2   port: 8761
 3 eureka:
 4   instance:
 5     appname: provider-service
 6     hostname: localhost
 7   client:
 8     service-url:
 9       defaultZone:
10         http://localhost:8761/eureka/
11     register-with-eureka: false
12     fetch-registry: false

说明:

 1 server:
 2   port: 8761
 3 eureka:
 4   instance:
 5     #服务名,默认取 spring.application.name 配置值,若是没有则为 unknown
 6     appname: provider-service
 7     #设置当前实例的主机名称
 8     hostname: localhost
 9   client:
10     service-url: 
11       #指定服务注册中心地址,类型为 HashMap,并设置有一组默认值,
12       #默认的Key为 defaultZone;默认的Value为 http://localhost:8761/eureka ,
13       #若是服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。
14       defaultZone:
15         http://localhost:8761/eureka/
16 
17     #是否将本身注册到Eureka-Server中,默认的为true
18     register-with-eureka: false
19 
20     #是否从Eureka-Server中获取服务注册信息,默认为true
21     fetch-registry: false

附:Spring Cloud Eureka 经常使用配置及说明

四、修改启动类

 1 import org.springframework.boot.SpringApplication;
 2 import org.springframework.boot.autoconfigure.SpringBootApplication;
 3 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 4 
 5 @SpringBootApplication
 6 @EnableEurekaServer
 7 public class EurekaserverApplication {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(EurekaserverApplication.class, args);
11     }
12 
13 }

五、经过浏览器访问Eureka-Server服务管理平台

 

3、 搭建高可用Eureka注册中心(Eureka集群)

在微服务架构中,注册中心是一个必不可少的组件,前面咱们搭建的注册中心只适合本地开发使用,在生产环境必须搭建一个集群来保证高可用。

Eureka的集群搭建很简单,每一台Eureka都须要在配置中指定另外N个Eureka的地址就能够。

在Eureka服务端的配置项eureka.client.serviceUrl.defaultZone中地址那一行要使用ip或域名,因为这里是在本地一台机子上模拟集群环境,ip地址都同样,因此经过更改本地host文件的方式建立三个可用的域名。

1.修改C:\Windows\System32\drivers\etc\hosts文件:

127.0.0.1 server1
127.0.0.1 server2
127.0.0.1 server3

2.本地刷新dns:

指令: ipconfig /flushdns

 

 

3.建立3个application.yml

application-p8761.yml

server:
  port: 8761
eureka:
  instance:
    #服务名,默认取 spring.application.name 配置值,若是没有则为 unknown
    appname: provider-service
    #设置当前实例的主机名称
    hostname: server1
  client:
    service-url:
      #指定服务注册中心地址,类型为 HashMap,并设置有一组默认值, #默认的Key为 defaultZone;默认的Value为 http://localhost:8761/eureka ,
 #若是服务注册中心为高可用集群时,多个注册中心地址以逗号分隔,地址形式是ip或域名:端口号
      defaultZone:
        http://server2:8762/eureka/,http://server3:8763/eureka/

application-p8762.yml

server:
  port: 8762
eureka:
  instance:
    appname: provider-service
    hostname: server2
  client:
    service-url:
      defaultZone:
        http://server1:8761/eureka/,http://server3:8763/eureka/

application-p8763.yml

server:
  port: 8763
eureka:
  instance:
    appname: provider-service
    hostname: server3
  client:
    service-url:
      defaultZone:
        http://server1:8761/eureka/,http://server2:8762/eureka/

4.而后须要注意,application-xxxx.yml不是默认的配置形式,是没法被自动识别的,能够经过配置spring.profiles.active的方式指定运行时加载。

 5.分别启动pEurekaServerApplication-8761, EurekaServerApplication-8762, EurekaServerApplication-8763

 显示以下:

6.同时,在客户端的配置项中设置服务注册中心地址时,设置为哪个均可以,最好都写上(用逗号隔开),这样当其中一个节点挂了,客户端还会自动尝试链接其余节点。

server:
  port: 80
spring:
  application:
    name: order-service
eureka:
  client:
    service-url:
      #设置服务注册中心地址
      defaultZone:
        http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

 

4、 在Eureka注册中心中构建客户端服务

Eureka客户端开发要点:
   ①、maven 依赖 spring-cloud-starter-netflix-eureka-client;
   ②、application.yml 配置 eureka.client.service-url.defaultZone;
   ③、入口类増加 @EnableEurekaClient;

一、建立项目

 

 

二、pom.xml文件以下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.9.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.angei</groupId>
12     <artifactId>eureka-client</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>eureka-client</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
20     </properties>
21 
22     <dependencies>
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.cloud</groupId>
29             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-test</artifactId>
35             <scope>test</scope>
36         </dependency>
37     </dependencies>
38 
39     <dependencyManagement>
40         <dependencies>
41             <dependency>
42                 <groupId>org.springframework.cloud</groupId>
43                 <artifactId>spring-cloud-dependencies</artifactId>
44                 <version>${spring-cloud.version}</version>
45                 <type>pom</type>
46                 <scope>import</scope>
47             </dependency>
48         </dependencies>
49     </dependencyManagement>
50 
51     <build>
52         <plugins>
53             <plugin>
54                 <groupId>org.springframework.boot</groupId>
55                 <artifactId>spring-boot-maven-plugin</artifactId>
56             </plugin>
57         </plugins>
58     </build>
59 
60 </project>

三、添加application.yml全局配置文件

 1 server:
 2   port: 80
 3 spring:
 4   application:
 5     name: order-service
 6 eureka:
 7   client:
 8     service-url:
 9       #设置服务注册中心地址
10       defaultZone:
11         http://localhost:8761/eureka/

四、修改启动类

 1 import org.springframework.boot.SpringApplication;
 2 import org.springframework.boot.autoconfigure.SpringBootApplication;
 3 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 4 
 5 @SpringBootApplication
 6 @EnableEurekaClient
 7 public class EurekaClientApplication {
 8     public static void main(String[] args) {
 9         SpringApplication.run(EurekaClientApplication.class, args);
10     }
11 }

五、测试

 1 package com.angei.eurekaclient.Controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 @Controller
 9 public class orderController {
10 
11     @GetMapping("/order/{id}")
12     @ResponseBody
13     public String findById(@PathVariable("id") Integer orderId){
14         if(orderId==2019){
15             return "{\"Id\":1,\"Title\":\"饿了么订单\"}";
16         }else{
17             return null;
18         }
19     }
20 }

先启动服务器,再启动客户端:

 

5、 在高可用的Eureka注册中心中模拟构建provider服务和consumer服务

(一)搭建provider提供服务

1.搭载环境

 

 

 配置application.properties,将服务注册到注册中心。

spring.application.name=eureka-provider
server.port=9090
#设置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://server1:8761/eureka/,http://server2:8762/eureka/,http://server3:8763/eureka/

2.模拟提供服务

建立User实体类:

 1 import lombok.Getter;
 2 import lombok.Setter;
 3 import java.io.Serializable;
 4 
 5 @Getter
 6 @Setter
 7 public class User implements Serializable {
 8 
 9     private int id;
10 
11     private String name;
12 
13     public User() {
14     }
15 
16     public User(int id, String name) {
17         this.id = id;
18         this.name = name;
19     }
20 
21     @Override
22     public String toString() {
23         return "学号:" + this.id + "\t姓名:" + this.name;
24     }
25 }

建立userController:

 1 import com.example.demo.pojo.User;
 2 import org.springframework.stereotype.Controller;
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 import java.util.ArrayList;
 7 
 8 @Controller
 9 @RestController
10 public class userController {
11 
12     @RequestMapping("/getAllUser")
13     public ArrayList<User> getAllUser() {
14         ArrayList<User> list = new ArrayList<>();
15         list.add(new User(2018, "Benjieming"));
16         list.add(new User(2019, "Huangsi"));
17         list.add(new User(2020, "Yangyi"));
18         return list;
19 
20     }
21 }

3.启动项目并检测

 

(二)  搭建consumer调用服务

1.建立项目,过程同provider,最终项目结构以下:

application.yml内容以下:

server:
  port: 9091
spring:
  application:
    name: eureka-consumer
eureka:
  client:
    service-url:
      #设置服务注册中心地址
      defaultZone:
        http://server1:8761/eureka/,http://server2:8762/eureka/,http://server3:8763/eureka/

2.建立userService和userController

userService.java

 1 package com.angei.eurekaclient.Service;
 2 
 3 import com.angei.eurekaclient.pojo.User;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.cloud.client.ServiceInstance;
 6 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
 7 import org.springframework.core.ParameterizedTypeReference;
 8 import org.springframework.http.HttpMethod;
 9 import org.springframework.http.ResponseEntity;
10 import org.springframework.stereotype.Service;
11 import org.springframework.web.client.RestTemplate;
12 
13 import java.util.List;
14 
15 @Service
16 public class userService {
17 
18     @Autowired
19     private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器
20 
21     public List<User> getUsers() {
22 
23         //选择调用的服务的名称
24         //ServiceInstance类封装了服务的基本信息,如 IP,端口等
25         ServiceInstance si = this.loadBalancerClient.choose("eureka-provider");
26         //拼接访问服务的URL
27         StringBuffer sb = new StringBuffer();
28         //http://server1:8761/getAllUser
29         System.out.println("host:" + si.getHost());
30         System.out.println("Port:" + si.getPort());
31         sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/getAllUser");
32 
33 
34         //springMVC RestTemplate
35         RestTemplate rt = new RestTemplate();
36 
37         ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() {
38         };
39 
40         //ResponseEntity:封装了返回值信息
41         ResponseEntity<List<User>> response = rt.exchange(sb.toString(), HttpMethod.GET, null, type);
42         List<User> list = response.getBody();
43         return list;
44     }
45 }

注意:这里是向注册中心获取服务并拉取信息。

userController.java

 1 package com.angei.eurekaclient.Controller;
 2 
 3 import com.angei.eurekaclient.Service.userService;
 4 import com.angei.eurekaclient.pojo.User;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 import javax.annotation.Resource;
 9 import java.util.List;
10 
11 @RestController
12 public class userController {
13 
14     @Resource
15     private userService userservice;
16 
17     @RequestMapping("/getAllUsers")
18     public List<User> getUsers(){
19         return userservice.getUsers();
20     }
21 }

3.启动项目并测试

 

 

 

5、 Eureka注册中心架构原理

 

6、 Eureka优雅停服

一、在什么条件下,Eureka会启动自我保护?

什么是自我保护模式

1,自我保护的条件
   通常状况下,微服务在Eureka上注册后,会每30秒发送心跳包,Eureka经过心跳来判断服务是否健康,同时会按期删除超过90秒没有发送心跳服务。
2,有两种状况会致使Eureka Server收不到微服务的心跳
   a.是微服务自身的缘由;
   b.是微服务与Eureka之间的网络故障;
   一般微服务的自身的故障关闭只会致使个别服务出现故障,通常不会出现大面积故障,而网络故障一般会致使Eureka Server在短期内没法收到大批心跳。
   考虑到这个区别,Eureka设置了一个阀值,当判断挂掉的服务的数量超过阀值时,Eureka Server认为很大程度上出现了网络故障,将再也不删除心跳过时的服务。
3,那么这个阀值是多少呢?
   15分钟以内故障率是否低于85%;
   Eureka Server在运行期间,会统计心跳失败的比例在15分钟内是否低于85%,
   这种算法叫作Eureka Server的自我保护模式。

二、为何要启动自我保护?

为何要自我保护

    1,由于同时保留"好数据"与"坏数据"总比丢掉任何数据要更好,当网络故障恢复后,这个Eureka节点会退出"自我保护模式"。
    2,Eureka还有客户端缓存功能(也就是微服务的缓存功能)。即使Eureka集群中全部节点都宕机失效,微服务的Provider和Consumer都能正常通讯。
    3,微服务的负载均衡策略会自动剔除死亡的微服务节点。

三、如何关闭自我保护?

修改Eureka Server配置文件

#关闭自我保护:true为开启自我保护,false为关闭自我保护
eureka.server.enableSelfPreservation=false
#清理间隔(单位:毫秒,默认是60*1000)
eureka.server.eviction.interval-timer-in-ms=60000

 

7、 如何增强Eureka注册的安全认证

一、在Eureka Server中添加security的依赖包

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

二、修改Eureka的Server配置文件 

#开启http basic的安全认证
security.basic.enabled=true  
security.user.name=user
security.user.password=123456

三、修改Eureka集群节点之间互相访问的url值

eureka.client.serviceUrl.defaultZone=http://user:123456@server2:8762/eureka/, http://user:123456@server3:8763/eureka/

四、修改微服务的配置文件添加访问注册中心的用户名与密码

spring.application.name=eureka-provider
server.port=9090

#设置服务注册中心地址,指向另外一个注册中心
eureka.client.serviceUrl.defaultZone=http://user:123456@server1:8761/eureka/, http://user:123456@server2:8762/eureka/, http://user:123456@server3:8763/eureka/

#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
相关文章
相关标签/搜索