java springboot 初体验 (十二)对接eureka
- 上一篇
- java springboot 初体验 (十一)对接mysql
- https://www.cnblogs.com/zwjvzwj/p/16620045.html
- 搭建一个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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zwj</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <lombok.version>1.18.22</lombok.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具。 --> <!-- 常用注解 说明 @Getter/@Setter 此注解在属性或类上,可以为相应的属性自动生成 Getter/Setter 方法,还可以指定访问范围 @ToString 类使用此注解,生成toString()方法 @EqualsAndHashCode 用在类上,生成hashCode()和equals()方法 @Data 注解在类上, 相当于同时使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstrutor这些注解 @NonNull 用在属性或构造器上,对参数是否为空检查 @NoArgsConstructor 生成无参构造函数 @RequiredArgsConstructor 生成不为空的构造函数 @AllArgsConstructor 生成全参构造函数 @Value 用在类上,是@Data的不可变形式,相当于为属性添加final声明,只提供getter方法,而不提供setter方法 @Builder 表示可以进行生成器模式(Builder Pattern)方式初始化 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.3</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>
-
- application.properties或者application.yml添加配置
-
server: port: 8102 servlet: context-path: /eureka spring: application: name: eureka-server eureka: client: fetch-registry: false #表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false; register-with-eureka: true service-url: defaultZone: http://localhost:8102/eureka/eureka instance: hostname: localhost server: enable-self-preservation: false eviction-interval-timer-in-ms: 10000 # 解决Eureka /actuator/info 404错误及不显示info。为了安全,不要配置这个 management: endpoints: web: exposure: include: "*"
-
- 启动文件Application添加注解@EnableEurekaServer
-
package com.zwj.eureka; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @Slf4j @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
-
- 启动服务
- 进入http://localhost:8102/eureka/页面查看
-
- 创建服务
-
服务对接eureka
-
pom文件添加依赖
-
<!-- eureka 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.1.3</version> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
- 启动文件添加@EnableEurekaClient注解
- 启动服务
- 查看服务eureka注册
-
- 下一篇
- java springboot 初体验 (十三)对接esjob
- https://www.cnblogs.com/zwjvzwj/p/16650574.html
本文来自博客园,作者:zwjvzwj,转载请注明原文链接:https://www.cnblogs.com/zwjvzwj/p/16640193.html