我是靠谱客的博主 畅快柜子,最近开发中收集的这篇文章主要介绍Spring Cloud2注册中心Eureka+OpenFeign的使用(二) FeignClient放在提供方,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
一. 公共jar包项目: order-api
1. 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zsw.example</groupId>
<artifactId>order-api</artifactId>
<version>1.3-SNAPSHOT</version>
<name>order-api</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- spring-cloud-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</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>
</project>
2. 服务类接口MessageService
public interface MessageService {
@RequestMapping("/findAllList")
String findAllList();
@RequestMapping("/findById")
String findById();
}
3. 提供调用功能的客户端feignClient
@FeignClient("spring-cloud-message-service")
public interface MessageServiceFeignClient extends MessageService {
}
4. install发布到本地仓库
二. 服务功能实现方项目: spring-cloud-message-service
1. pom.xml 依赖order-api
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zsw</groupId>
<artifactId>spring-cloud-message-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-message-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!--com.zsw.example-order-api-->
<dependency>
<groupId>com.zsw.example</groupId>
<artifactId>order-api</artifactId>
<version>1.3-SNAPSHOT</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
2. 服务实现类MessageServiceImpl(此处必须使用@RestController注解或者@Controller+@ResponseBody, 否则404)
@RestController
public class MessageServiceImpl implements MessageService {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Value(("${server.port}"))
private Integer port;
@Override
public String findAllList() {
System.out.println("MessageServiceImpl findAllList: " + format.format(new Date()));
return "find all message list by empty";
}
@Override
public String findById() {
System.out.println("MessageServiceImpl findById: " + format.format(new Date()));
return "find a message by id";
}
}
3. application.yml配置
# 项目名称
spring:
application:
name: spring-cloud-message-service
server:
port: 8082
eureka:
client:
# 指向eureka注册中心地址,用来给服务端注册使用
service-url:
defaultZone: http://127.0.0.1:8777/eureka
4. -Dserver.port=8083 启动两个spring-cloud-message-service项目做成集群部署
5. 注意: application.yml配置中的项目名称,一定要和order-api那个公共依赖jar包中的feignClient的注解名称一致!
三. 服务调用方项目spring-cloud-vehicle-service
1. pom.xml 依赖order-api
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zsw</groupId>
<artifactId>spring-cloud-vehicle-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-vehicle-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!--com.zsw.example-order-api-->
<dependency>
<groupId>com.zsw.example</groupId>
<artifactId>order-api</artifactId>
<version>1.3-SNAPSHOT</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
2. application.yml
# 项目名称
spring:
application:
name: spring-cloud-vehicle-service
server:
port: 6789
# ribbon配置指定服务提供者的地址,使用了eureka注册中心就不用这个配置了
#spring-cloud-message-service:
# ribbon:
# listOfServers:
# localhost:8082,localhost:8083
eureka:
client:
# 指向eureka注册中心地址,用来给服务端注册使用
service-url:
defaultZone: http://127.0.0.1:8777/eureka
3. 调用测试控制类
@RestController
public class MessageFeignClientController {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private MessageServiceFeignClient messageServiceFeignClient;
@RequestMapping("/findAllList")
public String findAllList() {
System.out.println("MessageFeignClientController findAllList 调用message服务查询信息集合: " + format.format(new Date()));
String allList = messageServiceFeignClient.findAllList();
System.out.println("message服务响应信息: " + allList);
return allList;
}
}
5. 启动类加上注解,开启feignClient并且扫描指定包路径,注意包路径是引用的client路径
@EnableFeignClients("com.zsw.example.openfeign.client")
@SpringBootApplication
public class SpringCloudVehicleServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudVehicleServiceApplication.class, args);
}
}
四. 注册中心项目spring-cloud-eureka-server
1. 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 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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zsw</groupId>
<artifactId>spring-cloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</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-server</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
2. application.yml
server:
port: 8777
eureka:
client:
# eureka启动时注册自身,设置为false否则会报错,启动注册自己还没启动好呢
register-with-eureka: false
# eureka启动时加载服务端的配置信息(地址列表),设置为false否则会报错,启动注册自己还没启动好呢
fetch-registry: false
# 指向eureka注册中心地址,用来给服务端注册使用
service-url:
defaultZone: http://127.0.0.1:8777/eureka
3. 启动类加注解开启注册中心服务
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication {
public static void main(String[] args) {
System.out.println("eureka-server 启动");
SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
}
}
五. 测试
1. 启动注册中心项目spring-cloud-eureka
2. 启动服务提供方项目spring-cloud-message-service(两次两个端口的启动做成集群)
3. 启动服务调用方项目spring-cloud-vehicle-service
4. 注册中心
5. 多次调用测试浏览器展示
6. 多次调用后服务调用方spring-cloud-vehicle-service项目日志
7. 多次调用后服务提供方spring-cloud-message-service项目日志(可以打印出端口号的)
最后
以上就是畅快柜子为你收集整理的Spring Cloud2注册中心Eureka+OpenFeign的使用(二) FeignClient放在提供方的全部内容,希望文章能够帮你解决Spring Cloud2注册中心Eureka+OpenFeign的使用(二) FeignClient放在提供方所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复