版本
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文件
4.0.0
com.mario
swarm-cloud
0.0.1-SNAPSHOT
pom
discovery
provider-api
provider
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
UTF-8
UTF-8
1.8
1.18.4
Finchley.SR2
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.projectlombok
lombok
${lombok.version}
discovery:微服务注册中心
- pom.xml
4.0.0
com.mario
swarm-cloud
0.0.1-SNAPSHOT
discovery
discovery
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-maven-plugin
- 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
provider-api
com.mario
0.0.1-SNAPSHOT
4.0.0
jar
provider-user-api
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
- 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
swarm-cloud
com.mario
0.0.1-SNAPSHOT
4.0.0
provider
pom
provider-user
provider-order
org.springframework.boot
spring-boot-maven-plugin
provider-user:用户服务提供者,引用provider-user-api模块
- pom.xml
provider
com.mario
0.0.1-SNAPSHOT
4.0.0
provider-user
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.mario
provider-user-api
0.0.1-SNAPSHOT
- 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
provider
com.mario
0.0.1-SNAPSHOT
4.0.0
provider-order
com.mario
provider-user-api
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
- 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,可以看到成功获取了用户信息
下一篇 Docker Swarm + SpringCloud(三)构建镜像并推送到远程仓库