SpringCloud2020版本配置與環境搭建教程詳解
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秒發送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不發送就過期 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工程的依賴中是否存在下面的依賴項,如果沒有,請添加。原因可能是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版本配置與環境搭建教程詳解的文章就介紹到這瞭,更多相關SpringCloud2020版本配置內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringCloud如何搭建一個多模塊項目
- SpringCloud eureka(server)微服務集群搭建過程
- SpringCloud服務註冊和發現組件Eureka
- springcloud gateway無法路由問題的解決
- SpringCloud整合Consul的實現