(五)SpringCloud初级篇——Eureka服务注册与发现:单机Eureka构建步骤

IDEA生成EurekaServer端服务注册中心

建Module

cloud-eureka-server7001

改POM

<dependencies>
    <!--eureka-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sunyan.springcloud</groupId>
        <artifactId>cloud-api-common</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--一般为通用配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

1.x和2.x的对比说明

写YML


server:
  port: 7001

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

主启动

不要忘记@EnableEurekaServer

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

测试

No instance available没有服务被发现 因为没有注册服务进来当然不可能有服务被发现

EurekaClient端cloud-provider-payment8001

将注册进EurekaServer成为服务提供者provider

改pom

添加依赖

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

写yml

添加如下内容

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

主启动

@EnableEurekaClient

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

测试

1.先启动EurekaServer
2.http://localhost:7001/
3.微服务注册名配置说明

EurekaClient端cloud-consumer-order80

将注册进EurekaServer成为服务消费者consumer

参照cloud-provider-payment8001 修改

改pom

添加依赖

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

写yml

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order

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

主启动

@EnableEurekaClient

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

测试

http://localhost:7001/

http://localhost/consumer/payment/get/2

bug


原文地址:https://www.cnblogs.com/sunyanblog/p/12793030.html