Eureka服务注册与发现

Eureka主要可以将各类Spring boot微服务状态进行维护,微服务端注册到 Eureka服务端,服务消费者可以将从Eureka获取各类服务地址,具体细节不多说,网上已经很多了。

为了战争Eureka的服务注册和发现,要准备启动两个Spring boot工程

Eureka服务端搭建

pom.xml

 
        
            org.springframework.cloud
            spring-cloud-starter-config
        

        
            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
            
        
    

application.yml

server:
   port: 8761

eureka:
   instance:
       hostname: localhost
   client:
       registerWithEureka: false #服务端不主动去注册和发现
       fetchRegistry: false
       serviceUrl:
           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类添加注解@EnableEurekaServer,表明是Eureka服务器

package com.example.eurka.eurkaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurkaServerApplication {

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

}

访问http://localhost:8761/,就可以看到下面,Eureka服务端已启动

Eureka服务注册与发现_第1张图片

Eureka客户端搭建

pom.xml


    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter
        

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

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

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

application.properties

#eureka服务器地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
server.port= 8762
#服务名称
spring.application.name= service-helloworld

启动类添加客户端注解@EnableEurekaClient

package com.example.eurekaclient;

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

}

新建一个Controller

package com.example.eurekaclient.controller;

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

@RestController
public class TestController {

    @Value("${server.port}")
    String serverPort;
    @GetMapping("/test")
    public String test() {
        return "调用服务的端口号为:" + serverPort;
    }
}

启动后,打开http://localhost:8761/,可以看到已经看到客户端的配置名称和端口号了

Eureka服务注册与发现_第2张图片

你可能感兴趣的:(Spring,boot,Spring,Cloud)