zoukankan      html  css  js  c++  java
  • springboot+springcloud config

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

    1. springconfig server

    1.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-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 配置

    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分支}访问 ,并可以展示里面内容即可。

    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 启动类

    @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.yml

    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.yml

    server:
      port: 55591
      
    spring: 
      application: 
        name: asfood-ketchup
      profiles:
        active: dev
    
    #日志
    logging:
      file: ./logs/ketchup.log
    
    management:
      security: 
        enabled: false   #actuator是否需要安全保证 默认为true  不加会报错

    现在启动服务端,后启动客户端,可以访问就正常了,

    如果客户端启动报错:找不到所配置的读取的文件中的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 

  • 相关阅读:
    mysqladmin
    Android project structure in Eclipse
    Android System Architecture
    The Origins of Data Mining
    Ubuntu 12.04 ‘can not lock /etc/shadow try again later’
    20122013QS计算机专业世界大学排名
    What is Data Mining
    HOW to login MYSQL, Help, and Select Database
    C++函数指针与C#委托之间有何联系
    How to download codes from Google Code
  • 原文地址:https://www.cnblogs.com/moly/p/7978269.html
Copyright © 2011-2022 走看看