项目使用maven工程搭建,下面是工程的结构图。SpringCloud2020是父工程,仅负责依赖的管理,eureka是注册中心的服务端,testclient是测试的客户端。
1.1 父工程pom
<&#63;xml version="1.0" encoding="UTF-8"&#63;>4.0.0 org.example SpringCloud2020 pom 1.0-SNAPSHOT eureka testclient org.springframework.boot spring-boot-starter-parent 2.4.1 UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-dependencies 2020.0.0 pom import org.springframework.boot spring-boot-maven-plugin
1.2 eureka子工程pom
<&#63;xml version="1.0" encoding="UTF-8"&#63;>SpringCloud2020 org.example 1.0-SNAPSHOT 4.0.0 eureka org.springframework.cloud spring-cloud-starter-netflix-eureka-server
1.3 testclient子工程pom
<&#63;xml version="1.0" encoding="UTF-8"&#63;>SpringCloud2020 org.example 1.0-SNAPSHOT 4.0.0 testclient org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web
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.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); } }
如果没有意外,那么你将看到
如果启动testclient时报错
请检查testclient工程的依赖中是否存在下面的依赖项,如果没有,请添加。原因可能是eureka-client依赖spring-boot-starter-web
org.springframework.boot spring-boot-starter-web
如果没有出现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版本配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!