SpringCloud2020版本配置與環(huán)境搭建教程詳解
1、maven父子工程搭建
項目使用maven工程搭建,下面是工程的結構圖。SpringCloud2020是父工程,僅負責依賴的管理,eureka是注冊中心的服務端,testclient是測試的客戶端。
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運行的端口號 address: 127.0.0.1 #注冊中心運行地址 servlet: context-path: /server #eureka注冊中心管理界面地址 eureka: client: register-with-eureka: false #是否加入eureka注冊表 fetch-registry: false #還是向eureka請求注冊信息表 service-url: defaultZone: http://${server.address}:${server.port}/eureka #注冊中心地址,其它服務需要注冊到該地址
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的端口對應 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、啟動類
3.1 Eureka啟動類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啟動類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、運行結果
如果沒有意外,那么你將看到
如果啟動testclient時報錯
請檢查testclient工程的依賴中是否存在下面的依賴項,如果沒有,請?zhí)砑?。原因可能是eureka-client依賴spring-boot-starter-web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果沒有出現TEST_SERVICE,并且testclient出現以下報錯
請檢查testclient配置的defaultZone是否與eureka配置對應,并清空已經構建的內容,再重新啟動eureka,testclient。
在testclient控制臺看到以下日志信息,說明注冊成功。
訪問管理界面默認使用127.0.0.1:port
,如果要改變它,請按照下面的提示配置
server: port: 20001 #eureka運行的端口號 address: 127.0.0.1 #管理界面的地址 servlet: context-path: /eureka-ui#管理界面的context-path eureka: client: register-with-eureka: false #是否加入eureka注冊表 fetch-registry: false #是否向eureka請求注冊信息表 service-url: defaultZone: http://127.0.0.1:${server.port}/eureka # 配置注冊中心的地址,其它服務注冊的時候使用。
到此這篇關于SpringCloud2020版本配置與環(huán)境搭建教程詳解的文章就介紹到這了,更多相關SpringCloud2020版本配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringCloud使用Zookeeper作為配置中心的示例
- SpringCloud-Alibaba-Sentinel-配置持久化策略詳解
- SpringCloud2020 bootstrap 配置文件失效的解決方法
- SpringCloud2020整合Nacos-Bootstrap配置不生效的解決
- spring cloud gateway使用 uri: lb://方式配置時,服務名的特殊要求
- Springcloud GateWay網關配置過程圖解
- Springcloud Eureka配置及集群代碼實例
- SpringCloud Config配置加密解密用法解析
- Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置的教程
- 淺談springcloud常用依賴和配置
相關文章
淺談Java中常用數據結構的實現類 Collection和Map
下面小編就為大家?guī)硪黄獪\談Java中常用數據結構的實現類 Collection和Map。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09