使⽤Idea简单快速搭建springcloud项⽬的图⽂教程⽬录
⼀.搭建⽗项⽬
⼆.搭建eureka-server注册中⼼
三.搭建提供者服务
四.搭建消费者服务
五.实现服务之间的调⽤
前⾔:
开发⼯具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建⼀套spring cloud微服务系统,实现服务之间的调⽤。
需要搭建⼀个⽗⼯程springcloud-test,⼀个服务注册中⼼eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中⼼。
⼀.搭建⽗项⽬
2.
3.
(1)删掉src⽬录
(2)定义l⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>st</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>
⼆.搭建eureka-server注册中⼼
springcloud和springboot1.
2.
3.
4.
5.
6.
(1)定义l⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<!--引⼊⽗⼯程依赖-->
<parent>
<groupId>st</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>st</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</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.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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
(2)删掉test⽂件夹(⾃⼰设置,可有可⽆)
(3)启动加注解@EnableEurekaServer(开启eureka服务)
package st.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloudflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
(4)定义配置⽂件
配置⽂件改为l(可有可⽆,看⾃⼰喜好)
server:
port: 8080
spring:
application:
#应⽤名称(在注册中显⽰的)
name: eureka-server
eureka:
client:
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
#实例是否在eureka服务器上注册⾃⼰的信息以供其他服务发现,默认为true,即⾃⼰注册⾃⼰。
register-with-eureka: true
#与Eureka注册服务中⼼的通信zone和url地址
serviceUrl:
#localhost:8080/eureka/eureka
defaultZone: ${eureka.instance.hostname}:${server.port}/eureka
#服务注册中⼼实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8080
server:
#设为false,关闭⾃我保护,即Eureka server在云⼼光器件会去统计⼼跳失败⽐例在15分钟之内是否低于85%,如果低于85%,EurekaServer
#会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者⾮正常下线了,此时服务消费者会拿到⼀个⽆效的服务 #实例,此时调⽤会失败,对于这个问题需要服务消费者端有⼀些容错机制,如重试、断路器等;
enable-self-preservation: false
#扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
(5)测试
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论