Eureka Server及Eureka Client样例及源码

一、Eureka Server
1、maven依赖

    
    

    4.0.0

    com.ding
    eureka-server
    0.0.1-SNAPSHOT
    jar

    eureka-server
    Eureka Server for Spring Cloud

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.M9
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

2、启动类:

package com.ding.eureka.server;

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);
    }
}

3、配置文件application.yml

server:
  port: 8761

eureka:
  server:
    #关闭自我保护,从而会保证注销微服务
    enable-self-preservation: false
    # 清理时间间隔(单位毫秒,默认60*1000)
    eviction-interval-timer-in-ms: 3000
  client:
    # 表示是否将自已注册到Eureka Server,默认为true,因为自已就是Eureka Server,故设为false
    register-with-eureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka,不需要同步其它的Eureka Server节点数据,故设为false
    fetch-registry: false
logging:
  level:
    com:
      netflix:
        eureka: on
        discovery: on

4、这样,一个Eureka Server就写完了,启动,通过浏览器访问:http://localhost:8761/

二、Eureka Client端
1、pom文件



    4.0.0

    com.ding
    eureka-client
    0.0.1-SNAPSHOT
    jar

    eureka-client
    Eureka Client for Spring Cloud

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            test
        

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

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.M9
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    



2、配置文件application.yml

spring:
  application:
    name: a-bootiful-client
  cloud:
    client:
      ipAddress: 192.168.0.50
server:
  port: 8080

eureka:
  instance:
    # eureka client向eureka server发送心跳的时间间隔,默认30秒,将此值改小可解决eureka注册慢的问题
    lease-renewal-interval-in-seconds: 5
    # 续约到期时间(默认90秒)
    lease-expiration-duration-in-seconds: 5
    # 将instance id设置成IP:端口的形式
    # 说明:通过试验,spring.cloud.client.ipAddress及server.port要配置文件中必须有值
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

3、启动类:

package com.ding.eureka.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List serviceInstancesByApplicationName(@PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }

    @RequestMapping("/test")
    public String test() {
        return "hello";
    }
}

三、测试
1、启动服务端
2、启动客户端
3、访问http://localhost:8761/

Eureka Server及Eureka Client样例及源码_第1张图片
image.png

4、代码详请参见: https://github.com/dhappy05/spring-cloud.git中的eureka目录

你可能感兴趣的:(Eureka Server及Eureka Client样例及源码)