springCloud微服务系列——配置中心第二篇——简单搭建

目录

一、简介

二、服务端

三、客户端


一、简介

       这篇文章简单总结如何搭建配置中心

二、服务端

   pom配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

  bootstrap.yml或application.yml文件配置,一般建议把spring.application.name这种类似的信息配置到bootstrap中,git信息可以配置到application.yml文件中

spring: 
  application: 
    name: config-server
  cloud: 
    config: 
      server: 
       git: 
        uri: https://github.com/wulinfeng2/serverConfig
        username: xxx #如果企业是付费用户
        password: xxx #如果企业是付费用户
        searchPaths: aciv-cf #项目根路径
        clone-on-start: true #启动的时候拉取配置文件,而不是需要的时候,这样可能使启动变慢,第一次加载配置变快
        force-pull: true #配置文件在本地会有一个备份,设置为true,始终从服务端获取,防止脏数据

  在启动类上加上@EnableConfigServer注解

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

三、客户端

   pom配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
    
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>

   bootstrap.yml文件配置,注意必须将以下配置配置到bootstrap.yml中,不能配置到application.yml中。bootstrap.yml在application.yml之前加载,用来加载外部资源。

spring: 
  application: 
    name: config-demo
  cloud: 
    config: 
      uri: http://@localhost:8868/manage/serverConfig
      name: demo #{描述}-{环境}.yml中的描述,可以配置多个
      label: master #github的label,用来做版本控制
      fail-fast: true #如果不能连接到配置中心,则无法启动
      retry: 
        max-attempts: 6 #最大重试次数
      request-read-timeout: 6000 #读取配置超时时间
  profiles: 
    active: dev

  在启动类上加上@EnableDiscoveryClient注解

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

}

 

### 回答1: import xgboost as xgb from sklearn.grid_search import GridSearchCV# 设置参数列表 param_grid = { 'max_depth': [3, 4, 5], 'learning_rate': [0.01, 0.1, 0.2], 'n_estimators': [200, 400, 600], 'subsample': [0.8, 1.0], 'colsample_bytree': [0.8, 1.0] } # 使用GridSearchCV进行搜索 xgb_model = xgb.XGBClassifier() grid_search = GridSearchCV(xgb_model, param_grid, verbose=1, cv=5) grid_search.fit(X_train, y_train) # 输出最优参数 best_parameters = grid_search.best_params_ print(best_parameters) ### 回答2: XGBoost是一种常用的梯度提升树算法,可以用于分类和回归问题。调参是优化模型性能的关键步骤。下面是一个关于XGBoost机器学习模型调参的Python代码示例: ```python import xgboost as xgb from sklearn.datasets import load_boston from sklearn.model_selection import GridSearchCV, train_test_split from sklearn.metrics import mean_squared_error # 载入数据集 data = load_boston() X, y = data.data, data.target # 划分训练集和验证集 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=42) # 定义模型 model = xgb.XGBRegressor() # 定义要搜索的超参数范围 param_grid = { 'n_estimators': [50, 100, 200], 'max_depth': [3, 4, 5], 'learning_rate': [0.1, 0.01, 0.001] } # 网格搜索调参 grid = GridSearchCV(model, param_grid, scoring='neg_mean_squared_error', cv=5) grid.fit(X_train, y_train) # 输出最佳参数和最佳得分 print("Best Parameters: ", grid.best_params_) print("Best Score: ", -grid.best_score_) # 使用最佳参数的模型进行预测 best_model = grid.best_estimator_ y_pred = best_model.predict(X_valid) # 计算均方误差 mse = mean_squared_error(y_valid, y_pred) print("Mean Squared Error: ", mse) ``` 在这个示例中,我们首先导入了必要的库,包括xgboost、sklearn.datasets等。然后我们使用`load_boston`函数载入一个波士顿房价的数据集,并将其划分为训练集和验证集。 接下来,我们定义了一个XGBoost回归模型,并定义了我们要搜索的超参数范围。在这个示例中,我们搜索了三个超参数:n_estimators(弱学习器的个数)、max_depth(树的最大深度)和learning_rate(学习率)。 然后,我们使用`GridSearchCV`函数进行网格搜索调参。其中,`scoring`参数指定了评估指标(负均方误差),`cv`参数指定了交叉验证的折数。 最后,我们输出了最佳参数和最佳得分。然后,使用最佳参数的模型进行预测,并计算了均方误差。 这是一个简单的示例,实际调参可能需要更的超参数和更复杂的搜索策略,但以上代码可以作为一个起点帮助你进行XGBoost模型调参。 ### 回答3: xgboost是一种强大的机器学习模型,但在使用过程中需要调参来优化模型的性能。下面是一个关于xgboost机器学习模型调参的Python代码示例: ```python import xgboost as xgb from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error from sklearn.model_selection import GridSearchCV # 载入数据 boston = load_boston() X, y = boston.data, boston.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 构建xgb模型 xgbr = xgb.XGBRegressor() # 设置需要调参的参数 parameters = {'nthread': [4], 'objective': ['reg:squarederror'], 'learning_rate': [0.1, 0.01], 'max_depth': [3, 5, 7], 'min_child_weight': [1, 3, 5], 'subsample': [0.6, 0.8], 'colsample_bytree': [0.6, 0.8], 'n_estimators': [100, 200] } # 使用GridSearchCV进行调参 grid_search = GridSearchCV(estimator=xgbr, param_grid=parameters, scoring='neg_mean_squared_error', cv=5, n_jobs=-1) grid_search.fit(X_train, y_train) # 输出最佳参数和最佳得分 best_parameters = grid_search.best_params_ best_score = grid_search.best_score_ print("Best parameters: ", best_parameters) print("Best score: ", best_score) # 使用最佳参数训练模型 xgbr_best = xgb.XGBRegressor(**best_parameters) xgbr_best.fit(X_train, y_train) # 预测并计算均方误差 y_pred = xgbr_best.predict(X_test) mse = mean_squared_error(y_test, y_pred) print("Mean Squared Error: ", mse) ``` 以上代码使用了xgboost模型对波士顿房价数据进行预测,通过GridSearchCV调参获取最佳参数,并使用最佳参数训练模型,最后输出了预测结果的均方误差。你可以根据自己的需要,根据实际情况修改代码中的参数范围和评估指标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值