springcloud 随笔 1

之前公司一直用的都是ssm + dubbo 的后台框架, 后来公司的大神(csdn博客 菩提树下的杨过)极力推荐使用 新的开源框架springcloud,我也就跟风学一下,毕竟会有理解不透彻的地方,欢迎各位读者指正

所有的随笔都记在git的某个项目上 后续肯定会不定时更新 项目地址 
git@github.com:duanyuhan/miracle_cloud.git

准备工作

springcloud的核心是springboot 这点是毋容置疑的。 springboot大大简化了我们的开发和配置成本, 所以我们先温习一下springboot的简单启动步骤
先搭建一个简单的项目吧, 下图是一个简单的项目结构
这里写图片描述

  • 建立启动类 MiracleServiceApplication 这个类不能写在java 目录下,一定要写在某个包里
  • 写一个简单的controller
    下面附上代码
package com.miracle.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by naonao on 18/4/20.
 */
@SpringBootApplication
public class MiracleServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(MiracleServiceApplication.class, args);
    }
}
package com.miracle.service.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by naonao on 18/4/20.
 */
@RestController
@RequestMapping("/miracle")
public class TestController {
    @RequestMapping(path = "/test", method = RequestMethod.GET)
    public String test() {
        return "miracle is naonao";
    }
}

然后指定一下服务的端口号和日志文件目录 以下是application.yml的内容

server:
  port: 6001

logging:
  config: classpath:log4j2.xml

eureka:
  client:
    allow-redirects: false

再有, 为了 区分不同的环境, 我在gradle 文件里做了如下配置

buildscript {
    repositories {
        mavenLocal()
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }

    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
    }
}
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
def env = System.getProperty("env") ?: "dev"
sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources", "src/main/profile/$env"]
        }
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR1"
    }
}
dependencies {
//    compile('org.springframework.cloud:spring-cloud-starter-eureka'){
//        exclude module: "spring-boot-starter-logging"
//    }
    compile 'org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter:2.0.0.RELEASE'


    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

其中这段代码

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources", "src/main/profile/$env"]
        }
    }
}

是指当前配置读哪个 环境, 如果当前服务器没有 环境默认dev

那么现在可以运行启动类的 main方法
然后 在浏览器输入 http://localhost:6001/miracle/test
即可得到一下输出 miracle is naonao

猜你喜欢

转载自blog.csdn.net/weixin_39526391/article/details/80024237