springCloud学习笔记(二)——eureka服务提供者
一、构建项目
1. 引入jar包
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
2. 完整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>com.springcloud</artifactId><groupId>com.sc2020</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-eureka-client7002</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 引入自定义的api通用包,可以使用Payment支付Entity --><dependency><groupId>com.sc2020</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- boot web actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 一般通用配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><!-- 热部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!-- 单元测试 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>
3.yml配置文件内容
server:port: 7002eureka:instance:hostname: eureka7001.com #eureka服务端的实例名称instance-id: client7002 #服务中心中具体区分的名称prefer-ip-address: true #访问路径可以显示IP地址lease-renewal-interval-in-seconds: 1 #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除client:register-with-eureka: true #false表示不向注册中心注册自己fetch-registry: true #false表示自己端就是注册中心service-url:defaultZone: http://eureka7001.com:7001/eureka/ #单机# defaultZone: http://eureka7001.com:7001/eureka #,http://eureka7002.com:7002/eureka #集群server:# 关闭自我保护机制,保证不可用服务被及时剔除enable-self-preservation: falseeviction-interval-timer-in-ms: 2000spring:application:name: cloud-eureka-client #服务中心中注册服务名称
注意:eureka7001.com配置类hosts文件才成效,未配置使用localhost
4.Application,main方法
@SpringBootApplication@EnableEurekaClientpublic class CloudEurekaClient7002Application {public static void main(String[] args) {SpringApplication.run(CloudEurekaClient7002Application.class, args);}}
声明使用@EnableEurekaClient组件,。
注意:和服务注册中心注解不同一个为@EnableEurekaClient,一个为@EnableEurekaServer
5. 控制层接口
@Value("${server.port}")String port;/*** description: 负载均衡验证* version: 1.0* date: 2021/11/16 14:37* author: xiaYZ* iteration: 迭代说明** @param* @return*/@GetMapping("getPortMessage")public String getPortMessage() {return "端口号:" + port + ",请求接口!";}
二、项目运行
1.运行截图

三、总结
本章,只说明了如何向服务注册中心注册服务,下一章说明如何创建服务消费者,调用服务。
