第一步:搭建apollomysql
一、去GitHub地址:https://github.com/ctripcorp/apollo/releases地址下載安裝包,而後解壓,在apollo-quick-start\sql目錄下有兩個sql,導入到mysql中(5.6.5++)git
二、打開demo.sh文件,把下面的配置改爲本身的數據庫的用戶名和密碼github
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=adminspring
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=adminsql
三、第2個步驟改完成後,啓動demo.sh文件(cmd中輸入./demo.sh start)數據庫
四、訪問http://localhost:8070,用戶名默認apollo,密碼amin,而後新建一個項目填寫appId(這個appId與項目中的app.id是一致的)api
第二步:搭建項目(這裏寫了三個項目,一個是註冊中心、一個是網關zuul、另外一個是被調用zuul轉發調用的微服務)緩存
網關zuul項目中添加的依賴apollo依賴網絡
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>com.spotify</groupId> <artifactId>apollo-core</artifactId> <version>1.11.0</version> </dependency>
application.properties中配置app
app.id=hello-zuul apollo.meta=http://localhost:8080 #Apollo客戶端會把從服務端獲取到的配置在本地文件系統緩存一份,用於在遇到服務不可用,或網絡不通的時候依然能從本地恢復配置,不影響應用正常運行。本地緩存路徑默認位於如下路徑,因此請確保/opt/data或C:\opt\data\目錄存在,也能夠自定義,以下 apollo.cacheDir=C:\\Users\\yang\\Desktop #zuul.routes.hello-consumer-openfeign.path=/api/** #zuul.routes.hello-consumer-openfeign.serviceId=hello-consumer-openfeign
application.yml中配置
server: port: 10000 spring: application: name: hello-zuul eureka: client: register-with-eureka: false #網關不用註冊到註冊中心中,由於沒其餘微服務調用他 fetch-registry: true #這個必須是true fetchRegistry打開才能從eureka拉取服務列表 service-url: defaultZone: http://localhost:7000/eureka/ zuul: ignored-services: "*" #意味着http請求好比 "/myusers/101" 將跳轉到 "/101" # routes: # hello-consumer-openfeign: # path: /api/** # serviceId: hello-consumer-openfeign # host: # connect-timeout-millis: 3000 # socket-timeout-millis: 3000
主配置類中添加
@EnableApolloConfigz註解
若是要不重啓就自動刷新須要以下配置
@Component @Slf4j public class ApolloConfigChanged implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext=applicationContext; } @ApolloConfigChangeListener private void someChangeHandler(ConfigChangeEvent changeEvent) { for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); log.info("Found change - {}", change.toString()); } // 更新相應的bean的屬性值,主要是存在@ConfigurationProperties註解的bean this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); } }
這樣就配置完成了,其餘eureka、zuul的配置自行搭建
啓動項目測試就ok了