版本
spring-boot:2.1.1.RELEASE
spring-cloud:Finchley.SR2
項(xiàng)目目錄
├─swarm-cloud-----------------------------父項(xiàng)目,公共依賴
│ │
│ ├─discovery-----------------------------微服務(wù)注冊(cè)中心
│ │
│ ├─provider-api
│ │ │
│ │ └─provider-user-api------------------用戶服務(wù)API
│ │
│ ├─provider
│ │ │
│ │ ├─provider-user----------------------用戶服務(wù)
│ │ │
│ │ └─provider-order---------------------訂單服務(wù)
代碼結(jié)構(gòu)
swarm-cloud:父項(xiàng)目粉渠,只有一個(gè)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.mario</groupId>
<artifactId>swarm-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>discovery</module>
<module>provider-api</module>
<module>provider</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lombok.version>1.18.4</lombok.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</project>
discovery:微服務(wù)注冊(cè)中心
- pom.xml
<?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>
<parent>
<groupId>com.mario</groupId>
<artifactId>swarm-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>discovery</artifactId>
<name>discovery</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yml
spring:
application:
name: discovery
server:
port: 8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- DiscoveryApplication.java
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
provider-user-api:定義用戶服務(wù)的接口分冈,只是一個(gè)普通的jar,所以不需要SpringBoot啟動(dòng)
- pom.xml
<?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">
<parent>
<artifactId>provider-api</artifactId>
<groupId>com.mario</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>provider-user-api</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
- UserService.java:定義一個(gè)接口渣叛,@FeignClient用于讓其他微服務(wù)調(diào)用
@FeignClient("provider-user")
public interface UserService {
/**
* 根據(jù)ID獲取用戶信息
*/
@ResponseBody
@GetMapping("users/{id}")
UserDTO getUser(@PathVariable(value = "id") Long id);
}
- UserDTO.java:封裝用戶信息用戶轉(zhuǎn)換成JSON傳輸
@Data
public class UserDTO {
private Long id;
private String name;
private String mobile;
private LocalDateTime createTime;
}
provider:服務(wù)提供者父模塊
- pom.xml
<parent>
<artifactId>swarm-cloud</artifactId>
<groupId>com.mario</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<packaging>pom</packaging>
<modules>
<module>provider-user</module>
<module>provider-order</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
provider-user:用戶服務(wù)提供者丈秩,引用provider-user-api模塊
- pom.xml
<parent>
<artifactId>provider</artifactId>
<groupId>com.mario</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider-user</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.mario</groupId>
<artifactId>provider-user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
- UserServiceImpl.java:實(shí)現(xiàn)UserService接口,本質(zhì)是一個(gè)restful接口
@RestController
public class UserServiceImpl implements UserService {
@Override
public UserDTO getUser(Long id) {
UserDTO dto = new UserDTO();
dto.setId(id);
dto.setName("mario");
dto.setMobile("18768101234");
dto.setCreateTime(LocalDateTime.now());
return dto;
}
}
UserApplication.java
@ComponentScan("com.mario.provider")
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
- application.yml
spring:
application:
name: provider-user
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
provider-order:訂單服務(wù)提供者淳衙,需要引用provider-user-api模塊
- pom.xml
<parent>
<artifactId>provider</artifactId>
<groupId>com.mario</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider-order</artifactId>
<dependencies>
<dependency>
<groupId>com.mario</groupId>
<artifactId>provider-user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- OrderController.java
@RestController
public class OrderController {
@Autowired
private UserService userService;
@GetMapping("/orders/users/{userId}")
public UserDTO getUser(@PathVariable Long userId) {
return userService.getUser(userId);
}
}
- OrderApplication.java
@ComponentScan("com.mario.provider")
@SpringBootApplication
@EnableFeignClients(basePackages = "com.mario.provider")
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- application.yml
spring:
application:
name: provider-order
server:
port: 8763
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
本地運(yùn)行測(cè)試
依次啟動(dòng)discovery,provider-user,provider-order
訪問(wèn) http://localhost:8761/箫攀,可以看到兩個(gè)服務(wù)都已經(jīng)注冊(cè)成功
訪問(wèn) http://localhost:8763/orders/users/1肠牲,可以看到成功獲取了用戶信息
下一篇 Docker Swarm + SpringCloud(三)構(gòu)建鏡像并推送到遠(yuǎn)程倉(cāng)庫(kù)