spring cloud eureka服务搭建(生产者消费者)
1. Eureka是什么
Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功能了,微服务部署之后,一定要有服务注册和发现的能力,Eureka 就是担任这个角色的。如果你用过 dubbo 的话,那一定知道 dubbo 中服务注册和发现的功能是用 zookeeper 来实现的。
2. Eureka高级架构
如上图所示,其中
Application Server 表示服务提供方
Application Client 表示服务消费方
Make Remote Call 表示远程调用
服务在Eureka上注册,然后每隔30秒发送心跳来更新它们的租约。如果客户端不能多次续订租约,那么它将在大约90秒内从服务器注册表中剔除。注册信息和更新被复制到集群中的所有eureka节点。来自任何区域的客户端都可以查找注册表信息(每30秒发生一次)来定位它们的服务(可能在任何区域)并进行远程调用。
(PS:Eureka Client需要每30秒给Eureka Server发一次心跳,同时更新Server上最新的注册信息到本地,如果Server多次没有收到来自客户端的心跳,那么在90秒内会被Server上剔除)
版本说明
Java : 1.8
Spring Boot : 2.1.5.RELEASE
Spring Cloud: Greenwich.SR1
撸代码
项目结构
父级的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>com.demo</groupId>
<artifactId>spring-cloud-demo</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>eureia/eureka-server</module>
<module>eureia/eureka-client</module>
<module>eureia/eureka-consumer</module>
<module>eureia/eureka-server-peer</module>
<module>eureia/eureka-server-peer2</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
eureka-server(eureka服务端)
pom.xml文件
<?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>spring-cloud-demo</artifactId>
<groupId>com.demo</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>
application.properties
spring.application.name=eureka-server
server.port=20000
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
# 强制关闭服务自保(自动开关不起作用)
eureka.server.enable-self-preservation=false
# 每隔多久触发一次服务剔除
eureka.server.eviction-interval-timer-in-ms=10000
EurekaServerApplication
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaServerApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
启动EurekaServer
http://localhost:20000/
这样Eureka服务端就启动了简简单单。
eureka-client(eureka生产者)
pom.xml文件
<?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>spring-cloud-demo</artifactId>
<groupId>com.demo</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>eureka-client</artifactId>
<name>eureka-client</name>
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
</project>
application.properties
spring.application.name=eureka-client
server.port=30002
# 注册地址(想服务端发送注册指令)
eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/
# 每隔5秒钟,向服务中心发送一条续约指令
eureka.instance.lease-renewal-interval-in-seconds=5
# 如果30秒内,依然没有收到续约请求,判定服务过期(上西天)
eureka.instance.lease-expiration-duration-in-seconds=30
EurekaClientApplication
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
Controller
@RestController
@Slf4j
public class Controller {
Logger log = LoggerFactory.getLogger(Controller.class);
@Value("${server.port}")
private String port;
@GetMapping("/sayHi")
public String sayHi() {
return "This is " + port;
}
@PostMapping("/sayHi")
public Friend sayHiPost(@RequestBody Friend friend) {
log.info("You are " + friend.getName());
friend.setPort(port);
return friend;
}
}
实体类Friend
public class Friend {
private String name;
private String port;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
启动eureka-client(eureka生产者)
这样eureka-client(eureka生产者)就启动成功啦。
eureka-consumer(eureka消费者)
pom.xml文件
<?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>spring-cloud-demo</artifactId>
<groupId>com.demo</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-consumer</artifactId>
<packaging>jar</packaging>
<name>eureka-consumer</name>
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
application.properties
spring.application.name=eureka-consumer
server.port=31000
eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/
EurekaConsumerApplication
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {
@Bean
public RestTemplate register() {
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaConsumerApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
Controller
@RestController
@Slf4j
public class Controller {
Logger log = LoggerFactory.getLogger(Controller.class);
@Autowired
private LoadBalancerClient client;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public String hello() {
ServiceInstance instance = client.choose("eureka-client");
if (instance == null) {
return "No available instances";
}
String target = String.format("http://%s:%s/sayHi",
instance.getHost(),
instance.getPort());
log.info("url is {}", target);
return restTemplate.getForObject(target, String.class);
}
@PostMapping("/hello")
public Friend helloPost() {
ServiceInstance instance = client.choose("eureka-client");
if (instance == null) {
return null;
}
String target = String.format("http://%s:%s/sayHi",
instance.getHost(),
instance.getPort());
log.info("url is {}", target);
Friend friend = new Friend();
friend.setName("Eureka Consumer");
return restTemplate.postForObject(target, friend, Friend.class);
}
}
实体类Friend
public class Friend {
private String name;
private String port;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
启动eureka-consumer(eureka消费者)
这里消费者也注册成功了
测试
消费者通过LoadBalancerClient来调用生产者eureka-client提供的接口
通过调用消费者 @PostMapping("/hello")来消费eureka-client提供的sayHi接口。
成功!
如果有问题请指出!!
https://www.cnblogs.com/cjsblog/p/9858154.html
参考文献
本文地址:https://blog.csdn.net/qq_40297844/article/details/109645804
推荐阅读
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
spring cloud 初步搭建1-1(eureka配置)
-
spring cloud eureka服务搭建(生产者消费者)
-
基于Spring Boot 2.0.2.RELEASE 的 Spring Cloud 速成指南 | 二. Spring Cloud 服务注册中心(Eureka Server)
-
spring cloud 搭建(服务)
-
SpringCloud学习笔记二:Spring Cloud Eureka 服务治理
-
微服务解决方案 -- Spring Cloud Alibaba (三)服务消费者(Feign)
-
微服务解决方案 -- Spring Cloud Alibaba (八)Dubbo 服务消费者
-
Spring Cloud Eureka服务通信Ribbon/Feign(三)
-
服务治理-> Spring Cloud Eureka