spring cloud入门(Eureka client和Eureka server)

spring cloud分为好几个模块,现在我先学习neflix模块,这个模块是spring cloud的核心模块,首先使用maven将eureka client和eureka server的相关架包下载下来:

eureka server:



   4.0.0

   com.example
   springcloud
   0.0.1-SNAPSHOT
   jar


   springcloud
   springcloudtest

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

   
      UTF-8
      UTF-8
      1.8
   

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

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

   
      
         
            org.springframework.cloud
            spring-cloud-dependencies
            Dalston.RC1
            pom
            import
         
      
   

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

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


eureka client:



    4.0.0

    eurekaclient
    eurekaclient.com
    1.0-SNAPSHOT
    
        
            
                org.springframework.cloud
                spring-cloud-netflix
                1.4.6.BUILD-SNAPSHOT
                pom
                import
            
        
    
    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    
    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/libs-snapshot
        
            true
        
    


 

将这两个maven同时下载下来之后我们开始对eureka server进行部署 :

      首先,我们先在resouce下面生成一个application.yml文件,因为是spring boot,所以一切都是自动配置的,我们配置文件如下:

server:
  port: 8790

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里面,其实eureka server也是一个eureka client,这里面defaultZone表示他发送的注册的地址,我这里defaultZone的地址写的是它自己的地址,如果这个地址写的是其他 eureka server地址那么就会形成一个双注册机的情况,同时如果registerWithEureka和fetchRegistry为false的时候表示不去注册,注意这样就形成不了双注册机情况。这里的server.port表示这个程序申请的端口,然后我们创建一个启动类,

package com.example.demo;


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

@EnableEurekaServer
@SpringBootApplication
public class SpringcloudApplication {

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

@EnableEurekaServer和@SpringBootApplication两个注释不能或缺,一个是eureka server启动注解一个是spring boot启动注解,等启动完成我们可以访问localhost:8790。得到如下图:

spring cloud入门(Eureka client和Eureka server)_第1张图片

这就表示我们的eureka server配置成功了。

接下来是eureka client的配置文件:

server:
  port: 8060
  servletPath: /
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8790/eureka/
    enabled: true
  instance:
    healthCheckUrlPath: ${server.servletPath}/health
    statusPageUrlPath: ${server.servletPath}/info
    homePageUrl: ${server.servletPath}/
    healthcheck:
      enabled: true
spring:
  application:
    name: eureka-hello

这里eureka.enabled必须是true,否则就不会去想eureka server注册中心发送注册信息了,eureka.client.service-url.defaultZone写eureka server的注册地址。

同样创建一个启动类:

package com.eurekaclient.app;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 用户 on 2018/7/6.
 */
@RestController
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
    @Value("service.port")
    private String port;
    @Autowired
    private EurekaClient discoveryClient;
    public static void main(String[] args){
        new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
    }
    @RequestMapping("/hi")
    public String hello(@RequestParam String name){
        return "hello world,"+name+","+"I am from port:"+port;
    }

    @RequestMapping("/info")
    public String info(){
        return "hello, I am eureka client, i am from prot:"+port;
    }

    @RequestMapping("/health")
    public String health(){
        return "i am health,I am from port:"+port;
    }
    @RequestMapping("/instance")
    public String instance(){
        InstanceInfo instanceInfo = discoveryClient.getNextServerFromEureka("EUREKA-HELLO",false);
        return instanceInfo.getHomePageUrl();
    }
}

 @RestController表示这是一个resful的controller的注解 @SpringBootApplication表示启动springboot @EnableEurekaClient表示开启eurekaclient类。完成启动后我们可以在eureka server的页面上看到:

这里的EUREKA-HELLO就是上面配置文件中对应的spring.application.name,好了到这里我们就完成了eurekaclient和eurekaserver的配置了。 

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