Springcloud Alibaba详细入门教程(三)

 Eureka是Netflix开源的一款提供服务注册和发现的产品,注册中心是分布式开发的核心组件之一,而eureka是spring cloud推荐的注册中心实现

官网说明:Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing.

  • 创建子工程cloud-eureka-server7001工程

目录如下

Springcloud Alibaba详细入门教程(三)_第1张图片

cloud-eureka-server7001.pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    com.atguigu.springcloud
    cloud-eureka-server7001

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            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
        
        
            junit
            junit
            test
        
    


application.yml

server:
  port: 7001

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

启动类EurekaMain7001

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

启动服务 访问  http://localhost:7001/

Springcloud Alibaba详细入门教程(三)_第2张图片

  • 子工程payment8001和order80注册在eureka-server7001上

    子工程payment8001:

Cloud-provider-payment8001.pom上添加一个依赖


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

application.yml添加eureka配置

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

启动类PaymentMain8001

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


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

}

启动8001服务,然后访问 http://localhost:7001/

Springcloud Alibaba详细入门教程(三)_第3张图片

子工程order80:

OrderMain80.pom添加eureka-client的依赖

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

application.yml添加eureka的配置

server:
  port: 80

spring:
  application:
    name: cloud-order-service
eureka:
  client:
    #表示是否将自己注册进eurekaServer 默认为true
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/

启动类OrderMain80

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {

    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}

启动80服务,访问eureka7001:http://localhost:7001/

80也注册进了eureka

Springcloud Alibaba详细入门教程(三)_第4张图片

postman重新访问  http://localhost:80/consumer/payment/get/9

Springcloud Alibaba详细入门教程(三)_第5张图片

你可能感兴趣的:(Springcloud,Alibaba,Springcloud笔记,微服务,spring,cloud,微服务,分布式,spring,cloud,alibaba,spring,boot)