spring-boot微服务配置

微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

1、maven settings.xml设置jdk版本1.7及以上

<profile>
<id>jdk‐1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>

一、Hello World

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}

 版本管理中心,里面有的可以不用写版本号

二、配置文件

使用一个全局的配置文件,配置文件名是固定的:application.properties、application.yml

YAML语法:
1、以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

2、属性和值也是大小写敏感;

3、双引号;不会转义字符串里面的特殊字符:name: "zhangsan \n lisi":输出;zhangsan 换行 lisi

4、'':单引号;会转义特殊字符:name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

5、注意缩进,长度随意,同一级要对齐

map

friends:
   lastName: zhangsan
   age: 20

行内写法

friends: {lastName: zhangsan,age: 18}

list

pets:
‐ cat
‐ dog
‐ pig

行内
pets: [cat,dog,pig]

读取配置:

person:
   lastName: hello
   age: 18
   boss: false
   birth: 2017/12/12
   maps: {k1: v1,k2: 12}
   lists:
     ‐ lisi
     ‐ zhaoliu
   dog:
     name: 小狗
     age: 12
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

     private String lastName;
     private Integer age;
     private Boolean boss;
     private Date birth;
     private Map<String,Object> maps;
     private List<Object> lists;
     private Dog dog;

 需要导入这个,在添加配置时,就会有提示

!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
<dependency>
     <groupId>org.springframework.boot</groupId>
         <artifactId>spring‐boot‐configuration‐processor</artifactId>
         <optional>true</optional>
</dependency>

如果配置是中文,乱码。调整如图:

//指定路径下的配置
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
@Configuration
public class MyAppConfig {
     //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
     @Bean
     public HelloService helloService02(){
         System.out.println("配置类@Bean给容器中添加组件了...");
         return new HelloService();
     }
}

随机数

${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}

占位符获取之前配置的值,如果没有可以是用:指定默认值 

person.last‐name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15

激活指定profile

1、在配置文件中指定 spring.profiles.active=dev

2、命令行:java -jar 0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;

3、虚拟机参数;-Dspring.profiles.active=dev

server:
port: 8081
spring:
profiles:
active: prod
‐‐‐
server:
port: 8083
spring:
profiles: dev
‐‐‐
server:
port: 8084
spring:
profiles: prod #指定属于哪个环境

配置文件加载顺序:

在指定位置优先加载带profiles的配置

优先级由高到底,高优先级的配置会覆盖低优先级的配置,同时不同配置项会形成互补;

–file:./config/
–file:./
–classpath:/config/
–classpath:/

运行包指定配置文件

java -jar 0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties

猜你喜欢

转载自blog.csdn.net/qq_35418518/article/details/89220317