建立eureka服务和客户端(客户端获取已经注册服务)

1. 新建sping boot eureka server

    新建立spring starter  project

    修改pom.xml文件

    在parent后追加

   


    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Camden.SR3
            pom
            import
        
    

    追加

   

                
                     org.springframework.cloud
                      spring-cloud-config-server
               
		
    		org.springframework.cloud
    		spring-cloud-starter-eureka-server
		
应用

package com.example;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringBootEurekaServerApplication {

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



属性文件

server.port=8761

spring.application.name=Spring-Boot-Admin-Web
eureka.instance.hostname=localhost

eureka.client.registerWithEureka=true  #是否将自身注册
eureka.client.fetchRegistry=false    #如果为true,启动时报警.
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2. 建立spring boot eureka client

    pom.xml

   

   	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Camden.SR3
				pom
				import
			
		
	
    
    
    	
		org.springframework.cloud
		spring-cloud-starter-eureka
	
    

应用

package com.example;

import java.util.List;

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.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class SpringBootEurekaClientApplication {

	@Autowired
	private DiscoveryClient discoveryClient;
	
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    
    @RequestMapping("/registered")
    public String getRegistered(){
        List list = discoveryClient.getInstances("STORES");
        System.out.println(discoveryClient.getLocalServiceInstance());
        System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
        
        for( String s :  discoveryClient.getServices()){
        	System.out.println("services " + s);
        	List serviceInstances =  discoveryClient.getInstances(s);
        	for(ServiceInstance si : serviceInstances){
        		System.out.println("    services:" + s + ":getHost()=" + si.getHost());
        		System.out.println("    services:" + s + ":getPort()=" + si.getPort());
        		System.out.println("    services:" + s + ":getServiceId()=" + si.getServiceId());
        		System.out.println("    services:" + s + ":getUri()=" + si.getUri());
        		System.out.println("    services:" + s + ":getMetadata()=" + si.getMetadata());
        	}
        	
        }
        
        System.out.println(list.size());
        if (list != null && list.size() > 0 ) {
            System.out.println( list.get(0).getUri()  );
        }
    	return null;
    }
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBootEurekaClientApplication.class, args);
	}
}

配置文件

    application.properties
    spring.application.name=cloud-simple-service
server.port=8011

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/


你可能感兴趣的:(spring)