美文网首页我爱编程
SpringCloudConfig配置中心

SpringCloudConfig配置中心

作者: 面具猴 | 来源:发表于2018-06-20 09:23 被阅读98次

    配置中心

    使用 git 注册中心 rabbitmq消息总线 webhooks

    一.创建注册中心:同前

    二.配置服务端

    1.Maven依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

    2.配置文件

    #基础配置
    spring.application.name=weather-config-server
    server.port=8002
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
    #Config
    spring.cloud.config.server.git.uri=https://github.com/mianjuhou/SpringCloudDemo2
    spring.cloud.config.server.git.search-paths=config-repo
    spring.cloud.config.server.git.username=mianjuhou@163.com
    spring.cloud.config.server.git.password=fdaMJH0318
    spring.cloud.config.server.git.skip-ssl-validation=true
    #RabbitMQ
    spring.rabbitmq.host=127.0.0.1
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    

    3.启动类添加注解

    //@EnableConfigServer和@EnableDiscoveryClient
    @SpringBootApplication
    @EnableConfigServer
    @EnableDiscoveryClient
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    

    三.配置客户端

    1.Maven依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

    2.配置文件
    //bootstrap.properties

    #基础配置
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
    #Config配置
    spring.cloud.config.name=neo-config
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    spring.cloud.config.discovery.enabled=true
    spring.cloud.config.discovery.serviceId=weather-config-server
    spring.cloud.config.server.git.skip-ssl-validation=true
    

    //application.properties

    #基础配置
    spring.application.name=config-client
    server.port=9005
    #总线配置
    spring.cloud.bus.trace.enabled=true
    #Rabbitmq配置
    spring.rabbitmq.host=127.0.0.1
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    

    3.启动类注解

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

    4.Controller注解和使用配置变量

    //@RefreshScope允许内部总线刷新
    //@Value("${neo.hello}")获取远程配置值
    @RestController
    @RefreshScope
    public class HelloController {
    
        @Value("${neo.hello}")
        private String hello;
    
        @RequestMapping("/hello")
        public String from() {
            return this.hello;
        }
    }
    

    四.启动rabbitmq

    rabbitmq-server
    

    五.测试URL

    //POST方法
    http://localhost:8040/bus/refresh
    

    配置

    创建一个SpringCloudDemo2工程
    工程下创建一个config-repo目录
    目录下创建三个文件:neo-config-dev.properties,neo-config-pro.properties,neo-config-test.properties
    每个文件中有一个名字为neo.hello的属性

    2.0之后测试时特别注意


    spring.cloud.config.server.git.skip-ssl-validation=true
    代替
    management.security.enabled=false

    相关文章

      网友评论

        本文标题:SpringCloudConfig配置中心

        本文链接:https://www.haomeiwen.com/subject/ktqorftx.html