版本
spring-boot:2.1.1.RELEASE
spring-cloud:Finchley.SR2
项目目录
├─swarm-cloud-----------------------------父项目,公共依赖
│ │
│ ├─discovery-----------------------------微服务注册中心
│ │
│ ├─provider-api
│ │ │
│ │ └─provider-user-api------------------用户服务API
│ │
│ ├─provider
│ │ │
│ │ ├─provider-user----------------------用户服务
│ │ │
│ │ └─provider-order---------------------订单服务
代码结构
swarm-cloud:父项目,只有一个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:微服务注册中心
- 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:定义用户服务的接口,只是一个普通的jar,所以不需要SpringBoot启动
- 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:定义一个接口,@FeignClient用于让其他微服务调用
@FeignClient("provider-user")
public interface UserService {
/**
* 根据ID获取用户信息
*/
@ResponseBody
@GetMapping("users/{id}")
UserDTO getUser(@PathVariable(value = "id") Long id);
}
- UserDTO.java:封装用户信息用户转换成JSON传输
@Data
public class UserDTO {
private Long id;
private String name;
private String mobile;
private LocalDateTime createTime;
}
provider:服务提供者父模块
- 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:用户服务提供者,引用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:实现UserService接口,本质是一个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:订单服务提供者,需要引用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/
本地运行测试
依次启动discovery,provider-user,provider-order
访问 http://localhost:8761/,可以看到两个服务都已经注册成功
访问 http://localhost:8763/orders/users/1,可以看到成功获取了用户信息