前言:
开发工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服务系统,实现服务之间的调用。
需要搭建一个父工程springcloud-test,一个服务注册中心eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中心。
一.搭建父项目
在这里插入图片描述
2.
在这里插入图片描述
3.
在这里插入图片描述

(1)删掉src目录
在这里插入图片描述
(2)定义pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.1</version><relativePath/> <!-- lookup parent from repository -->
</parent><groupId>com.chen.test</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version>
</properties>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 二.搭建eureka-server注册中心 1. 在这里插入图片描述 2. 在这里插入图片描述 3. 在这里插入图片描述 4. 在这里插入图片描述 5. 在这里插入图片描述 6. (1)定义pom.xml文件 <?xml version="1.0" encoding="UTF-8"?>

4.0.0

<!--引入父工程依赖-->
<parent><groupId>com.chen.test</groupId><artifactId>springcloud-test</artifactId><version>1.0-SNAPSHOT</version>
</parent><groupId>com.chen.test</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><project.reporting.outputEncoding>UTF-8</project.reporting.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>https://repo.spring.io/milestone</url></repository>
</repositories>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(2)删掉test文件夹(自己设置,可有可无)
(3)启动加注解@EnableEurekaServer(开启eureka服务)

package com.chen.test.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(4)定义配置文件
配置文件改为application.yml(可有可无,看自己喜好)

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:
#http://localhost:8080/eureka/eureka
defaultZone: http://eureka.instance.hostname:{eureka.instance.hostname}: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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(5)测试
在这里插入图片描述
三.搭建提供者服务
1.
在这里插入图片描述
2.
在这里插入图片描述
3.
在这里插入图片描述
4.
在这里插入图片描述
5.
在这里插入图片描述
6.
(1)定义pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

com.chen.test
springcloud-test
1.0-SNAPSHOT

<groupId>com.chen.test</groupId>
<artifactId>cloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-provider</name>
<description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.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-client</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>https://repo.spring.io/milestone</url></repository>
</repositories>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(2)删除test文件加(可有可无)
(3)修改配置文件为application.yml(可有可无)

spring:
application:
name: cloud-provider
server:
port: 8081

eureka:
client:
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
register-with-eureka: true
service-url:
#defaultZone 这个是不会提示的,此处需要自己写
#实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
#但是自己还需要写一个defaultZone;
defaultZone: http://localhost:8080/eureka
#服务注册中心实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8081

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(4)启动类加注解@EnableEurekaClient

