隨着服務數量的增多,尤爲是多數項目涉及jni本地方法的調用,所需參數配置較多,同時內存溢出等維護問題時常發生.鑑於此,原tomcat集羣的使用已難知足需求,而微服務的思想契合當前項目實踐,特在服務端構建起高可用eureka_server註冊中心集羣/config_server配置中心集羣,完成對應用和git配置文件的管理.同時考慮到服務器集羣併發清洗數據的必要性,構建起了ribbon+zuul負載均衡集羣(後續完成)並在實踐中效果顯著.總體而言,微服務的引用改善了項目開發和平常維護/迭代過程紛亂的現狀.特整理註冊中心和配置中心構建過程博客以下:git
new Module -> Spring Initializr --- Module SDK: 1.8 + Initializr Service URL - Defaultspring
-> Project Metadata:完成Group + Artifact + Version + Package的配置docker
-> Dependencies:bootstrap
-> SpringBoot - 1.5.19tomcat
-> Cloud Discovery - Eureka Server安全
-> Finish服務器
構建過程基於 IDEA新建Maven模塊流程併發
新增 @EnableEurekaServer 註解便可.app
@EnableEurekaServer @SpringBootApplication public class RosettaEurekaServer1Application { public static void main( String[] args ) { SpringApplication.run(RosettaEurekaServer1Application.class,args); } }
注意:此處構建爲 eureka_server 集羣 , 故而開發對自身的註冊(單實例設置爲false便可),同時默認註冊中心爲另外一實例路徑,具體配置以下: 負載均衡
server: port: 8761 eureka: client: register-with-eureka: true # Eureka Server向本身註冊 fetch-registry: true service-url: defaultZone: http://127.0.0.1:8762/eureka server: enable-self-preservation: false wait-time-in-ms-when-sync-empty: 0 spring: application: name: resetta_eureka_server jackson: time-zone: GMT+8
結合4,配置以下 ( 注意: 實例名相同 ):
server: port: 8762 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://127.0.0.1:8761/eureka server: enable-self-preservation: false wait-time-in-ms-when-sync-empty: 0 spring: application: name: resetta_eureka_server jackson: time-zone: GMT+8
同 二 - 2 , 只在 pom依賴環節改動以下:
Dependencies:
-> Spring Boot - 1.5.19
-> Cloud Discovery - Eureka Discovery
Finish
新增 @EnableDiscoveryClient 註解
@EnableDiscoveryClient @SpringBootApplication public class EurekaClientTestApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientTestApplication.class, args); } }
注意:因由多個註冊中心,故在配置時指定多個實例,具體以下:
eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka, http://127.0.0.1:8762/eureka server: port: 8777 spring: application: name: eureka_client cloud: config: allow-override: true override-system-properties: false # 在eureka集羣中,需注意關閉安全組件 management: security: enabled: false
注:idea有一好用的實例啓動視圖 - RunDashbord . 若是實例啓動未加載至該視圖 , 可作如下修改
a. 找到項目根目錄下 .idea - workspace.xml 配置文件
b. 找到文件夾下的 RunDashboard 組件, 設置 配置類型( configurationTypes )爲 ( SpringBootApplicationConfigurationType )
c. 最終該組件配置以下:
<component name="RunDashboard"> <option name="configurationTypes"> <set> <option value="SpringBootApplicationConfigurationType" /> </set> </option> <option name="ruleStates"> <list> <RuleState> <option name="name" value="ConfigurationTypeDashboardGroupingRule" /> </RuleState> <RuleState> <option name="name" value="StatusDashboardGroupingRule" /> </RuleState> </list> </option> </component>
d.Run Dashboard 視圖啓動效果以下:
項目啓動完畢後,進入 localhost:8761 / localhost:8762 查看各實例運行狀態,截圖以下:
配置中心的構建,有些分歧,這裏暫提供行之有效的終版也是簡略版.
同 二 - 2 , 只在 pom依賴環節改動以下:
Dependencies:
-> Spring Boot - 1.5.19
-> Cloud Config - Config Server
-> Cloud Discovery - Eureka Discovery
新增 @EnableConfigServer / @EnableEurekaClient 註解 :
@EnableEurekaClient @EnableConfigServer @SpringBootApplication public class RosettaConfigServer1Application { public static void main(String[] args) { SpringApplication.run(RosettaConfigServer1Application.class, args); } }
eureka: client: service-url: defaultZone: http://172.18.28.100:8761/eureka, http://172.18.28.100:8762/eureka server: port: 8768 spring: application: name: rosetta_config_server cloud: config: server: git: uri: git@*.*.*.*:~/git/rosetta_cloud_config.git # 管理配置文件git服務器端路徑 search-paths: test # 對應的配置文件路徑 username: git password: 123456 label: master # 分支
同 3 , 除 端口不一樣
Configuration Server 端點與配置文件的映射規則以下:
/{applicaiton}/{profile}[/{label}] /{appllication}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
啓動配置中心實例,根據上述規約測試配置信息以下:
pom依賴方面, 因須要時常刷新配置,故需採用 config client 結合 actuator的 /refresh的使用方式.依賴以下:
Dependencies:
Spring Boot - 1.5.19
Cloud Config - Config Client
Cloud Discovery - Eureka Discovery
Ops - Actuator
此處只需保留 @EnableEurekaClient 便可
application.yml
management.security.enabled 置爲 false , 免去調用/refresh時的權限驗證
eureka: client: service-url: defaultZone: http://172.18.28.100:8761/eureka, http://172.18.28.100:8762/eureka spring: application: name: test-config server: port: 8771 management: security: enabled: false
bootstrap.yml
spring: cloud: config: fail-fast: true label: master profile: dev discovery: enabled: true service-id: rosetta_config_server
@Component @ConfigurationProperties(prefix = "test") @RefreshScope public class PersonConfigRemote { private String husband; private String wife; public String getHusband() { return husband; } public void setHusband(String husband) { this.husband = husband; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } }
@RestController @RequestMapping("/test") public class TestConfig { @Autowired private PersonConfigRemote personConfigRemote; @GetMapping public String test(){ return personConfigRemote.getHusband() + " --- " + personConfigRemote.getWife(); } }
Spring提供了@ConfigurationProperties註解,能夠將配置屬性映射到一個JavaBean , 並且 Actuator 導出 /refresh 服務 , 每當調用這個服務的時候,被@ConfigurationProperties標註的Bean就會刷新屬性值 .
注 : /refresh 爲 POST 請求.
關於 Spring Cloud Bus + rabbit mq
新增pom:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
應用配置以下:
spring: application: name: rosetta_config_server cloud: bus: trace: enabled: true # 開啓cloud bus的跟蹤 rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest management: security: enabled: false
rabbit mq 安裝使用 :
docker -> rabbit mq
docker pull rabbitmq:3-management docker run -d --hostname localhost --name myrabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management 參數說明: -d 後臺進程運行 hostname RabbitMQ主機名稱 name 容器名稱 -p port:port 本地端口:容器端口 -p 15672:15672 http訪問端口 -p 5672:5672 amqp訪問端口
注:映射2個端口:15672是Web管理界面的端口;5672是MQ訪問的端口。
實際應用中,調用 Config Client實例的 /bus/refresh 端點 , 注意 : POST 請求