SpringCloud2020版本配置與環(huán)境搭建教程詳解
1、maven父子工程搭建
項(xiàng)目使用maven工程搭建,下面是工程的結(jié)構(gòu)圖。SpringCloud2020是父工程,僅負(fù)責(zé)依賴的管理,eureka是注冊(cè)中心的服務(wù)端,testclient是測(cè)試的客戶端。
1.1 父工程pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SpringCloud2020</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>eureka</module> <module>testclient</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!-- Provide the latest stable Spring Cloud release train version (e.g. 2020.0.0) --> <version>2020.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1.2 eureka子工程pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud2020</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
1.3 testclient子工程pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud2020</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>testclient</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--引入WebStart--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2、配置application
2.1 eureka 配置
server: port: 20001 #eureka運(yùn)行的端口號(hào) address: 127.0.0.1 #注冊(cè)中心運(yùn)行地址 servlet: context-path: /server #eureka注冊(cè)中心管理界面地址 eureka: client: register-with-eureka: false #是否加入eureka注冊(cè)表 fetch-registry: false #還是向eureka請(qǐng)求注冊(cè)信息表 service-url: defaultZone: http://${server.address}:${server.port}/eureka #注冊(cè)中心地址,其它服務(wù)需要注冊(cè)到該地址
2.1 testclient 配置
server: port: 20002 # Spring spring: application: name: test_service # Eureka eureka: client: service-url: defaultZone: http://127.0.0.1:20001/eureka #這里的port與eureka的端口對(duì)應(yīng) instance: lease-renewal-interval-in-seconds: 5 # 每隔5秒發(fā)送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不發(fā)送就過期 prefer-ip-address: true instance-id: ${spring.application.name}:${server.port}
3、啟動(dòng)類
3.1 Eureka啟動(dòng)類EurekaApplication
package org.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3.2 TestClient啟動(dòng)類TestClientApplication
package org.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class TestClientApplication { public static void main(String[] args) { SpringApplication.run(TestClientApplication.class, args); } }
4、運(yùn)行結(jié)果
如果沒有意外,那么你將看到
如果啟動(dòng)testclient時(shí)報(bào)錯(cuò)
請(qǐng)檢查testclient工程的依賴中是否存在下面的依賴項(xiàng),如果沒有,請(qǐng)?zhí)砑印T蚩赡苁莈ureka-client依賴spring-boot-starter-web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果沒有出現(xiàn)TEST_SERVICE,并且testclient出現(xiàn)以下報(bào)錯(cuò)
請(qǐng)檢查testclient配置的defaultZone是否與eureka配置對(duì)應(yīng),并清空已經(jīng)構(gòu)建的內(nèi)容,再重新啟動(dòng)eureka,testclient。
在testclient控制臺(tái)看到以下日志信息,說明注冊(cè)成功。
訪問管理界面默認(rèn)使用127.0.0.1:port
,如果要改變它,請(qǐng)按照下面的提示配置
server: port: 20001 #eureka運(yùn)行的端口號(hào) address: 127.0.0.1 #管理界面的地址 servlet: context-path: /eureka-ui#管理界面的context-path eureka: client: register-with-eureka: false #是否加入eureka注冊(cè)表 fetch-registry: false #是否向eureka請(qǐng)求注冊(cè)信息表 service-url: defaultZone: http://127.0.0.1:${server.port}/eureka # 配置注冊(cè)中心的地址,其它服務(wù)注冊(cè)的時(shí)候使用。
到此這篇關(guān)于SpringCloud2020版本配置與環(huán)境搭建教程詳解的文章就介紹到這了,更多相關(guān)SpringCloud2020版本配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud使用Zookeeper作為配置中心的示例
- SpringCloud-Alibaba-Sentinel-配置持久化策略詳解
- SpringCloud2020 bootstrap 配置文件失效的解決方法
- SpringCloud2020整合Nacos-Bootstrap配置不生效的解決
- spring cloud gateway使用 uri: lb://方式配置時(shí),服務(wù)名的特殊要求
- Springcloud GateWay網(wǎng)關(guān)配置過程圖解
- Springcloud Eureka配置及集群代碼實(shí)例
- SpringCloud Config配置加密解密用法解析
- Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動(dòng)刷新配置的教程
- 淺談springcloud常用依賴和配置
相關(guān)文章
Java正則表達(dá)式驗(yàn)證固定電話號(hào)碼符合性
這篇文章主要介紹了Java正則表達(dá)式驗(yàn)證固定電話號(hào)碼符合性的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)類 Collection和Map
下面小編就為大家?guī)硪黄獪\談Java中常用數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)類 Collection和Map。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Java TCP網(wǎng)絡(luò)通信協(xié)議詳細(xì)講解
TCP/IP是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,它會(huì)保證數(shù)據(jù)不丟包、不亂序。TCP全名是Transmission?Control?Protocol,它是位于網(wǎng)絡(luò)OSI模型中的第四層2022-09-09java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù)
RedisTemplate是Spring Data Redis提供的一個(gè)用于操作Redis的模板類,本文主要介紹了java實(shí)現(xiàn)RedisTemplate操作哈希數(shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09JAVA正則表達(dá)式提取key-value類型字符值代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA正則表達(dá)式提取key-value類型字符值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-10-10Java?Maven構(gòu)建工具中mvnd和Gradle誰更快
這篇文章主要介紹了Java?Maven構(gòu)建工具中mvnd和Gradle誰更快,mvnd?是?Maven?Daemon?的縮寫?,翻譯成中文就是?Maven?守護(hù)進(jìn)程,下文更多相關(guān)資料,需要的小伙伴可以參考一下2022-05-05