# springCloud-learn **Repository Path**: worldcreator/springCloud-learn ## Basic Information - **Project Name**: springCloud-learn - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-03-15 - **Last Updated**: 2021-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ####IDEA 构建第一个spring cloud 项目 > spring cloud的架构体系自己去搜以下就行, 一般 eureka\config server\service\client 就差不多 ###首先创建一个不带模板的maven项目作为父工程, 配置springCloud.pom(关键部分) ```xml org.springframework.boot spring-boot-starter-parent 2.1.12.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-dependencies Greenwich.SR6 pom import org.springframework.boot spring-boot-maven-plugin 2.4.3 ``` ####注册中心 eureka > 新建 maven项目, 其中package以 com.hll(group) + 模块名开头, 如 com.hll.eureka (称为主包), test文件夹直接删掉,只要main > 在主包下创建 springboot启动类: ```java @SpringBootApplication @EnableEurekaServer /*开启 eureka server*/ public class EurekaApplication { /*类名自定*/ public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } } ``` > eureka pom: ```xml springCloud com.hll 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-server ``` > 编写 application.yml: ```yaml server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka/ #server.port`:当前 Eureka Server 服务端口。 # #`eureka.client.register-with-eureka`:是否将当前的 Eureka Server 服务作为客户端进行注册。 # #`eureka.client.fetch-fegistry`:是否获取其他 Eureka Server 服务的数据。 # #`eureka.client.service-url.defaultZone`:注册中心的访问地址。 ``` > 这时启动 springboot 启动类, 然后访问 localhost:8761 可以看到页面 >期间如果碰到 slf4j 报错,就在 父pom下引入: ```xml org.slf4j slf4j-api 1.7.16 ``` ###config Server 负责保存各微服务的配置,方便管理 > 新建 maven项目 config_server > 启动类上加 @EnableConfigServer > application.yml: ```yaml server: port: 8762 spring: application: name: configserver profiles: active: native cloud: config: server: native: search-locations: classpath:/shared #保存其他微服务配置的文件夹 ``` > 这是不注册在 eureka的写法,每个微服务都需要知道config server 的地址 ### user_service 用户服务提供者 >spring boot 创建基本增删改项目 > >引入下列依赖, mabatis-spring-boot和mysql connector也一样 ```xml org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client ``` >配置 bootstrap.yml, 只配置 config server, config server中保存了其具体yml配置 ###客户端 client, 调用微服务 >pom也要导入 config/eureka client, 不需要Mybatis.... >为了调用其他服务: ```xml org.springframework.cloud spring-cloud-starter-openfeign ``` >主要核心代码: ```java @FeignClient(value = "user") public interface UserFeign { @GetMapping("/user/all") List findAll(); } ``` ###启动 >依次启动 eureka, config server, user_service, client