Skip to content

Commit

Permalink
add admin modules
Browse files Browse the repository at this point in the history
  • Loading branch information
macrozheng committed Oct 27, 2019
1 parent b60aaf8 commit f541b28
Show file tree
Hide file tree
Showing 23 changed files with 761 additions and 0 deletions.
31 changes: 31 additions & 0 deletions admin-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
65 changes: 65 additions & 0 deletions admin-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.macro.cloud</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-client</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<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>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.macro.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {

public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.macro.cloud.controller;

import com.macro.cloud.domain.CommonResult;
import com.macro.cloud.domain.User;
import com.macro.cloud.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by macro on 2019/8/29.
*/
@RestController
@RequestMapping("/user")
public class UserController {

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@Autowired
private UserService userService;

@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
userService.create(user);
return new CommonResult("操作成功", 200);
}

@GetMapping("/{id}")
public CommonResult<User> getUser(@PathVariable Long id) {
User user = userService.getUser(id);
LOGGER.info("根据id获取用户信息,用户名称为:{}",user.getUsername());
return new CommonResult<>(user);
}

@GetMapping("/getUserByIds")
public CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids) {
List<User> userList= userService.getUserByIds(ids);
LOGGER.info("根据ids获取用户信息,用户列表为:{}",userList);
return new CommonResult<>(userList);
}

@GetMapping("/getByUsername")
public CommonResult<User> getByUsername(@RequestParam String username) {
User user = userService.getByUsername(username);
return new CommonResult<>(user);
}

@PostMapping("/update")
public CommonResult update(@RequestBody User user) {
userService.update(user);
return new CommonResult("操作成功", 200);
}

@PostMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
userService.delete(id);
return new CommonResult("操作成功", 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.macro.cloud.domain;

/**
* Created by macro on 2019/8/29.
*/
public class CommonResult<T> {
private T data;
private String message;
private Integer code;

public CommonResult() {
}

public CommonResult(T data, String message, Integer code) {
this.data = data;
this.message = message;
this.code = code;
}

public CommonResult(String message, Integer code) {
this(null, message, code);
}

public CommonResult(T data) {
this(data, "操作成功", 200);
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}
}
44 changes: 44 additions & 0 deletions admin-client/src/main/java/com/macro/cloud/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.macro.cloud.domain;

/**
* Created by macro on 2019/8/29.
*/
public class User {

private Long id;
private String username;
private String password;

public User() {
}

public User(Long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.macro.cloud.service;

import com.macro.cloud.domain.User;

import java.util.List;

/**
* Created by macro on 2019/8/29.
*/
public interface UserService {
void create(User user);

User getUser(Long id);

void update(User user);

void delete(Long id);

User getByUsername(String username);

List<User> getUserByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.macro.cloud.service.impl;

import com.macro.cloud.domain.User;
import com.macro.cloud.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by macro on 2019/8/29.
*/
@Service
public class UserServiceImpl implements UserService {
private List<User> userList;

@Override
public void create(User user) {
userList.add(user);
}

@Override
public User getUser(Long id) {
List<User> findUserList = userList.stream().filter(userItem -> userItem.getId().equals(id)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(findUserList)) {
return findUserList.get(0);
}
return null;
}

@Override
public void update(User user) {
userList.stream().filter(userItem -> userItem.getId().equals(user.getId())).forEach(userItem -> {
userItem.setUsername(user.getUsername());
userItem.setPassword(user.getPassword());
});
}

@Override
public void delete(Long id) {
User user = getUser(id);
if (user != null) {
userList.remove(user);
}
}

@Override
public User getByUsername(String username) {
List<User> findUserList = userList.stream().filter(userItem -> userItem.getUsername().equals(username)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(findUserList)) {
return findUserList.get(0);
}
return null;
}

@Override
public List<User> getUserByIds(List<Long> ids) {
return userList.stream().filter(userItem -> ids.contains(userItem.getId())).collect(Collectors.toList());
}

@PostConstruct
public void initData() {
userList = new ArrayList<>();
userList.add(new User(1L, "macro", "123456"));
userList.add(new User(2L, "andy", "123456"));
userList.add(new User(3L, "mark", "123456"));
}
}
21 changes: 21 additions & 0 deletions admin-client/src/main/resources/application-eureka.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
spring:
application:
name: admin-client
server:
port: 9305
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
file: admin-client.log #添加开启admin的日志监控
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
Loading

0 comments on commit f541b28

Please sign in to comment.