Spring Cloud Netflix 服务发现:Eureka (一) 注册和运行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wyx_wx/article/details/89281267

部分内容摘自 Spring Cloud 官方文档中文版

本文源码地址:https://github.com/Wyxwx/SpringCloudDemo

Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具

例如配置管理,服务发现,断路器,智能路由,微代理,控制总线

Eureka

服务发现是基于微服务架构的关键原则之一。

Eureka 是 Netflix 服务发现中的服务注册和发现模块。

作用:将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器

创建一个服务注册中心 

新建一个 maven 工程,next and finished

 接下来,在这个工程下创建一个 Module: eureka_server

next 选择 Cloud Discovery 下的 Eureka Server

注意:生成后的 pom.xml 文件里有关于 Spring Cloud 的版本选择,若是2.0以上的 Spring 版本,请选择 Greenwich.SR1 版本的 Spring Cloud,否则将无法使用

接下来编写配置文件 application.properties

server.port=8761

eureka.instance.hostname=localhost

# 默认值都为 true
# 为 true 时代表注册为客户端
# 此时注册为服务端,所以都改为 false
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

最后,为启动类加入 @EnableEurekaServer 注解 并运行该类

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

服务器具有一个带有UI的主页,所以访问 http://localhost:8761 后可以看到如下界面

由于此时并未注册客户端,所以红框圈起的部分显示没有连接

创建一个 Eureka 客户端

在刚刚创建的工程下继续创建一个新的 Module: eureka_client

在依赖选择时依旧选择 Cloud Discovery 下的 Eureka Server

接下来编写配置文件 application.properties

server.port=8762

# 为客户端提供服务 URL
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#默认应用程序名称(服务ID)
spring.application.name=provideHelloService

新建一个 HelloController 类

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    //从配置文件中找到名字为 server.port 的值并将其注入 port 中
    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/")
    public String hello(){
        return "Hello world";
    }

    /**
     * 测试负载均衡
     */
    @RequestMapping(value = "/hi")
    public String hi(){
        return "my port is " + port;
    }

}

为启动类加入 @EnableEurekaClient 注解,并启动该类


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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

 此时再次访问 http://localhost:8761/ 可以看到刚刚启动的 Eureka 已经成功在服务中心注册

访问 http://localhost:8762/ 成功返回 Hello world

访问 http://localhost:8762/hi 成功显示端口号

猜你喜欢

转载自blog.csdn.net/Wyx_wx/article/details/89281267