Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
eureka-client
eureka-server
README.md

README.md

Spring Cloud 入门教程:服务注册与发现 - Eureka

Eureka简介

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

创建服务注册中心

  • 创建一个Module,命名为eureka-server

  • 点击Finish,即可完成module创建。

  • 根目录settings.gradle 自动添加了模块之间的依赖关系。

  • 在module内的build.gradle 中添加依赖:

    group 'com.liutf'
    version '0.0.1-SNAPSHOT'
    
    apply plugin: 'java'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }

  • EurekaServerApplication.java 启动类中添加注解:@EnableEurekaServer

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaServerApplication.class, args);
    	}
    }

  • application.yml 添加配置

    server:
      port: 8761
    
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          
  • 启动服务,访问 http://localhost:8761/

创建服务提供方

  • 创建一个Module,命名为eureka-client 。创建过程同上。

  • EurekaClientApplication.java 启动类添加注解:@EnableEurekaClient @RestController

  • 添加一个hi方法,用于后续验证提供的服务。

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class EurekaClientApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaClientApplication.class, args);
    	}
    
    	@Value("${server.port}")
    	String port;
    
    	@GetMapping("/hi/{name}")
    	public String hi(@PathVariable String name) {
    		return "hi "+name+",i am from port:" +port;
    	}
    
    }

  • application.yml 配置文件添加配置

    配置本应用端口为8762,名为service-hi的应用以微服务形式注册到Eureka的服务注册地址 http://localhost:8761/eureka/

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    
    server:
      port: 8762
    
    spring:
      application:
        name: service-hi
  • 启动服务。访问http://localhost:8762/hi/jack ,Controller正常返回。

  • 再访问Eureka: http://localhost:8761/ ,此时,我们的服务已注册到Eureka,注册完成。

You can’t perform that action at this time.