溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java怎么配置中心服務(wù)化和高可用

發(fā)布時間:2022-01-18 10:51:04 來源:億速云 閱讀:151 作者:iii 欄目:軟件技術(shù)

這篇文章主要介紹“java怎么配置中心服務(wù)化和高可用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“java怎么配置中心服務(wù)化和高可用”文章能幫助大家解決問題。

客戶端和服務(wù)端的耦合性太高,如果server端要做集群,客戶端只能通過原始的方式來路由,server端改變IP地址的時候,客戶端也需要修改配置,不符合springcloud服務(wù)治理的理念。springcloud提供了這樣的解決方案,我們只需要將server端當做一個服務(wù)注冊到eureka中,client端去eureka中去獲取配置中心server端的服務(wù)既可。

server端改造

1、添加依賴

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

需要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。

2、配置文件

server:
server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git倉庫的地址
          search-paths: config-repo                             # git倉庫地址下的相對地址,可以配置多個,用,分割。
          username: username                                        # git倉庫的賬號
          password: password                                    # git倉庫的密碼
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/   ## 注冊中心eurka地址

增加了eureka注冊中心的配置

3、啟動類

啟動類添加@EnableDiscoveryClient激活對配置中心的支持

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

這樣server端的改造就完成了。先啟動eureka注冊中心,在啟動server端,在瀏覽器中訪問:http://localhost:8000/就會看到server端已經(jīng)注冊了到注冊中心了。

java怎么配置中心服務(wù)化和高可用

按照上篇的測試步驟對server端進行測試服務(wù)正常。

客戶端改造

1、添加依賴

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

需要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。

2、配置文件

spring.application.name=spring-cloud-config-client
server.port=8002
 
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
 
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三個配置:

spring.cloud.config.discovery.enabled :開啟Config服務(wù)發(fā)現(xiàn)支持

spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值

eureka.client.serviceUrl.defaultZone :指向配置中心的地址

這三個配置文件都需要放到bootstrap.properties的配置中

3、啟動類

啟動類添加@EnableDiscoveryClient激活對配置中心的支持

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

啟動client端,在瀏覽器中訪問:http://localhost:8000/ 就會看到server端和client端都已經(jīng)注冊了到注冊中心了。

java怎么配置中心服務(wù)化和高可用

高可用

為了模擬生產(chǎn)集群環(huán)境,我們改動server端的端口為8003,再啟動一個server端來做服務(wù)的負載,提供高可用的server端支持。

java怎么配置中心服務(wù)化和高可用

如上圖就可發(fā)現(xiàn)會有兩個server端同時提供配置中心的服務(wù),防止某一臺down掉之后影響整個系統(tǒng)的使用。

我們先單獨測試服務(wù)端,分別訪問:http://localhost:8001/neo-config/dev、http://localhost:8003/neo-config/dev返回信息:

{
    "name": "neo-config", 
    "profiles": [
        "dev"
    ], 
    "label": null, 
    "version": null, 
    "state": null, 
    "propertySources": [
        {
            "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 
            "source": {
                "neo.hello": "hello im dev"
            }
        }
    ]
}

說明兩個server端都正常讀取到了配置信息。

再次訪問:http://localhost:8002/hello,返回:hello im dev update。說明客戶端已經(jīng)讀取到了server端的內(nèi)容,我們隨機停掉一臺server端的服務(wù),再次訪問http://localhost:8002/hello,返回:hello im dev update,說明達到了高可用的目的。

關(guān)于“java怎么配置中心服務(wù)化和高可用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI