zoukankan      html  css  js  c++  java
  • 服务注册与发现

    摘自:https://www.jianshu.com/p/11c3cec737c2

    服务注册与发现

    ​ 微服务是有多个独立的微小服务构成,需要有一个有效的机制对架构中所有的服务进行管理监控,了解他们的状态。就像体检单一样知道我们"身体各个器官"的状态。这样的机制是什么呢?

    ​ 服务注册与发现就是可以帮助我们很好管理每个微服务的机制,服务注册是指向服务注册中注册一个服务实例,服务提供者将自己的信息告知服务注册中心。服务发现是指一个服务需要消费另一个服务时 ,服务注册中心给他消费服务的信息,如ip地址。这样我们就可以掌握每个服务实例状态,同时服务间的消费也可以交给服务注册中心。

    ​ 在springCloud的中提供了Enable组件,方便我们搭建服务注册中心及每个微服务进行服务注册及服务消费。

    1. 服务注册 - 服务运行向注册中心注册服务,提供ip、端口、接口信息
    2. 服务续约 - 服务提供者和 注册中心 定时保持通讯,间隔时间为30s,确保服务可正常调用
    3. 服务获取 - 服务消费者从 注册中心 上每30s进行服务清单同步。服务清单有服务提供者访问连接等信息
    4. 服务访问 - 服务消费者使用清单中的信息去访问获取服务提供者提供的资源
     
    image.png

    eureka作为服务注册中心

    ​ 服务注册和服务发现是配套进行使用的,使用eureka作为注册中心,服务提供者需要使用eureka client 作为配套使用

    服务注册中心 - eureka server

    pom依赖
    • Spring-cloud-starter-netfix-eureka-server
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     </dependency>
    
    声明 : 使用该声明就表示该项目为eureka 服务中心
    • @EnableEurekaServer
    @SpringBootApplication
    @EnableEurekaServer
    public class UserMemberApplication {
       public static void main(String[] args) {
          SpringApplication.run(UserMemberApplication.class, args);
       }
    }
    
    application.yml 配置
    • 默认端口 8761
    • server 不能注册到服务中

    在工程的配置文件进行服务名、端口号

    # 注册中心端口号
    server.port=8761
    # 本身不进行服务注册
    eureka.client.fetch-registry=false
    # 不获取注册相关内容
    eureka.client.register-with-eureka=false
    
    总结

    eureka 注册中心使用步骤

    1. pom文件依赖引入
    2. 启动类声明该项目为eureka服务注册中心
    3. 配置端口,server本身不进行注册

    eureka client 服务提供者

    pom 依赖
    • Spring-cloud-start-netfix-eureka-client
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
    声明
    • @EnableEurekaClient : eureka 提供的注解
    • @EnableDiscoveryClient : springCloud 提供的服务发现注解,建议使用
    @SpringBootApplication
    @EnableEurekaClient
    public class UserApplication{
       public static void main(String[] args) {
          SpringApplication.run(UserApplication.class, args);
       }
    }
    
    配置
    • 该服务端口
    • 服务名称
    • 注册中心地址
    #该服务端口  
    server.port= 10082
    #该服务命名
    spring.application.name= user-server
    # 服务注册中心地址配置
    eureka.client.service-url.defaultZone= http://localhost:10086/eureka/
    

    启动该服务,服务注册中心出现该服务

     
    image.png

    使用Feign 访问服务

    ​ Feign 是声明式的Result web 服务客户端,使用该组件可先调用方法一样使用其他服务

    创建 feign工程

    创建一个spring-boot工程,工程名称为 user-member,引入feign、eureka的起步依赖

    pom依赖
    • Euraka-client 依赖
    • Openfeign 依赖
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    application.yml 配置
    # 服务端口
    server:
      port: 10083
    
    #服务注册中心地址
    eureka:
      client:
          service-url:
              defaultZone: http://localhost:10086/eureka/
    
    #服务名称
    spring:
      application:
          name: user-memeber
    
    启动类注解

    使用 EnableFeignClients 开启 feign功能

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class UserMemberApplication {
    
    public static void main(String[] args) {
    SpringApplication.run(UserMemberApplication.class, args);
    }
    
    }
    
    定义 feign接口

    使用 @FeignClient 注解,@@FeignClient 注解中使用value指定调用的服务,同时调用对应接口,使用的是springMVC相同的注解

    @FeignClient(value = "user-server")
    public interface UserClient {
    
          @GetMapping("/hello")
          String hello();
    }
    
    使用feign接口

    使用注入的方式进行feignClient调用

    @RestController
    @SuppressWarnings("all")
    public class UserMemberController {
    
      @Autowired
      UserClient memberClient;
    
      //测试fign调用
      @GetMapping("/hello")
      public String hello(){
      
          return memberClient.hello();
      }
    }
    
     
     
    1人点赞
     
     


    作者:六层
    链接:https://www.jianshu.com/p/11c3cec737c2
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    网游开发中的可怕群体:单机派
    关卡设计的基本理论
    游戏程序员所需的知识体系
    关于SQL Server将一列的多行内容拼接成一行的问题讨论——之一(转)
    c# lmada 修改List内容
    c# 取sqlite库分组的第一行不对
    关于SQL Server将一列的多行内容拼接成一行的问题讨论(转)
    “打包的”爱情
    “婚礼哥”隔空喊爱:我要做你一生的北京情人
    北漂,都不敢奢望爱情?
  • 原文地址:https://www.cnblogs.com/xichji/p/11946770.html
Copyright © 2011-2022 走看看