package com.chen.test.cloudprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class CloudProviderApplication {

public static void main(String[] args) {SpringApplication.run(CloudProviderApplication.class, args);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(5)测试
在这里插入图片描述
四.搭建消费者服务
1.
在这里插入图片描述
2.
在这里插入图片描述
3.
在这里插入图片描述
4.
在这里插入图片描述
5.
在这里插入图片描述
(1)定义pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

com.chen.test
springcloud-test
1.0-SNAPSHOT

<groupId>com.chen.demo</groupId>
<artifactId>cloud-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-client</name>
<description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.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-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.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url></repository>
</repositories>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(2)删除test文件夹(可有可无)
(3)修改配置文件为application.yml(可有可无)

server:
#定义端口号
port: 8082
spring:
application:
#定义应用名称,即服务名称
name: cloud-client
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
#服务注册中心实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8082
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(4)启动类加注解@EnableEurekaClient

package com.chen.demo.cloudclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class CloudClientApplication {

public static void main(String[] args) {SpringApplication.run(CloudClientApplication.class, args);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(5)测试
在这里插入图片描述
(6)在父pom中加上

<modules><module>eureka-server</module><module>cloud-provider</module><module>cloud-client</module>
</modules>

1
2
3
4
5
在这里插入图片描述

五.实现服务之间的调用
1.在cloud-provider中创建controller包和service包

package com.chen.test.cloudprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chen.test.cloudprovider.service.HelloService;

@RestController
@RequestMapping("/hello")
public class HelloController {

@Autowired
private HelloService helloService;
@GetMapping("/getHello")
public String getHello(){return helloService.getHello();
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.chen.test.cloudprovider.service;

public interface HelloService {

String getHello();

}

1
2
3
4
5
6
7
package com.chen.test.cloudprovider.service.impl;

import com.chen.test.cloudprovider.service.HelloService;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

@Override
public String getHello() {return "你好兄弟";
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(2)测试
在这里插入图片描述
(3)在cloud-client中创建controller包和service包

package com.chen.demo.cloudclient.controller;

import com.chen.demo.cloudclient.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/Hello")
public class HelloClient {

@Autowired
private HelloService helloService;@GetMapping("/getClient")
public String getClient(){return helloService.getProduct();
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.chen.demo.cloudclient.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = “cloud-provider”,path ="/hello" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface HelloService {

@RequestMapping(value = "getHello")
String getProduct();

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
特别注意
在启动类加上注解@EnableFeignClients

package com.chen.demo.cloudclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CloudClientApplication {

public static void main(String[] args) {SpringApplication.run(CloudClientApplication.class, args);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(4)测试
在这里插入图片描述

使用Idea简单搭建springcloud项目相关推荐

  1. 手把手教你搭建SpringCloud项目(十六)集成Stream消息驱动

    Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...

  2. 手把手教你搭建SpringCloud项目(九)集成OpenFeign服务接口调用

    Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...

  3. 手把手教你搭建SpringCloud项目(十)集成Hystrix之服务降级

    Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...

  4. 搭建springcloud项目报错Non-parseable POM xxxx unexpected markup

    搭建springcloud项目的时候maven依赖POM文件报错了: D:\tools\jdk\jdk8\bin\java.exe -Dmaven.multiModuleProjectDirector ...

  5. 零配置简单搭建SpringMVC 项目

    SpringMVC是比较常用的JavaWeb框架,非常轻便强悍,能简化Web开发,大大提高开发效率,在各种Web程序中广泛应用.本文采用Java Config的方式搭建SpringMVC项目,并对Sp ...

  6. idea搭建springcloud项目_Eureka搭建分布式SpringCloud项目

    @Author:By Runsen SpringCloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册 ...

  7. 快速搭建springcloud项目

    本文讲述springcloud项目从0开始的搭建过程以及其中需要注意的问题,不涉及具体的理论内容以及微服务项目中的五大问题和具体解决办法的插件或手段.五大问题的具体解决办法在本合集中分别进行逐一讲解 ...

  8. 搭建微服务_快速搭建 SpringCloud 微服务开发环境的脚手架

    本文作者:HelloGitHub-秦人 本文适合有 SpringBoot 和 SpringCloud 基础知识的人群,跟着本文可使用和快速搭建 SpringCloud 项目. HelloGitHub ...

  9. 快速搭建 SpringCloud 微服务开发环境的脚手架

    快速搭建 SpringCloud 微服务开发环境的脚手架 本文作者:HelloGitHub-秦人 本文适合有 SpringBoot 和 SpringCloud 基础知识的人群,跟着本文可使用和快速搭建 ...

最新文章

  1. 小米盒子连接不上服务器显示wifi,小米盒子连接不上wifi的原因及解决办法
  2. MySQL之三范式:原子性 唯一性 避免数据冗余
  3. 实时计算 Flink 版总体介绍
  4. ubuntu11.10 64bits机器安装flash方法
  5. gradle入门_Gradle入门:简介
  6. 痛惜!年仅38岁,中科院研究员、博导周传不幸病逝!
  7. mysql数据库下载压缩包_mysql 8.0.22 zip压缩包版(免安装)下载、安装配置步骤详解...
  8. 再获信通院权威认证,优等生华为云GaussDB数据库凭什么?
  9. cdp备份mysql数据库_数据库如何备份与恢复
  10. ArcGIS:ArcToolBox工具使用——提取DEM/DSM中的高程点
  11. 黑马程序员---交通灯管理系统
  12. pls-00302: 必须声明 组件_vue组件
  13. 数控加工仿真系统 - FANUC OI 车床编程笔记(粗略)
  14. 三星s9Android9内测申请链接,国行三星S9/S9+开启安卓9.0内测
  15. 工行银企互联接入详解(1)--流程说明
  16. 微信小程序 手写签名_你竟然还不知道在微信上就可以手写签名、签文件了~
  17. 腾讯官网生成qq在线客服代码
  18. 统计学中的P值与显著性的意义
  19. photoSwipe插件使用
  20. Linux下内核进程、用户进程和轻量级进程(LWP)的理解

热门文章

  1. 差分跳频MATLAB,基于Matlab的短波差分跳频通信仿真设计与实现
  2. 哪些情况下sql索引会失效
  3. 避免使用GroupByKey
  4. java服务器客户端文件,客户端服务器文件传输Java
  5. 南大cssci期刊目录_最新版CSSCI来源期刊目录(2019-2020)及增减变化!【南大核心】...
  6. C语言程序设计double,C语言中double类型数据占字节数为
  7. advice 和 拦截器_ControllerAdvice拦截器
  8. WARNING: IPv4 forwarding is disabled. Networking will not work.
  9. Android访问瓦片地图 费流量,瓦片地图服务在线资源访问总结
  10. 安居客检测到网页抓取_python3爬虫实战-安居客写字楼信息,学会月入上万理所当然...