单机Eureka构建步骤——08端口服务和8001端口服务注册进Eureka

开始吧

解释一下:什么是单机版!?

单机版指的是一个服务,不是集群那种。这个单机针对Eureka而言!

IDEA生成enrekaServer端服务注册中心(类似物业公司)

1、 创建enreka注册中心模块儿

cloud-eureka-server7001模块儿

此处省略创建过程

2、 整理pom文件

    <dependencies>
        <!--eureka服务端的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--springboot启动包-->
        <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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3、 yml配置文件

server:
port: 7001

eureka:
instance:
  hostname: localhost  # eureka 服务器的实例名称

client:
  # false 代表不向服务注册中心注册自己,因为它本身就是服务中心
  register-with-eureka: false
  # false 代表自己就是服务注册中心,自己的作用就是维护服务实例,并不需要去检索服务
  fetch-registry: false
  service-url:
    # 设置与 Eureka Server 交互的地址,查询服务 和 注册服务都依赖这个地址
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4、 启动类创建好

加上@EnableEurekaServer注解!!!

// exclude :启动时不启用 DataSource的自动配置检查
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaServer   // 表示它是服务注册中心
public class EurekaServerMain7001 {
    public static void main(String[] args){
        SpringApplication.run(EurekaServerMain7001.class, args);
    }
}

5、 启动访问本地主机端口7001

成功。不解释。太简单太基础。

EurekaClient端cloud-provider-payment8001将注册进EnrekaServer成为服务提供者provider

类似尚硅谷学校对外提供授课服务

1、 pom文件添加eureka客户端的包

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

2、 启动类上加注解

加上标注eureka客户端的注解:@EnableEurekaClient

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

3、 yml配置文件

添加上eureka配置信息:

eureka:
  client:
    # 注册进 Eureka 的服务中心
    register-with-eureka: true
    # 检索 服务中心 的其它服务
    fetch-registry: true
    service-url:
      # 设置与 Eureka Server 交互的地址
      defaultZone: http://localhost:7001/eureka/

启动Eureka服务端和客户端测试

成功不解释

EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer

类似来尚硅谷上课消费的各位同学

步骤和8001注册进EurekaServer一样

此处略~

posted @ 2020-11-18 19:33  我才不是你的谁  阅读(436)  评论(0编辑  收藏  举报