EureKaSpringApplication:java
package com.crow.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @SpringBootApplication @EnableEurekaServer public class EureKaSpringApplication { public static void main(String[] args) { SpringApplication.run(EureKaSpringApplication.class, args); } /** * 而後會發現其餘路徑訪問不了的,好比向eureka註冊服務,註冊不進來。 * 那是由於springboot在這個版本默認開啓了CSRF攻擊防護,2.x其餘版本不少也會存在此問題。 * 網上的解決辦法是禁用CSRF防護,禁用以後部分版本會出如今登陸eureka界面的時候又沒有安全登陸驗證了,要註冊服務禁用/eureka便可,而不是直接禁用CSRF * * @author Administrator * */ @Configuration @EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // // Spring Security 默認開啓了全部 CSRF 攻擊防護,須要禁用 /eureka 的防護 http.csrf().ignoringAntMatchers("/eureka/**"); // 訪問eureka控制檯和/actuator時能作安全控制 super.configure(http); // http.csrf().disable();//禁用CSRF // http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } } }
application.properties配置:git
#在默認設置下,Eureka服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲。 #服務註冊中心端口號 server.port=7070 #服務註冊中心實例的主機名 eureka.instance.hostname=localhost #是否向服務註冊中心註冊本身 eureka.client.register-with-eureka=false #是否檢索服務 eureka.client.fetch-registry=false #服務註冊中心的配置內容,指定服務註冊中心的位置 eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/ # 安全認證的配置 #熱部署生效 spring.devtools.restart.enabled=false #用戶名 spring.security.user.name=admin # 用戶密碼 spring.security.user.password=admin123456 spring.application.name= eureka spring.devtools.restart.enabled=false #本機測試下關閉自我保護機制 eureka.server.enableSelfPreservation=false
pom.xml配置:github
<!-- eureka 信息面板安全驗證 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- actuator監控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
ConfigSpringBootApplication:web
package com.crow.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class ConfigSpringBootApplication { public static void main(String[] args) { SpringApplication.run(ConfigSpringBootApplication.class, args); } }
application.properties:spring
server.port=7072 # 優先註冊IP地址而不是hostname eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://admin:admin123456@localhost:7070/eureka/ #配置GitHub 私有倉庫 HTTP 克隆地址 spring.cloud.config.server.git.uri={你的倉庫地址} #配置你的 github賬號 spring.cloud.config.server.git.username={用戶名或郵箱} #配置你的github賬號密碼 spring.cloud.config.server.git.password={密碼} #克隆配置文件存儲地址(注意會項目清空這個目錄,設置目錄需謹慎!!!!) spring.cloud.config.server.git.basedir=C:/Users/Administrator/Desktop/config #git倉庫配置文件分支(默認即爲master) spring.cloud.config.label=master #Git倉庫路徑下搜索路徑(多個路徑用逗號分隔),即你的倉庫目錄 spring.cloud.config.server.git.search-paths=safe-service,safe-gateway # 配置中心經過git從遠程git庫,有時本地的拷貝被污染,這時配置中心沒法從遠程庫更新本地配置,設置force-pull=true,則強制從遠程庫中更新本地庫 spring.cloud.config.server.git.force-pull=true spring.cloud.bus.trace.enabled= true #注意得本身安裝rabbitmq spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=crow9527 #支持動態刷新 #打開bus/refresh刷新開關 #actuator配置 management.endpoints.enabled-by-default=true management.endpoint.health.show-details=always #management.endpoints.web.exposure.include=refresh,info,health management.endpoints.web.exposure.include=* management.endpoints.web.base-path=/actuator #本機服務名 spring.application.name=safe-config-service #服務信息 info.app.name= safe-config-service info.app.message=config info.company.name= abzykj info.build.artifactId= @project.artifactId@ info.build.version= @project.version@
pom.xml:json
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <!--配置中心監控 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
我是在碼雲上新建的屬性文件 bootstrap
服務端訪問測試:安全
http://192.168.50.100:7072/safe-test.properties springboot
或 http://192.168.50.100:7072/safe-test.json服務器
或 http://192.168.50.100:7072/safe-test.yml
或 http://192.168.50.100:7072/safe/test (這個是根據你屬性文件的命名來的 例如:${application}-${profiles}.properties safe-dev.properties safe-test.properties)
ConfigClientSpringBootApplication:
package com.crow.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ConfigClientSpringBootApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientSpringBootApplication.class, args); } }
application.properties:
#springboot 在啓動時候. 會先去加載bootstrap.properties 中的配置, 從gtihub上加載配置下來. 再啓動 server.port= 7073 #服務名 spring.application.name=safe-config-client #方式一:經過服務名訪問服務器 #Config服務端服務名 #spring.cloud.config.discovery.service-id=safe-config-service #支持註冊中心訪問Config服務端 #spring.cloud.config.discovery.enabled=true #方式二:配置中文件的地址 經過uri訪問配置服務 spring.cloud.config.uri=http://localhost:7072/ #git倉庫配置文件分支(默認即爲master) spring.cloud.config.label=master spring.cloud.config.name=safe-test #git倉庫配置文件環境信息 spring.cloud.config.profile=test #得本身安裝rabbitmq spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=crow9527 spring.cloud.bus.trace.enabled= true
pom.xml:
<!--Spring Cloud Config 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
ConfigController: 測試查看屬性文件
package com.crow.client.controller; import java.nio.charset.StandardCharsets; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * <p></p> * @author:Crow * @Date: 2019年10月14日 下午4:13:34 */ @Controller //開啓更新功能 @RefreshScope @RequestMapping("/config") public class ConfigController { @Value("${user.name}") private String value; @RequestMapping("/get") @ResponseBody public String getValue() {
//由於若是配置文件中,要是有中文的話這裏轉換一下 //return new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
return new String(value);
}
}
客戶端訪問測試:
測試在線刷新 :
1.更改碼雲上屬性文件類容(記得要提交)
2.經過actuator手動刷新http://localhost:7073/actuator/bus-refresh(能夠在碼雲上的WebHooks中配置自動刷新)
3.從新訪問客戶端 http://192.168.50.100:7072/safe-test.properties
OK