客户端的搭建与注册
上文说到eureka服务中心后,接下来需要介绍以下服务间的调用了。
1.什么是客户端
微服务的每一个业务功能服务都叫做客户端。即每一个被拆分出来的功能模块服务。
2.搭建客户端
客户端就是普通的springboot项目,在其中添加一些注解就被叫做微服务的客户端。
1.新建springboot项目
按照传统方法新建springboot项目。
2.修改pom
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.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.baocl</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> 复制代码
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 复制代码
这里有个坑!!!!最新的client是这个jar包,以前的包和现在的不同,如果eureka上没有注册进去,那么看看是不是这个原因..
3.修改EurekaClientApplication
这里@EnableDiscoveryClient注解说明是spring cloud的客户端。
package com.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } } 复制代码
4.编写接口
然后咱们这里写一个接口,没啥说的。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/aaa") public class DcController { @Value("${server.port}") String port; @GetMapping("/dc") public String dc(@RequestParam(value = "dk") String dk) { // try { // Thread.sleep(100L); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } //String services = "Services: " + discoveryClient.getServices(); //System.out.println(services); // HttpServletRequest request = ((ServletRequestAttributes) // (RequestContextHolder.getRequestAttributes())); // ServletRequestAttributes se = // (ServletRequestAttributes)(RequestContextHolder.getRequestAttributes()); // HttpServletRequest request = se.getRequest(); return "服务提供者端口为" + port + "。。。。消费者端口为" + dk; } } 复制代码
5.修改application.properties
1.必要配置
最后着重说一下配置文件。以下是是必不可少的配置。
spring.profiles.active=dev application-dev.properties spring.application.name=eureka-client server.port=2001 #指定服务端口 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/ 复制代码
2.其他配置解析
发呆时间,即服务续约到期时间(缺省为90s) 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间, #在这个时间内若没收到下一次心跳,则将移除该instance。 (上下两个一起使用) #eureka.instance.lease-expiration-duration-in-seconds=10 心跳时间,即服务续约间隔时间(缺省为30s) #eureka.instance.lease-renewal-interval-in-seconds = 5 #连接 Eureka Server 的超时时间,单位:秒 #eureka.client.eureka-server-connect-timeout-seconds=10 #从Eureka服务器端获取注册信息的间隔时间,单位:秒 默认30 #eureka.client.registery-fetch-interval-seconds=1000 #启动服务注册 默认true #eureka.client.register-with-eureka = true #获取实例时是否过滤,只保留UP状态的实例 默认true #eureka.client.filter-only-up-instances = true 开启健康检查(需要spring-boot-starter-actuator依赖) #eureka.client.healthcheck.enabled = true #获取注册信息并缓存到本地 #eureka.client.fetch-registry= true 复制代码
这里还有一点 我们可以更改application.properties确定我们启动时启动的配置文件,同时可以在启动一个项目后修改application.properties,这样我们自己就可以创建一个集群环境。
然后,我们启动了三次(三个配置的端口号需要不同),这样我们就创建了一个集群了,然后eureka上就可以看到我们客户端的信息了,如下图!
作者:小黄鸡1992
链接:https://juejin.cn/post/7022430150340345864