springboot 集成windows下apollo

1.确保apollo服务可用

 

2.新建配置项目

 

 

3.springboot项目中修改pom.xml


        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.1.0</version>
        </dependency>

4.修改配置(多环境可在不同的yml中引用不同的meta地址)

 

app:
  id: mybatisplus  #在apollo中新建项目,将appId设置为mybatisplus
apollo:
  meta: http://127.0.0.1:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true

 

5.代码修改

启动类加@EnableApolloConfig注解

@Slf4j
@SpringBootApplication
@MapperScan(value="com.test.mybatisplus.mapper")
@EnableApolloConfig
public class MybatisplusApplication {

    public static void main(String[] args) {

        SpringApplication.run(MybatisplusApplication.class, args);

        //NettyServer.run(8080);
        //log.info("======netty服务已经启动========");
    }

}
@Api
@RestController
@RequestMapping("apollo")
@Slf4j
public class ApolloController {


      @Value( "${test}" )
    String test;

    @ApiOperation(value = "apollo",notes = "apollo")
    @RequestMapping(value ="/getApolloInfo" , method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String getApolloInfo(){

        log.info("AppConfigUtil获取配置文件"+AppConfigUtil.get("test"));

        log.info(test);
        return  test;
    }
}

6.请求

 

7.添加监听(只要apollo配置有修改发布,就会打印)注意:修改端口数据库连接之类的项目需要重启

@Slf4j
@Configuration
public class ApolloConfig {

    @com.ctrip.framework.apollo.spring.annotation.ApolloConfig
    private Config config;

    @ApolloConfigChangeListener
    private void configChangeListter(ConfigChangeEvent changeEvent) {
        refreshLoggingLevels();
    }

    @PostConstruct
    private void refreshLoggingLevels() {
        Set<String> keyNames = config.getPropertyNames();
        for (String key : keyNames) {
                String strLevel = config.getProperty(key, "info");
                log.info("{}:{}", key, strLevel);

        }
    }

}

7.1修改test配置

7.2项目输出

 

8.添加工具类AppConfigUtil 获取apollo配置文件

 

public class AppConfigUtil {

    public static String get(String key) {
        return findAppConfigByName(key);
    }

    /**
     * 查找配置 namespace为不同的配置文件名称
     * @param name
     * @return
     */
    public static String findValueByName(String namespace, String name){
        Config appConfig = ConfigService.getConfig(namespace);
        return appConfig.getProperty(name,null);
    }
    /**
     * 查找配置 namespace为不同的配置文件名称
     * 此处默认为application.yml 若为空则默认为 application.properties
     * 非prop的配置文件,必须添加后缀比如 yaml yml
     * @param key
     * @return
     */
    private static String findAppConfigByName(String key){
        return findValueByName("application",key);
    }
}

 

8.1

 @ApiOperation(value = "apollo",notes = "apollo")
    @RequestMapping(value ="/getApolloInfo" , method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String getApolloInfo(){

        log.info("AppConfigUtil获取配置文件"+AppConfigUtil.get("test"));

        log.info(test);
        return  test;
    }

8.2 请求接口输出

9.测试数据库配置

9.1启动成功

 

 

二、根据上面项目做优化

 

1.配置文件全部放到apollo中

spring.profiles.active = dev
test = dsdsds
server.servlet.context-path = /mybatisplus
server.port = 8084
spring.datasource.url = jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.elasticsearch.rest.uris = xxx
spring.elasticsearch.rest.username = xxx
spring.elasticsearch.rest.password = xxx
mybatis-plus.mapper-locations = classpath:mapper/*.xml
mybatis-plus.type-aliases-package = com.test.mybatisplus.Entity
logging.level.com.test.mybatisplus.mapper = debug
zookeeper.node = xxxx
clusterServersConfig.idleConnectionTimeout = 10000
clusterServersConfig.connectTimeout = 10000
clusterServersConfig.timeout = 3000
clusterServersConfig.retryAttempts = 3
clusterServersConfig.retryInterval = 1500
clusterServersConfig.password = null
clusterServersConfig.subscriptionsPerConnection = 5
clusterServersConfig.clientName = null
clusterServersConfig.slaveSubscriptionConnectionMinimumIdleSize = 1
clusterServersConfig.slaveSubscriptionConnectionPoolSize = 50
clusterServersConfig.slaveConnectionMinimumIdleSize = 32
clusterServersConfig.slaveConnectionPoolSize = 64
clusterServersConfig.masterConnectionMinimumIdleSize = 32
clusterServersConfig.masterConnectionPoolSize = 64
clusterServersConfig.readMode = SLAVE
clusterServersConfig.nodeAddresses[0] = redis:/xxx
clusterServersConfig.nodeAddresses[1] = redis://xxx
clusterServersConfig.nodeAddresses[2] = redis://xxx
clusterServersConfig.scanInterval = 1000
threads = 0
nettyThreads = 0

在RedissonUtils 中修改实现方式(只是我自己项目有用)

@Configuration
@Slf4j
public class RedissonUtils {

    @Value("${spring.profiles.active}")
    private String active;
    @Value("${clusterServersConfig.nodeAddresses[0]}")
    private String redisNode1;
    @Value("${clusterServersConfig.nodeAddresses[1]}")
    private String redisNode2;

    @Value("${clusterServersConfig.nodeAddresses[2]}")
    private String redisNode3;

    @Bean
     public RedissonClient redissonClient() {

//方式一
                 Config config = new Config();

                 config.useClusterServers()
                         .setScanInterval(2000)
                         .addNodeAddress(redisNode1, redisNode2)
                         .addNodeAddress(redisNode3);

                RedissonClient redisson = Redisson.create(config);
//方式二
//        log.error("active::::::"+active);
//        RedissonClient redisson =null;
//            try {
//               // Config config1=Config.fromYAML(new ClassPathResource("redisson.yml").getInputStream());
//                Config config1=Config.fromYAML(new ClassPathResource("redisson-"+active+".yml").getInputStream());
//                 redisson = Redisson.create(config1);
//            }catch (Exception e){
//             log.error(e.getMessage());
//            }
              return redisson;
         }

2.新建app.properties

3.新建apollo-env.properties

4.本地启动时候 -Denv=dev  -Dapollo.configService=http://127.0.0.1:8080 自己apollo server地址

5.打的jar包启动 ----                java -Denv=dev -jar mybatisplus-0.0.1-SNAPSHOT.jar

 

6.多环境

 

6.1 在确保多环境服务可用的前提下,修改apollo-env.properties

6.2 本地启动

  -Denv=dev  -Dapollo.configService=http://127.0.0.1:8080 

     -Denv=pro  -Dapollo.configService=http://127.0.0.1:8081 

 

6.3  打的jar包启动 ----             

java -Denv=dev -jar mybatisplus-0.0.1-SNAPSHOT.jar

java -Denv=pro -jar mybatisplus-0.0.1-SNAPSHOT.jar

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值