SpringCloud学习心得(一) 构建最基础的SpringCloud项目

首先本系列博客参照 史上最简单的 SpringCloud 教程,地址:https://blog.csdn.net/forezp/article/details/70148833

根据工作需要,笔者最近研究SpringCloud微服务框架,将最近的学习过程写下来供大家参考。

好,闲话少叙,咱们直接整

首先本系统采用的springcloud的版本是1.5.9 Dalston.SR5

首先建立服务中心,pom文件如下:


  4.0.0
  cn.eric
  eureka-server-eric
  0.0.1-SNAPSHOT
  eurekaserver
 

   
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.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.SR5
                pom
                import
           

       

   


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

       

   

 

配置文件 application.yml

spring:
  application:

    name: eureka-server

server:

  port: 8761

eureka:
  instance:
    hostname: localhost
  server:
    enableSelfPreservation: true
    renewalPercentThreshold: 0.1
  client:
    #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
    registerWithEureka: false
    fetchRegistry: false

 

建立一个启动类:代码如下:

package cn.eric.springcloud;

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

@EnableEurekaServer  //启动一个服务注册中心,只需要一个注解@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

启动服务,在浏览器里面输入:http://localhost:8761

界面如下:

SpringCloud学习心得(一) 构建最基础的SpringCloud项目_第1张图片

 

服务端启动后,我们来建立一个客户端:

pom文件如下:


  4.0.0
  cn.eric
  eureka-server-client-eric
  0.0.1-SNAPSHOT
  eureka-server-client-eric
 
     
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.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.SR5
                pom
                import
           

       

   


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

       

   

    

配置文件如下:

 

spring:
  application:
    name: eureka-server-client1
 
server:
  port: 8762

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

启动类如下:

package cn.eric.springcloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

@EnableEurekaClient
@SpringBootApplication
@RestController
public class EurekaserverClientOneApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverClientOneApplication.class, args);
    }
    
    @Value("${server.port}")
    String port;
    
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        //String port = "8762";
        return "hi "+name+",i am from port:" + port;
    }

}

将客户端启动,在浏览器里打开:http://localhost:8762/hi?name=forezp

 

SpringCloud学习心得(一) 构建最基础的SpringCloud项目_第2张图片

推荐:史上最全的java开发工具类    地址:https://github.com/EricLoveMia/JavaTools

你可能感兴趣的:(JAVA,微服务,SpringCloud,springCloud学习之旅)