栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

服务治理是什么(服务治理和注册中心)

服务治理是什么(服务治理和注册中心)

目录

一、Eureka

一、Eureka基础知识

1、什么是服务治理

2、什么是服务注册

3、Eureka的两个组件

二、构建单机的Eureka步骤

1、生成eurekaServier端的服务注册中心

2、将服务提供者provider注册进eurekaServer

3、将消费者consumer注册进EredaServer

三、构建Eureka集群

 1、在新建一个eurekaServer服务器(cloud-eureka-server7002)

2、将服务者Provider8002集群构建

3、将两台provider发布到上面的两台eureka集群配置中

4 、将consumer发布到eureka集群中

四、actuator微服务信息完善

1、主机名称:服务名称修改

 2、显示IP

五、服务发现

六、eureka的自我保护

1、概述

2、禁用eureka的自我保护

3、测试

二、zookeeper

一、将服务生产者注册进zookeeper

1、建module

2、改pom

3、写yml

4、主启动

5、controller

6、测试

 二、服务消费方注册进zookeeper

1、建module

2、改pom

3、写yml

4、主启动

5、controller

6、测试

 三、consul

一、简介和安装

1、简介

 二、服务提供者注册

1、建module

2、pom

3、yml

4、主启动类

5、controller

6、验证测试

三、服务消费者注册

1、建module

2、pom

3、yml

4、主启动类

5、controller

6、验证测试

四、三个注册中心的异同点



一、Eureka

一、Eureka基础知识

1、什么是服务治理

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

2、什么是服务注册

Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

eureka的架构图

3、Eureka的两个组件

Eureka Server提供服务注册服务

各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。


EurekaClient通过注册中心进行访问

是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

二、构建单机的Eureka步骤

1、生成eurekaServier端的服务注册中心

建module

module名称为cluod_eureka_server7001

改pom


    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
    
        org.example
        cloud-api-commons
        1.0-SNAPSHOT
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

写yml

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

主启动

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

测试

在浏览器输入http://localhost:7001进入管理台

结果页面

2、将服务提供者provider注册进eurekaServer

改pom

加入该坐标

dependency>
    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

写yml

server:
  port: 8001
