一:架構簡介java
Spring Cloud是微服務思想的體現。每一個項目單獨部署,我只須要知道你服務的name就能直接調用你,而不關心你的ip和端口的變化。當接口服務不可用的時候,我能感知到你沒法用了,就再也不使用你。web
咱們作這麼一個場景:spring
producer數據庫
有一個生產者服務producer1,對外提供用戶的信息。(Spring Boot構建)apache
用戶信息保存在MySQL中,讀取MySQL數據的時候,用MyBatis框架實現dao層的代碼。(Mybatis整合)架構
將構建好的項目copy出來一份,改一下端口號,服務的名字不變。(作集羣模擬)框架
producer1 和 producer2都註冊到Eureka中maven
Eurekaspring-boot
建立一個Spring Boot項目,微服務
二:建立生產者項目producer1
(1)建立一個maven項目,pom文件添加依賴以下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cs.cloud</groupId> <artifactId>producer1</artifactId> <version>1.0-SNAPSHOT</version> <!-- 引入spring boot的依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- 引入spring web的依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引入spring 操做數據庫依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- H2數據庫 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)新建啓動類
(3)寫controller