SpringCloud项目构建

本文介绍了如何构建一个SpringCloud项目,包括设置Eureka作为服务注册和发现中心,创建服务提供者、消费者,使用Feign进行服务调用,以及配置Zuul作为API网关。最后提到将通过K8s部署该项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud项目构建
关于概念类的,可以直接参考官方文档。本文从应用介绍:

  1. 首先创建eureka服务,服务的注册中心
  2. 创建服务的提供者
  3. 创建服务的消费者
  4. 创建feign
  5. 创建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发布这个项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值