Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

初学springcloud-笔录,详细介绍看原文链接。这里直接上调试通过后代码

eclipse  Version: 2018-12 (4.10.0)

spring-boot :   2.1.1.RELEASE

spring-cloud:    Greenwich.RELEASE

JDK: 1.8

apache-maven-3.3.9

--------------------

一、创建Eureka Server ,新建maven工程: erurekaserver

1、编写 pom.xml



  4.0.0
  com.wg
  eurekaserver
  0.0.1-SNAPSHOT
  jar
  springcloud.helloworld.Eureka.server
  Demo Spring Eureka Server

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

  
    UTF-8
    UTF-8
    1.8
    Greenwich.RELEASE
  

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

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

    
    
      org.springframework.cloud
      spring-cloud-starter
    


    
      org.springframework.boot
      spring-boot-starter-data-redis
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    
    
      io.projectreactor
      reactor-test
      test
    
  

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

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

2、编写EurekaServerApplication.java

package wg;


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

@EnableEurekaServer
@SpringBootApplication
class EurekaServerApplication {

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

 3、编写application.yml

server:
  port: 8761

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

4、启动项目

访问http://localhost:8761

Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)_第1张图片

 

二、创建Eureka Client

1、编写 pom.xml




  4.0.0

  wg
  eurekaclient1
  1.0-SNAPSHOT

  eurekaclient1
  
  http://www.example.com

  
    UTF-8
    UTF-8
    Greenwich.RELEASE
    1.8
  

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

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

    
    
      org.springframework.cloud
      spring-cloud-config-client
    

    
    
      org.springframework.boot
      spring-boot-starter-web
    
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud.version}
        pom
        import
      
    
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

2、编写EurekaServerApplication.java

package wg;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Value("${server.port}")
    String port;

    @RequestMapping("/")
    public String home() {
        return "hello world from port " + port;
    }
}

 3、编写application.yml

server:
  port: 8762
spring:
  application:
    name: eurekaclient1
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4、启动项目

访问http://localhost:8762

Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)_第2张图片

5、访问http://localhost:8761

Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)_第3张图片

项目截图:

Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)_第4张图片

Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE)_第5张图片

下一篇:Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)(Greenwich.RELEASE)

转载于:https://my.oschina.net/pipi1919/blog/3059048

你可能感兴趣的:(Spring Cloud 入门教程(一): 服务注册与发现(Eureka)(Greenwich.RELEASE))