实现服务消费者
- 创建项目与服务提供者一样。
- 添加依赖
<?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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Eureka Consumer Demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置资源文件application.properties
#“服务消费者”的名称
spring.application.name=consumer
#服务消费者的端口号
server.port=9000
#不注册到服务中心
eureka.client.register-with-eurek=false
#服务中心地址
eureka.client.serviceUrl.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/,http://node3:8083/eureka/
在启动类中,添加注解以启用客户端的服务注册和发现功能
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
//启用客户端的服务注册和发现功能
@EnableDiscoveryClient
//启用Feign的远程服务调用RPC
@EnableFeignClients
public class EurekaConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerDemoApplication.class, args);
}
}
调用“服务提供者”接口
Feign是一个声明式Web Service客户端,使用Feign能让编写Web Service客户端更加简单,具体步骤为:
- 添加Feign的依赖
- 在启动类中加入注解@EnableFeignClients
- 定义一个接口,在该接口中添加注解以使用它。
package com.example.demo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name="provider")
public interface MmqFeignClient {
@RequestMapping(value = "/hello")
public String hello();
}
实现客户端接口
package com.example.demo.controller;
import com.example.demo.MmqFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: mumanquan
* @date: 2021/9/30 15:31
* Description:
*/
@RestController
public class ConsumerController {
//注入接口
@Autowired
MmqFeignClient mfCli;
@RequestMapping("/hello")
public String index(){
//返回内容
return mfCli.hello();
}
}
启动服务
java -jar demo-0.0.1-SNAPSHOT.jar
注:Linux系统上若想后台运行这些指令,则按照以下格式运行
nohup java -jar demo-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &
若需停掉服务,则使用ps -aux | grep node…找到对应进程后,执行kill 123关闭进程。
验证结果
最后,在浏览器中输入http://localhost:9000/hello即可返回Provider提供的数据,刷新页面,会发现provider1/provider2交替出现。
最后查看整个服务中心的状态,可以看到整个服务状态。例如有哪些提供者、消费者等。