Spring Cloud服务治理(Eureka)

Spring Cloud服务治理(Eureka)

Eureka
  • Eureka是Netflix公司开源的一个服务注册与发现的组件(注册中心)

    在这里插入图片描述

  • Eureka和其他Netflix公司的服务组件(例如负载均衡、熔断器、网关等)一起,被SpringCloud社区整合为Spring-Cloud-NetFlix模块

  • Eureka包含两个组件:Eureka Server(注册中心)和Eureka Client(服务提供者、服务消费者)

Eureka-Server服务方搭建
  • 父工程导入依赖

    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <!-- spring cloud版本信息 -->
            <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        </properties>
    
    	<!-- spring cloud依赖 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
  • Eureka-Server服务方导入依赖

            <!-- eureka-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
  • 编写服务方启动类

    @SpringBootApplication
    @EnableEurekaServer//启用eureakServer,此注解可以把该项目(模块)作为注册中心
    public class EurekaApp {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApp.class,args);
        }
    }
    
  • 配置eureka服务方的yml配置文件

    server:
      port: 8761 #默认端口8761
    
      #eureka配置
      #eureka一共4部分配置
      #1.dashboard:eureka的web控制台配置
      #2.server:eureka的服务端配置
      #3.client:eureka的客户端配置
      #4.instance:eureka的项目实例配置
    eureka:
      instance:
        hostname: localhost #主机名
        
        
      client:
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #http://localhost:8761/eureak  eureak服务端地址,将来客户端使用该地址和eureak进行通信,不写默认为此值
          
          
        register-with-eureka: false #是否将自己的路径注册到eureak上,默认为true,一般提供方才需要把自己的路径注册到eureak上
        
        
        fetch-registry: false #是否需要从eureak中抓取路径,默认为true,一般消费方才需要从eureak中抓取路径
    
  • 测试

在这里插入图片描述

Eureka-Provider提供方搭建
  • Eureak-Provider提供方导入依赖

            <!-- eureka-client  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
  • 编写服务方启动类

    @EnableEurekaClient//提供方,在新版本中此注解可以省略,但是建议加上
    @SpringBootApplication
    public class ProviderApp {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApp.class,args);
        }
    }
    
  • 编写提供方yml文件

    server:
      port: 8001
    
    
    eureka:
      instance:
        hostname: localhost #主机名
        prefer-ip-address: true #是否将自己的ip注册到eureka中,默认false
        ip-address: 127.0.0.1 #设置当前实例ip
        instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} #web控制台显示的实例  ip:应用名称:端口
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka #localhost:注册中心地址     8761:注册中心端口号
    spring:
      application:
        name: eureka-provider #设置当前应用名称
    
    
    
  • 测试结果

在这里插入图片描述

Eureka-Consumer消费方搭建
  • Eureka-Consumer消费方导入依赖

            <!-- eureka-client  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
  • 编写消费方启动类

    @EnableDiscoveryClient //激活DiscoveryClient,动态获取
    @EnableEurekaClient
    @SpringBootApplication
    public class ConsumerApp {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApp.class,args);
        }
    }
    
    
  • 编写消费方yml文件

    server:
      port: 9000
    
    
      eureka:
        instance:
          hostname: localhost #主机名
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka #localhost:注册中心地址     8761:注册中心端口号
      spring:
        application:
          name: eureka-consumer #设置当前应用名称
    
    
  • 动态从eureka server中获取provider的ip和端口

    • 步骤
    1. 注入DiscoveryClient对象并激活
    2. 调用方法
    @RestController
    @RequestMapping("/order")
    public class OrderController {
        @Autowired
        private RestTemplate restTemplate;
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @GetMapping("/goods/{id}")
        public Goods findGoodsById(@PathVariable("id") int id){
    
            /*
            动态从eureka server中获取provider的ip和端口
                1.注入DiscoveryClient对象并激活
                2.调用方法
             */
            List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");//不能填错,一旦填错发生空指针异常
            
            if (instances==null || instances.size()==0){
                return null;
            }
            
            ServiceInstance instance = instances.get(0);
            String host = instance.getHost();//获取ip
            int port = instance.getPort();//获取端口
    
            //拼接
            String url="http://"+host+":"+port+"/goods/findOne/"+id;
            Goods goods = restTemplate.getForObject(url, Goods.class);
            return goods;
        }
    }
    
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值