码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud(四)Eureka服务注册与发现

时间:2020-01-27 23:47:25      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:服务   模式   中心   instance   ref   end   ati   ber   host   

1 Eureka Server

提供服务注册和发现

2 Service Provider

服务提供方

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

3 Service Consumer

服务消费方

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

介绍

我们的服务发现一般分为两种模式一种为客户端发现模式(我们主要讲这个),一种为服务端发现模式

服务实例的网络位置都是动态分配的,而且因为扩展,失效和升级等需求,服务实例会常常发生动态改变,因此客户端一种更加复杂的服务发现机制

当使用客户端发现模式时,客户端负责决定相应服务实例之间的网络位置,并且对请求实现负载均衡,客户端从一个服务注册服务中查询,其中是所有可用服务实例的库,客户端使用负载均衡算法对从多个服务实例中选择出一个,然后发出请求

使用Eureka的步骤

1、搭建eureka server

1.1 创建工程

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <artifactId>spring_cloud_demo</artifactId>
         <groupId>cn.qyx</groupId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>eureka_server</artifactId>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
         </dependency>
     </dependencies>
 ?
 ?
 ?
 </project>

1.2 导入相应的eureka server的坐标

1.3 配置application.yml

 server:
  port: 9000
 #配置eureka server
 eureka:
  instance:
    hostname: localhost #主机地址名称
  client:
    register-with-eureka: false #是否将自己注册到注册中心
    fetch-registry: false #是否要从eureka中获取注册的信息
     #配置暴露给EurekaClient的请求地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.4 配置启动类

 package com.eureka;
 ?
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 ?
 @SpringBootApplication
 @EnableEurekaServer //激活EurekaServer
 public class EurekaApplication {
     public static void main(String[] args) {
         SpringApplication.run(EurekaApplication.class,args);
    }
 }
 ?

2、将服务提供者注册到eurekaServer上

2.1 引入EurekaClient的坐标

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

2.2 修改application.yml 添加EurekaServer的配置信息

 server:
  port: 9001
 spring:
  application:
    name: service-product #服务名称
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
    username: root
    password: 15192734369qwe
  jpa:
    database: mysql
    show-sql: true
    open-in-view: true
 #配置Eureka
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用IP地址进行注册
 ?

2.3 修改启动类,添加服务发现的支持

 package com.qyx;
 ?
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 ?
 @SpringBootApplication
 @EntityScan("com.qyx.entity")
 //激活EurekaClient
 @EnableEurekaClient
 //@EnableDiscoveryClient 为netflix提供的注解,与@EnableEurekaClient效果一样,新版本不写也行
 public class ProductApplication {
     public static void main(String[] args) {
         SpringApplication.run(ProductApplication.class);
    }
 }
 ?

3、服务消费者通过注册中心获取服务列表,并调用

Eureka中的元数据:例如服务的主机名、IP等信息,可以通过EurekaServer获取,用于服务之间的调用

3.1 引入EurekaClient的坐标

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

3.2 配置application.yml

 server:
  port: 9002
 spring:
  application:
    name: order-product #服务名称
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shops?useUnicode=true&characterEncoding=utf8
    username: root
    password: 15192734369qwe
  jpa:
    database: mysql
    show-sql: true
    open-in-view: true
 #配置Eureka
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用IP地址进行注册
 ?

3.3 获取服务的元数据

 package com.qqq.controller;
 ?
 import com.qqq.entity.Product;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.client.ServiceInstance;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.client.RestTemplate;
 ?
 import java.util.List;
 ?
 @RestController
 @RequestMapping("/order")
 public class OrderController {
     //注入RestTemplate对象
     @Autowired
     private RestTemplate restTemplate;
     /**
      * 注入DiscoveryClient
      * SpringCloud提供的获取元数据的工具类
      * 调用方法获取服务的元数据信息
      */
     @Autowired
     private DiscoveryClient discoveryClient;
     /**
      * 参数:商品ID
      * 通过订单系统,调用商品服务根据id查询商品信息
      * 1 需要配置一个商品对象
      * 2 需要调用商品服务
      * 使用java中的urlConnection完成,或使用httpClient,okHttp
      * RestTemplate
      */
     @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
     public Product findById(@PathVariable("id") Long id)
    {
         //调用DiscoveryClient的getInstances方法根据服务名获取元数据
         List<ServiceInstance> instances = discoveryClient.getInstances("SERVICE-PRODUCT");
         //获取唯一的元数据
         ServiceInstance instance = instances.get(0);
         Product product=null;
         //如何调用商品服务
         //根据元数据和端口号拼接请求的url
         product=restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/"+id,Product.class);
         return product;
    }
 ?
 }
 ?

SpringCloud(四)Eureka服务注册与发现

标签:服务   模式   中心   instance   ref   end   ati   ber   host   

原文地址:https://www.cnblogs.com/qyx66/p/12237187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!