SpringCloud2020版本配置与环境搭建教程详解
程序员文章站
2022-06-24 09:49:57
1、maven父子工程搭建项目使用maven工程搭建,下面是工程的结构图。springcloud2020是父工程,仅负责依赖的管理,eureka是注册中心的服务端,testclient是测试的客户端。...
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版本配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 下拉菜单的若干种实现方法
下一篇: vue中配置scss全局变量的步骤