首頁>技術>

本篇文章講解一下 如何將資料庫檔案放到配置中心中,並且啟動專案之後,如果修改配置中心內容,在不重啟專案的基礎上,更新配置,載入最新配置喜歡的小夥伴記得點個關注
Apollo搭建2.1 建立ApolloPortalDB

在MySQL中建立一個名為ApolloPortalDB的資料庫,並匯入SQL檔案(SQL檔案在Apollo官方檔案中,可以去官方下載,如果不知道的小夥伴可以點選關注私信我)

2.2 建立ApolloConfigDB

在MySQL中建立一個名為ApolloConfigDB的資料庫,並匯入SQL檔案(SQL檔案在Apollo官方檔案中,可以去官方下載,如果不知道的小夥伴可以點選關注私信我)

2.3 配置demo.sh

\apollo-build-scripts-master\apollo-build-scripts-master\demo.sh

2.4 啟動

在\apollo-build-scripts-master\apollo-build-scripts-master 中右鍵---》git bash here(我使用的是window系統配置的,需要安裝git)

Administrator@PC-20201002XZXA MINGW64 /d/AAAAAA/apollo-build-scripts-master/apollo-build-scripts-master $ ./demo.sh start

此時可以訪問 http://127.0.0.1:8080

客戶端http://127.0.0.1:8070/signin

2.5 配置build.bat 並啟動

將apollo1的jar包編譯到maven倉庫中。

D:\AAAAAA\apollo-master\apollo-master\scripts\build.bat

2.6 springboot專案中匯入依賴
 <dependency>            <groupId>com.ctrip.framework.apollo</groupId>            <artifactId>apollo-client</artifactId>            <version>1.0.0</version>        </dependency>        <dependency>            <groupId>com.ctrip.framework.apollo</groupId>            <artifactId>apollo-core</artifactId>            <version>1.0.0</version>        </dependency>
2.7 apollo-env.properties連線配置中心
local.meta=http://127.0.0.1:8080dev.meta=http://127.0.0.1:8080fat.meta=${fat_meta}uat.meta=${uat_meta}lpt.meta=${lpt_meta}pro.meta=${pro_meta}

######

2.9 springboot中新增META-INF/app.properties
app.id=aaa001
2.10 修改系統settings
C:\opt\settings\server.properties(Windows)檔案,設定env為DEV:env=DEV
2.11 啟動類

此時我們搭建好了apollo,可以在springboot專案中使用。

啟動類添加註解@EnableApolloConfig

@SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableCircuitBreaker@EnableApolloConfigpublic class App{    public static void main( String[] args )    {        SpringApplication.run(App.class,args);    }}

此時我們可以這樣讀取apollo的配置資訊

@RestController@RequestMapping("/stu")public class StudentController {  /***     讀取配置中心的配置  */     @Value("${myname:heihei}")     private String  haha;         @GetMapping("listAll")     @HystrixCommand(fallbackMethod = "listAllFallBackMethod")     public Result listAll(){         return  Result.success(haha);     } }

如果完成到這一步,基本上就算成功了。

此時如果我們希望將資料庫的配置放到配置中心中。

我們可以將資料庫的配置資訊放到配置中心中,例如:

spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis-plus.type-aliases-package=com.aaa.entitymybatis-plus.mapper-locations=classpath:mapper/*.xmlmybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

此時我們需要一個配置類,來監聽遠端配置中心的內容,並且實時更新

@Configuration@EnableConfigurationProperties(DataSourceProperties.class)public class DataSourceConfig {        @Autowired    ApplicationContext context;    @Autowired    private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;    @ApolloConfigChangeListener()    private void onChange(ConfigChangeEvent changeEvent) {        DataSourceProperties dataSourceProperties = context.getBean(DataSourceProperties.class);        changeEvent.changedKeys().stream().forEach(s -> {          // 注意 我此時只修改了password  如果需要全部都修改的小夥伴 可以多加幾個判斷            if (s.contains("spring.datasource.password")) {                dataSourceProperties.setPassword(changeEvent.getChange(s).getNewValue());            }        });        boolean dataSource = refreshScope.refresh("dataSource");        System.out.println("-----------------" + dataSource);    }    @RefreshScope    @Primary    @Bean    public DataSource dataSource(DataSourceProperties dataSourceProperties) {               return dataSourceProperties.initializeDataSourceBuilder().build();    }}

6
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 教你如何動態解析JSON