SpringCloud项目构建
关于概念类的,可以直接参考官方文档。本文从应用介绍:
- 首先创建eureka服务,服务的注册中心
- 创建服务的提供者
- 创建服务的消费者
- 创建feign
- 创建zuul网关
现在开始介绍:
一. 创建eureka作为服务的注册和发现中心
a. pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
b. application.yaml
server:
port: 10086
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
healthcheck:
enabled: true
service-url:
defaultZone: http://127.0.0.1:10086/eureka
error:
whitelabel:
enabled: true
c. 启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
d. 启动服务看下效果
http://localhost:10086/
二.创建服务提供者
a. pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
b. application.yaml
spring:
application:
name: cloud-system
datasource:
url:jdbc:mysql://localhost:3306/kgc?characterEncoding=utf8&serverTimezone=UTC
username:root
password:root
driver-class-name=com.mysql.cj.jdbc.Driver
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
server:
port: 10087
c. 启动类
@SpringBootApplication
@EnableEurekaClient
public class CloudSystemApplication {
public static void main(String[] args) {
SpringApplication.run(CloudSystemApplication.class, args);
}
}
d. 接口编写
@Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/login")
public String Login(String name, String password) {
return "login success";
}
@GetMapping("/getUserById")
public UserPo GetUserById(Long userId) {
return userService.GetUserById(userId);
}
}
c. 启动服务
测试接口地址:
http://localhost:10087/user/getUserById?userId=1
{“UserName”:“hello”,“UserId”:1,“userName”:“hello”,“userId”:1}
二. 服务的消费者
a.pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.cyh</groupId>
<artifactId>cloud-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
b. application.yaml
spring:
application:
name: cloud-filesystem
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
server:
port: 10020
feign:
httpclient:
enabled: true
c. 启动类
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = "com.cyh.cloudfilesystem.feign")
public class CloudFilesystemApplication {
public static void main(String[] args) {
SpringApplication.run(CloudFilesystemApplication.class, args);
}
}
d. 接口
@Controller
@RestController
@RequestMapping("/file")
public class FileSystenController {
@Autowired
private IFileSystemService fileSystemService;
@PostMapping(value = "/uploadFile", consumes = "multipart/form-data")
public ResponseResult uploadFileList(@RequestParam("fileList") List<MultipartFile> fileList)
throws IOException, BusinessException {
ResponseResult result = new ResponseResult();
List<FileInfo> fileInfoList = fileSystemService.uploadFileList(fileList);
result.setData(fileInfoList);
return result;
}
@GetMapping(value = "/testFeign")
public UserPo testFeign() {
return fileSystemService.testFeign();
}
}
d. feign
@Component
@FeignClient(value = "cloud-system",fallback = SystemFeignCallback.class)
public interface SystemFeign {
@GetMapping("/user/getUserById")
public UserPo GetUserById(@RequestParam long userId);
}
e. 启动服务
测试接口,通过feign的方式
http://localhost:10020/file/testFeign
{“UserName”:“hello”,“UserId”:1,“userName”:“hello”,“userId”:1}
三. 启用网关
a.pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.cyh</groupId>
<artifactId>cloud-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
b. application.yaml
server:
port: 10089
zuul:
routes:
cloud-system:
path: cloud-system/**
serviceId: cloud-system
cloud-filesystem:
path: cloud-filesystem/**
serviceId: cloud-filesystem
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000
feign:
hystrix:
enable: true
c. 启动类
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
@Bean
public AccessFilter accessFilter() {
return new AccessFilter();
}
}
d. 启动服务
测试接口
http://host.docker.internal:10089/cloud-filesystem/file/testFeign?accessToken=5
{“UserName”:“hello”,“UserId”:1,“userName”:“hello”,“userId”:1}
总结:
以上就完成了springcloud项目的构建,包含服务的注册和发现中线,服务的提供和消费者,以及网关等等。下篇会介绍如何通过k8s发布这个项目。