SpringCloud环境搭建
搭建父项目
创建cloud_parent5 项目
修改pom文件
<!--1 确定spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!--2 确定版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>
</properties>
<!-- 3 锁定sprig cloud版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-release.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 4 确定spring cloud私有仓库-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
搭建注册中心(eureka)
步骤一:创建eureka_demo5
步骤二:修改pom,添加web依赖,server依赖
步骤三:创建yml:端口号,服务名,注册地址,关闭(注册自己,拉取列表)
步骤四:创建启动类:添加@EnableEurekaServer (开启注册中心)
修改pom
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
创建yml
#端口号
server:
port: 10086
#服务名
spring:
application:
name: eurekademo5
#注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
register-with-eureka: false #关闭注册自己
fetch-registry: false #关闭拉取列表
创建启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaDemo5Application {
public static void main(String[] args) {
SpringApplication.run(EurekaDemo5Application.class,args);
}
}
搭建User服务
步骤0:创建 user_service
步骤一:修改pom文件,添加web依赖、client依赖
步骤二:创建yml文件:端口号、服务名、注册地址
步骤三:创建启动类,添加开启客户端注解
添加依赖
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring boot监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
创建yml
#端口号
server:
port: 8080
#服务名
spring:
application:
name: userservice
#注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
创建启动类
@SpringBootApplication
@EnableEurekaClient //开启eureka客户端
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
搭建admin服务
步骤0:创建 adminr_service
步骤一:修改pom文件,添加web依赖、client依赖
步骤二:创建yml文件:端口号(9090)、服务名、注册地址
步骤三:创建启动类,添加开启客户端注解
添加依赖
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring boot监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
创建yml
server:
port: 9090
spring:
application:
name: adminservice
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
创建启动类
@SpringBootApplication
@EnableEurekaClient //开启eureka 客户端
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
基本功能 远程调用
user服务(服务提供方)
步骤0:创建JavaBean
步骤一:创建UserService接口
步骤二:创建UserService实现类,模拟数据–直接返回参数
步骤三:创建UserController,提供post请求处理
admin服务(调用方)
步骤一:编写Feign
1.1 修改pom,添加依赖
1.2 修改启动类,添加开启Feign注解
1.3 编写Feign接口
步骤二:编写Service
步骤三:编写Controller
添加Fegin依赖
<!--远程调用Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在启动类上添加注解@EnableFeignClients(开启feign)
编写Feign接口 (value=“服务名”,path=“控制器路径(controller)”)
@FeignClient(value="userservice",path="/user")
public interface UserFeign {
@PostMapping("/findByUsernameAndPassword")
public ResponseEntity<User> findByUsernameAndPassword(@RequestBody User user);
}
在service层注入feign 进行调用
至此,我们简单的SpringCloud架构远程调用就已经完成了
接下来简单说一下参数的其它传递方式
get请求:路径参数
服务提供方
提供方,方法参数必须添加@PathVariable注解
@GetMapping("/{uid}")
public ResponseEntity<String> findById(@PathVariable("uid") Integer uid) throws Exception {
//模拟延迟
return ResponseEntity.ok("测试数据" + uid);
}
服务调用方
@GetMapping("/{uid}")
public ResponseEntity<String> findById(@PathVariable("uid") Integer uid);
get请求:请求参数
服务提供方
*方法参数必须添加注解 *@RequestParam
@GetMapping("/query")
public ResponseEntity<String> query(@RequestParam("name") String name) throws Exception {
//模拟延迟
return ResponseEntity.ok("测试数据" + name);
}
服务调用方
post请求:对象
服务提供方
@PostMapping
public ResponseEntity<String> save(@RequestBody User user) throws Exception {
//模拟延迟
return ResponseEntity.ok("测试数据" + user);
}
服务调用方