spring:
  application:
    name: provider_payment8001
  datasource:
    url: jdbc:mysql://localhost:3306/xiaojiang?serverTimezone=UTC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  #指定映射文件的位置
  mapper-locations: classpath:mapper/*.xml
  #type-aliases-package: org.example.

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

主启动

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

测试

在浏览器输入http://localhost:7001进入管理台

3、将消费者consumer注册进EredaServer

改pom


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

写yml

server:
  port: 81

spring:
  application:
    name: comsumer_order

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

主启动

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

测试

在浏览器输入http://localhost:7001进入管理台

三、构建Eureka集群

 1、在新建一个eurekaServer服务器(cloud-eureka-server7002)

改pom


    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
    
        org.example
        cloud-api-commons
        1.0-SNAPSHOT
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

写yml

7001的yml

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

7002的yml

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

修改hosts文件

在C:WindowsSystem32driversetc下的hosts文件,这样就可以通过

http://eurka7002.com:7002来访问服务器的管理台了

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

主启动

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

2、将服务者Provider8002集群构建

这个服务者与上面的provider8001几乎相同所以也可将provider8001端口改为8002重新发布即可

改pom


    org.example
    cloud-api-commons
    1.0-SNAPSHOT


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-actuator


    org.mybatis.spring.boot
    mybatis-spring-boot-starter



    com.alibaba
    druid-spring-boot-starter
    1.1.10



    mysql
    mysql-connector-java



    org.springframework.boot
    spring-boot-starter-jdbc


    org.springframework.boot
    spring-boot-devtools
    runtime
    true


    org.projectlombok
    lombok
    true


    org.springframework.boot
    spring-boot-starter-test
    test

写yml

server:
  port: 8002
spring:
  application:
    name: provider-payment
  datasource:
    url: jdbc:mysql://localhost:3306/xiaojiang?serverTimezone=UTC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  #指定映射文件的位置
  mapper-locations: classpath:mapper/*.xml
  #type-aliases-package: org.example.

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

主启动

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

业务类直接从provider8001中粘贴

3、将两台provider发布到上面的两台eureka集群配置中

将service-url: defaultZone:改为两台eureka的IP即可

spring.application.name最好相同

server:
  port: 8002
spring:
  application:
        #一定要是中划线,不能是下划线
    name: provider-payment
  datasource:
    url: jdbc:mysql://localhost:3306/xiaojiang?serverTimezone=UTC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  #指定映射文件的位置
  mapper-locations: classpath:mapper/*.xml
  #type-aliases-package: org.example.

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

    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4 、将consumer发布到eureka集群中

yml与上面相同做法将service-url: defaultZone:改为两台eureka的IP即可

controller和conf要更改,启动负载均衡,默认为轮询

controller修改

服务提供端的地址不能写死,否则不能有集群的效果,改为

spring: application:
       name: provider-payment

//public static final String PaymentSrv_URL = "http://localhost:8001";
public static final String PaymentSrv_URL = "http://PROVIDER-PAYMENT";

 conf的修改

public class ApplicationContextConfig {
    @Bean
    @LoadBalanced   //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

四、actuator微服务信息完善

1、主机名称:服务名称修改

由于现在,会显示主机名不安全

 修改yml

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

修改后

 2、显示IP

修改前

yml文件

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。
    #单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/
  instance:
    instance-id: payment8001
    #访问路径可以显示IP地址
    prefer-ip-address: true

修改后

五、服务发现

对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息

更改controller

@Resource
    DiscoveryClient discoveryClient;
@GetMapping(value = "/discovery")
    public Object discovery()
    {
        List services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        List instances = discoveryClient.getInstances("PROVIDER-PAYMENT");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId() + "t" + element.getHost() + "t" + element.getPort() + "t"
                    + element.getUri());
        }
        return this.discoveryClient;
    }

主启动类

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

六、eureka的自我保护

1、概述

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
如果在Eureka Server的首页看到以下这段提示,则说明Eureka进入了保护模式:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE

一句话:某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存 

2、禁用eureka的自我保护

再服务器进行如下配置就行

server:
  enable-self-preservation: false

3、测试

在生产者服务端8002进行以下配置就行

 instance:
  #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-renewal-interval-in-seconds: 1
  #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    lease-expiration-duration-in-seconds: 2

二、zookeeper

一、将服务生产者注册进zookeeper

1、建module

新建一个cloud-provider-payment8004maven模块

2、改pom

由于spring-cloud-starter-zookeeper中的zookeeper的jar包与我所使用的的zookeeper版本冲突,

所以排除掉了,自己导入了一个包

有由于我这里日志文件slf4j-log4j12和logback-classic冲突所以又排除掉了slf4j-log4j12


    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
        
            
                org.apache.zookeeper
                zookeeper
            
        
    
    
        org.apache.zookeeper
        zookeeper
        
            
                org.slf4j
                slf4j-log4j12
            
        
    
    
        org.example
        cloud-api-commons
        1.0-SNAPSHOT
    
    
        org.springframework.boot
        spring-boot-starter-web
        
            
                org.slf4j
                slf4j-log4j12
            
        
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

3、写yml
server:
  port: 8004

spring:
  application:
    name: provider-payment
  cloud:
    zookeeper:
      #zookeeper注册中心的IP和地址
      connect-string: 192.168.195.128:2181

4、主启动
@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationPayment8004 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationPayment8004.class,args);
    }

}

5、controller
@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/payment/zk")
    public String paymentzk()
    {
        return "springcloud with zookeeper: "+serverPort+"t"+ UUID.randomUUID().toString();
    }
}

6、测试

  ./zkCli.sh 进入到客户端,用 ls / 来查看有几个节点,用ls /service 就能查看注册进的服务了

 二、服务消费方注册进zookeeper 1、建module

建一个cloud-consumerzk-order的maven模块

2、改pom

与上面相同

3、写yml
server:
  port: 81

spring:
  application:
    name: comsumer-order
  cloud:
    zookeeper:
      connect-string: 192.168.195.128:2181
4、主启动
@SpringBootApplication
@EnableDiscoveryClient
public class order81 {
    public static void main(String[] args) {
        SpringApplication.run(order81.class,args);
    }
}
5、controller

配置类

@Configuration
public class ApplicationConf {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

controller

@RestController
public class orderController {
    public static final String PROVIDER_URL="http://provider-payment";

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/consumer/payment/zk")
    public String paymentInfo()
    {
        String result = restTemplate.getForObject(PROVIDER_URL+"/payment/zk", String.class);
        System.out.println("消费者调用支付服务(zookeeper)--->result:" + result);
        return result;
    }

}
6、测试

 三、consul

一、简介和安装

1、简介

https://www.consul.io/intro/index.html  这是consul的官方文档

    服务发现:提供HTTP和DNS两种发现方式。健康监测:支持多种方式,HTTP、TCP、Docker、Shell脚本定制化监控kv存储:Key、Value的存储方式多数据开发:支持多数据中心可视化web界面

下载地址

Downloads | Consul by HashiCorphttps://www.consul.io/downloads.html

启动consul agent -dev通过以下地址可访问首页http://localhost:8500结果页面

 二、服务提供者注册 1、建module

新建cloud-consul-provider8005moudule

2、pom

添加坐标


    org.springframework.cloud
    spring-cloud-starter-consul-discovery

3、yml

server:
  port: 8005

spring:
  application:
    name: consul-provider
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

4、主启动类

与上面一致

5、controller

与上面一致

6、验证测试

三、服务消费者注册 1、建module

新建cloud-consul-provider8005moudule

2、pom

添加坐标

org.springframework.cloud spring-cloud-starter-consul-discovery

3、yml
server:
  port: 82
spring:
  application:
    name: consul-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
4、主启动类

与上面一致

5、controller
@RestController
public class ConsulController {
    public static final String PROVIDER_URL="http://consul-provider";

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/consumer/payment/consul")
    public String paymentInfo()
    {
        String result = restTemplate.getForObject(PROVIDER_URL+"/payment/consul", String.class);
        System.out.println("消费者调用支付服务(zookeeper)--->result:" + result);
        return result;
    }
}
6、验证测试

四、三个注册中心的异同点

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772272.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号