springboot+springcloud config

参考:sorry,全找不到了,当时没记录,最后后知后觉以为应该记录,因此后面的都有在asfood父项目中的doc文件夹下记录,望见谅。git

1. springconfig server

1.1. pom.xmlgithub

<!-- 父项目以来 -->

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
1.2 启动类
@EnableConfigServer
@SpringBootApplication
public class TomatoApplication {
    public static void main(String[] args) {
        SpringApplication.run(TomatoApplication.class, args);
    }
}

1.3 配置web

server: 
  port: 55590
spring:
  application:
    name: asfood-tomato
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          # 配置git仓库的地址  #访问地址: http://localhost:55590/{filename}/{env}/{branch}
          uri: https://github.com/molyjao/mlims
          # git仓库地址下的相对地址,能够配置多个,用,分割。
          #search-paths: asfoodconfig
          # git仓库的帐号
          #username: #jiu_shaan@163.com
          # git仓库的密码
          #password: 

#配置以后访问git配置须要输入用户名密码
security:
  user:
    name: tomato
    password: tomato

启动工程,若是能够使用 http://localhost:55590/{文件名不带后面环境}/{环境}/{git分支}访问 ,并能够展现里面内容便可。spring

2. springcloud client

2.1 pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </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>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.2 启动类bootstrap

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

控制层:安全

@RestController
@RefreshScope  //刷新配置使用的注解,
public class KetchupController {
    
    //qqname为配置文件的内容的一个key,':'后面是默认值
    @Value("${qqname:defaultqqname}")  
    private String str;

    @RequestMapping("/ketchup")
    String hello() { 
        return "Hello " + str + "!";  
    }
}
2.3 配置文件:两部分bootstrap.yml和application.yml文件,因为bootstrap.yml加载最先,因此须要加载服务端配置文件内的内容须要优先加载。

bootstrap.ymlapp

spring:
  cloud:
    config:
      name: asfood
      profile: dev
      label: master
      uri: http://localhost:55590/
      #discovery:
        enabled: true                        # 默认false,设为true表示使用注册中心中的configserver配置而不本身配置configserver的uri
        serviceId: asfood-tomato            # 指定config server在服务发现中的serviceId,默认为:configserver
      #因为服务端配置了访问须要用户名和密码,因此此处也须要配置
      username: tomato
      password: tomato

application.ymlmaven

server:
  port: 55591
  
spring: 
  application: 
    name: asfood-ketchup
  profiles:
    active: dev

#日志
logging:
  file: ./logs/ketchup.log

management:
  security: 
    enabled: false   #actuator是否须要安全保证 默认为true  不加会报错

如今启动服务端,后启动客户端,能够访问就正常了,spring-boot

若是客户端启动报错:找不到所配置的读取的文件中的key,  xxx placeholder ${xxx}  这个错误就是没有找到配置文件(保证不会手误,key写的不同),若是此时你的服务端的页面访问配置文件,不能访问到配置文件中的内容,这个须要再次百度,若是是服务端能够访问到配置文件中的内容,这个时候须要检查客户端的服务端地址等的配置,检查服务端和客户端启动类的注释,一个是server一个是client还有问题百度吧,我也初学。。。还有个好网站,stackoverflow。网站

自动刷新配置文件访问: 客户端ip:port/refresh

 

有须要能够参考这里(在asfood-ketchup-config-client和asfood-tomato-config-server中):  https://github.com/molyjao/mlims.git 

相关文章
相关标签/搜索