spring cloud 第一天

一、创建服务注册中心

  1、首先创建一个maven工程,引入springboot依赖  



    4.0.0

    com.llg
    springcloudlearn
    1.0-SNAPSHOT
    pom

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

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

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

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

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

2、创建服务中心子模块

maven依赖



    
        springcloudlearn
        com.llg
        1.0-SNAPSHOT
    
    4.0.0

    eurekaserver

    

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

    


yml配置文件

server:
  port: 8001

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

spring:
  application:
    name: eurekeserver

启动类

package com.llg;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

启动程序,访问http://localhost:8001/

spring cloud 第一天_第1张图片

 

二、创建客户端

1、创建子模块,引入依赖



    
        springcloudlearn
        com.llg
        1.0-SNAPSHOT
    
    4.0.0

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


2、yml配置文件

server:
  port: 8002

spring:
  application:
    name: client1

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/

3、启动类

package com.llg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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


    @GetMapping("/hi")
    public String sayHi(){
        return "hi, my friend";
    }
}

4、启动程序,刷新http://localhost:8001/

spring cloud 第一天_第2张图片

 

5、访问http://localhost:8002/hi

spring cloud 第一天_第3张图片

6、工程目录结构

spring cloud 第一天_第4张图片

 

三、总结

springcloud使用非常简单,只需引入依赖包、在启动类上加入相应注解和少量的配置即可。

源码路径:

https://github.com/lilianggui/springcloud/tree/master/springcloudlearn

 

 

 

 

 

 

 

 

 

 

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