spring cloud【进阶二】

使用spring boot 搭建spring cloud :

   使用spring boot搭建spring cloud 的原因是 spring boot 是spring cloud 的基础,另外如自动化配置,快速开发,轻松部署等,非常适合用作微服务架构中各项具体微服务的开发框架。可以轻松整合spring cloud 实现系统服务化.

使用IDEA创建项目过程:

File--new--project--spring Initializr--1.8[jdk]--https://start.spring.io--next--packageName 和项目名--next--cloud config---config client--next--finish

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.didispace</groupId>
   <artifactId>hello</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>hello</name>
   <description>hello</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.16.16</version>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>
HelloApplication【启动类】


package com.didispace;


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


@SpringBootApplication
public class HelloApplication {


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


HelloController 【控制层】
package com.didispace.web;


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


/**
 * @author 罗显
 * @date 2018/3/13
 */
@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String index(){
        return "hello world";
    }
}


TestController【得到配置文件中的值测试类】


package com.didispace.web;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 罗显
 * @date 2018/3/13
 * 该类主要用于测试,不用于其他
 */
@RestController
@Slf4j
public class TestController {
    @Value("${girl.name}")
    private String girlName;


    @Value("${concat.word}")
    private String concatWord;


    @Value("${com.didispace.value}")
    private String longValue;


    @RequestMapping("/girl")
    public String getGrilNameMethod(){
        log.info("girl的名字是:"+girlName+"\n"+"配置文件拼接得到的内容是:"+concatWord+"\n"+"随机字符串:"+longValue);
        return "gril";
    }
}


application.properties


#具体加载哪个配置文件
#加载测试环境
spring.profiles.active=test


application-test.properties


#容器端口号
server.port=3333
#指定应用名
spring.application.name=hello


#自定义属性
girl.name=zhangjie
love.name=wutong
miss.name=zhangxiaopan
baby.name=baobao
#配置文件的内容可以拼接
concat.word=${baby.name} love ${girl.name} and ${love.name} and ${miss.name}
#关于随机的应用
#随机字符串
com.didispace.value=${random.value}
#随机int
com.didispace.number=${random.int}
#随机long
com.didispace.long =${random.long}
#随机10以内的数字
com.didispace.randomNumber=${random.int(10)}
#随机10-20之间的数字
com.didispace.randomBetweenNumber=${random.int[10,20]}


#用来关闭应用的端点,只需要访问应用的/shutdown 就能实现关闭改应用的远程操作。由于开放关闭应用
#的操作本身是危险操作,所以真正在线上使用需加入保护机制,比如定制actuator的端点路径,整个spring
#security 进行安全校验。

endpoints.actuator.enabled=true

访问:http://10.168.1.35:3333/hello ,返回:hello world
访问:http://10.168.1.35:3333/health,返回健康节点json 字符串:
{
 "status":"up",
 "diskspace":{
  "status:":"up",
  "total":491270434816,
  "free":383870214144,
  "threshold":10485760
  }
}

pom文件见解:
   
   parent配置指定为spring-boot-starter-parent:该父项定义了spring boot 版本依赖以及一些模式配置内容,如application.properties的位置等。
   
   spring-boot-starter-web:全栈web开发模块,包含嵌入式tomcat,springmvc
   
   spring-boot-starter-test:通用测试模块,包含junit,hamcrest,mockito.
   
   spring-boot-starter-data-jpa:访问数据库
   spring-boot-starter-jdbc:[同上,访问数据库]
   
   创建项目:
   File--new--module---Spring Initialize----https://start.sping.io----prejectName---packageName---web(选中)---finish
   
   容器端口号:server.port=
   指定应用名:spring.application.name=hello
   
   监控与管理:为了保证软件的高可用,需要对这些指标信息加监控和预警规则。

猜你喜欢

转载自blog.csdn.net/qq_35781178/article/details/80050661