重点:本项目资源地址请点击:https://download.csdn.net/download/hp_yangpeng/11064773(ps:最好先看文档,跟着做完,然后再下载demo)
1、相关环境
2、项目创建 说明:此处我们会创建一个父项目,其他子项目(生产者、消费者、注册中心)均以module的形式在展示在项目目录中,首先比较符合当前开发规范,其次也比较方便; 1. 创建父项目:springbootdemo,具体截图如下:
注意:此处可以选择一个web依赖,一个Eureka Server依赖
在Idea里我将无关的文件全部隐藏了,另外将test给删除了,具体文件目录如下:
pom.xml 做个修改,具体pom.xml配置如下::
说明:在创建当前项目的时候选择了springcloud的eureka,但是创建结束之后我发现springcloud的版本号为<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
这个我没有细查,所以就直接用下面的配置文件吧,保证是没有问题的;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 1.5.X eureka依赖 -->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>-->
<!--springboot 2.X eureka 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
父项目创建完毕;
2.创建子项目——公共entity项目 springbootdemo-entity
项目结构如下:entity子项目中主要有两个entity,包名为:com.example.springbootdemoentity.entity
;
entity中的字段自己随便写,我的内容如下:
public class Consumer {
private String name;
private int age;
private String add;
private String email;
public Consumer() {
this.name = "name";
this.age = 12;
this.add = "北京市历史互通";
this.email = "6666.qq.com";
}
@Override
public String toString() {
return "Consumer{" +
"name='" + name + '\'' +
", age=" + age +
", add='" + add + '\'' +
", email='" + email + '\'' +
'}';
}
}
/**
* @author YangPeng
* @Title: Product
* @ProjectName springboot
* @Description: TODO
* @date 2019/3/25-16:23
*/
public class Product {
private String name;
private int age;
private String add;
private String email;
public Product() {
this.name = "name";
this.age = 12;
this.add = "北京市历史互通";
this.email = "6666.qq.com";
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", age=" + age +
", add='" + add + '\'' +
", email='" + email + '\'' +
'}';
}
}
entity子项目创建结束!!!
3.创建子项目——注册中心 springbootdemo-eureka 项目目录如下:
<dependencies>
<!-- springboot2.0.*对应的eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- springboot 2.0.* 对应的springcloud依赖管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server:
port: 5060
servlet:
context-path: /eureka
spring:
application:
name: eureka-server
eureka:
client:
#是否启用湖区注册服务信息,因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;
fetch-registry: false
#表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false;
register-with-eureka: true
service-url:
defaultZone: http://localhost:5060/eureka/eureka
instance:
hostname: localhost
server:
#设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
#会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
#实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
enable-self-preservation: false
#扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
启动eureka项目,并访问:http://localhost:5060/eureka ,出现如下页面表示Eureka项目创建成功!
4.创建子项目——生产者 springbootdemo-product
说明:此处删除无用的test文件夹,并将application.properties改为application.yml; pom.xml文件修改:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修改为:
<parent>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
具体配置如下:
<?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>
<parent>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>springbootdemo-product</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo-product</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 将entity依赖添加到product工程中 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootdemo-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 添加springboot fegin依赖,product项目即可以作为生产者,又可以作为消费者-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring:
application:
name: product-server
server:
port: 7002
servlet:
context-path: /product
eureka:
client:
service-url:
#defaultZone 这个是不会提示的,此处需要自己写
#实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
#但是自己还需要写一个defaultZone;
defaultZone: http://localhost:5060/eureka/eureka
@SpringBootApplication
//@EnableEurekaClient 和 @EnableDiscoveryClient 都是让eureka发现该服务并注册到eureka上的注解
//相同点:都能让注册中心Eureka发现,并将该服务注册到注册中心上;
//不同点:@EnableEurekaClient只适用于Eureka作为注册中心,而@EnableDiscoveryClient可以是其他注册中心;
@EnableEurekaClient
//表示开启Fegin客户端
@EnableFeignClients
public class SpringbootProductApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootProductApplication.class, args);
}
}
至此,Product工程创建结束
5.创建子项目——消费者 springbootdemo-cousumer
<parent>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 公共entity依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootdemo-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- fegin客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
server:
servlet:
# 定义项目的访问访问路径
context-path: /consumer
#定义端口号
port: 7001
spring:
# 下面是我整合redis使用的配置,你们此处不需要
# redis:
# cluster:
# expire-seconds: 120
# command-timeout: 5000
# nodes:
# namenode22:6379,datanode23:6379,datanode24:6379,datanode25:6379,datanode26:6379,datanode27:6379
application:
#定义应用名称,即服务名称
name: consumer-server
eureka:
client:
service-url:
defaultZone: http://localhost:5060/eureka/eureka
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringbootdemoConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoConsumerApplication.class, args);
}
}
– 整合fegin步骤:
下图为我服务的接口相关信息:
fegin调用接口信息如下:
/**
* @author YangPeng
* @Title: ProductService
* @ProjectName springbootdemo
* @Description: TODO
* @date 2019/3/27-11:23
*/
//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = "product-server",path ="/product" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface ProductService {
@RequestMapping(value = "getProduct")
String getProduct();
}
@RestController
public class ConsumerController {
@Autowired
private ProductService productService;
@RequestMapping(value = "getConsumer")
public String getConsumer(){
String str = productService.getProduct();
return str;
}
}
访问服务:http://localhost:7001/consumer/getConsumer;如下图所示,已经能够使用fegin调用服务的接口了;
至此,springboot2.1.13(官网最新版本)+springcloud+eureka+fegin进行远程调用的功能已经完成;欢迎各位留言;
重点中的重点:鉴于好多朋友的CSDN账号没有C币,所以我把项目传到github上了,哈哈哈哈,朋友们可以直接去github clone下来,地址如下:https://github.com/yanpgeng/springbootdemo 克隆下来之后直接使用idea工具open这个项目,然后将项目add as maven project即可
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171869.html原文链接:https://javaforall.cn