diff --git a/README.md b/README.md index c0c91f5ef93298fb39a363444fc8c70c70a2f035..6bcc1ef3b5d21ba60df3b76db866606fcccc53fa 100644 --- a/README.md +++ b/README.md @@ -29,5 +29,7 @@ SpringBoot应用集合 - [SpringBoot整合ActiveMq](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-ActiveMq) - [SpringBoot整合Knife4j](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/springboot-knife4j) - [springboot-pagehelper实现分页查询](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/Springboot-pagehelper) +- [springboot-async实现异步](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/springboot-async) +- [springboot-nacos注册中心](https://gitee.com/superbutton/SpringBoot-Components/tree/develop/springboot-nacos) diff --git a/springboot-async/.gitignore b/springboot-async/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b56f1dd0d04da4e03f710af3917e4fbdd9be4aa8 --- /dev/null +++ b/springboot-async/.gitignore @@ -0,0 +1,33 @@ +README.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### 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/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/springboot-async/pom.xml b/springboot-async/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..017f829a5d12a75f1fe0d19f76f9933c5b996c0a --- /dev/null +++ b/springboot-async/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.2 + + + com.button + springboot-async + 0.0.1-SNAPSHOT + springboot-async + springboot-async异步 + + 1.8 + + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/springboot-async/src/main/java/com/button/SpringbootAsyncApplication.java b/springboot-async/src/main/java/com/button/SpringbootAsyncApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..48f946371fda7aaf56d4fdee7b983412a0a8f2a3 --- /dev/null +++ b/springboot-async/src/main/java/com/button/SpringbootAsyncApplication.java @@ -0,0 +1,15 @@ +package com.button; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableAsync; + +@EnableAsync // 开启异步的注解 +@SpringBootApplication +public class SpringbootAsyncApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootAsyncApplication.class, args); + } + +} diff --git a/springboot-async/src/main/java/com/button/controller/TestController.java b/springboot-async/src/main/java/com/button/controller/TestController.java new file mode 100644 index 0000000000000000000000000000000000000000..4bd8f7063266cfbd7e99e4049285fae8ec722522 --- /dev/null +++ b/springboot-async/src/main/java/com/button/controller/TestController.java @@ -0,0 +1,52 @@ +package com.button.controller; + +import com.button.job.AsyncJob; +import com.button.util.SpringContextUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +@RequestMapping("/api") +public class TestController { + + @Autowired + AsyncJob asyncJob; + + /** + * 异步任务与调用不在同一个类中 + * @return + */ + @GetMapping("/success") + public String getSuccessInfo() { + asyncJob.successAsyncJob(); + return "success"; + } + + /** + * 异步任务与调用在同一个类中 + * @return + */ + @GetMapping("/same-class") + public String getSameClass() { + TestController bean = SpringContextUtil.getBean(TestController.class); + bean.test(); + return "same-class"; + } + + @Async + public void test(){ + try { + log.info("=======开始执行异步处理类========"); + Thread.sleep(5000); + log.info("=======执行异步处理类完毕========"); + }catch (Exception e){ + log.error("异常. e={}", e); + } + } + +} diff --git a/springboot-async/src/main/java/com/button/job/AsyncJob.java b/springboot-async/src/main/java/com/button/job/AsyncJob.java new file mode 100644 index 0000000000000000000000000000000000000000..8e588a17fdad5e3632e742cf43479c93e07a3e78 --- /dev/null +++ b/springboot-async/src/main/java/com/button/job/AsyncJob.java @@ -0,0 +1,23 @@ +package com.button.job; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class AsyncJob { + + @Async + public void successAsyncJob() { + try { + log.info("/success的异步方法开始执行了......"); + for(int i = 0; i < 1000000; i++) { + log.info("打印{}", i); + } + log.info("/success的异步方法执行完了......"); + }catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/springboot-async/src/main/java/com/button/util/SpringContextUtil.java b/springboot-async/src/main/java/com/button/util/SpringContextUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..2c8b97b9cdbedf529d95905426b12e6af09bf554 --- /dev/null +++ b/springboot-async/src/main/java/com/button/util/SpringContextUtil.java @@ -0,0 +1,30 @@ +package com.button.util; + + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component("springContextUtil") +public class SpringContextUtil implements ApplicationContextAware { + private static ApplicationContext applicationContext = null; + + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + @SuppressWarnings("unchecked") + public static T getBean(String beanId) { + return (T) applicationContext.getBean(beanId); + } + + public static T getBean(Class requiredType) { + return (T) applicationContext.getBean(requiredType); + } + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + SpringContextUtil.applicationContext = applicationContext; + } + +} diff --git a/springboot-async/src/main/resources/application.yml b/springboot-async/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..0a377cfbf0c6b171dfd8f11dd5dc4ecbc2ca967c --- /dev/null +++ b/springboot-async/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8819 diff --git a/springboot-nacos/.gitignore b/springboot-nacos/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..549e00a2a96fa9d7c5dbc9859664a78d980158c2 --- /dev/null +++ b/springboot-nacos/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### 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/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/springboot-nacos/README.md b/springboot-nacos/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8b17befb2e44ef67cf0188a670dc46606c7b7c2c --- /dev/null +++ b/springboot-nacos/README.md @@ -0,0 +1 @@ +Springboot整合nacos \ No newline at end of file diff --git a/springboot-nacos/api-gateway/README.md b/springboot-nacos/api-gateway/README.md new file mode 100644 index 0000000000000000000000000000000000000000..46b4a83bfae7eac2f67e9f778624f53dec0d5a57 --- /dev/null +++ b/springboot-nacos/api-gateway/README.md @@ -0,0 +1,3 @@ +http://localhost:8081/system/api/test/get-list +http://localhost:8081/system/api/test/get-token +http://localhost:8081/system/api/test/get-user \ No newline at end of file diff --git a/springboot-nacos/api-gateway/pom.xml b/springboot-nacos/api-gateway/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..beab5e603ed54fe0f0df6da95f05f2f652601a1e --- /dev/null +++ b/springboot-nacos/api-gateway/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.button + springboot-nacos + 0.0.1-SNAPSHOT + + springboot-nacos + api-gateway + 0.0.1-SNAPSHOT + api-gateway + api-gateway + + 1.8 + + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + com.alibaba + fastjson + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springboot-nacos/api-gateway/src/main/java/com/button/ApiGatewayApplication.java b/springboot-nacos/api-gateway/src/main/java/com/button/ApiGatewayApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..2dde4130abead3f297da8db619edbcada2cb275b --- /dev/null +++ b/springboot-nacos/api-gateway/src/main/java/com/button/ApiGatewayApplication.java @@ -0,0 +1,54 @@ +package com.button; + +import com.button.filter.AccessFilter; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.context.annotation.Bean; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; + +@EnableZuulProxy +@EnableDiscoveryClient +@SpringBootApplication +public class ApiGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiGatewayApplication.class, args); + } + + @Bean + public AccessFilter accessFilter() { + return new AccessFilter(); + } + + @Bean + public CorsFilter corsFilter() { + final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + final CorsConfiguration config = new CorsConfiguration(); + // 允许cookies跨域 + config.setAllowCredentials(true); + // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin + config.addAllowedOrigin("*"); + // #允许访问的头信息,*表示全部 + config.addAllowedHeader("*"); + // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 + config.setMaxAge(18000L); + // 允许提交请求的方法,*表示全部允许 + config.addAllowedMethod("OPTIONS"); + config.addAllowedMethod("HEAD"); + // 允许Get的请求方法 + config.addAllowedMethod("GET"); + config.addAllowedMethod("PUT"); + config.addAllowedMethod("POST"); + config.addAllowedMethod("DELETE"); + config.addAllowedMethod("PATCH"); + //预检请求的有效期,单位为秒 + config.setMaxAge(3600L); + source.registerCorsConfiguration("/**", config); + return new CorsFilter(source); + } + +} diff --git a/springboot-nacos/api-gateway/src/main/java/com/button/fallback/SystemFallback.java b/springboot-nacos/api-gateway/src/main/java/com/button/fallback/SystemFallback.java new file mode 100644 index 0000000000000000000000000000000000000000..22cf247ea896da279cbb3f06f86ec8ccaaf0c709 --- /dev/null +++ b/springboot-nacos/api-gateway/src/main/java/com/button/fallback/SystemFallback.java @@ -0,0 +1,79 @@ +package com.button.fallback; + +import com.alibaba.fastjson.JSONException; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.stereotype.Component; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +@Component +@Slf4j +public class SystemFallback implements FallbackProvider { + + @Override + public String getRoute() { + return "system-service"; + } + + @Override + public ClientHttpResponse fallbackResponse() { + return new ClientHttpResponse(){ + + @Override + public void close() { + log.info("close"); + } + + @Override + public HttpStatus getStatusCode() throws IOException { + return HttpStatus.OK; + } + + @Override + public int getRawStatusCode() throws IOException { + + return HttpStatus.OK.value(); + } + + @Override + public String getStatusText() throws IOException { + return HttpStatus.OK.getReasonPhrase(); + } + + @Override + public InputStream getBody() throws IOException { + JSONObject r = new JSONObject(); + try { + r.put("code", "999"); + r.put("msg", "system不可用,请联系管理员处理!"); + } catch (JSONException e) { + log.error("获取body异常. e={}", e); + } + return new ByteArrayInputStream(r.toString().getBytes("UTF-8")); + } + + @Override + public HttpHeaders getHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + return headers; + } + }; + } + + @Override + public ClientHttpResponse fallbackResponse(Throwable cause) { + log.error("system-service error", cause); + return fallbackResponse(); + } +} diff --git a/springboot-nacos/api-gateway/src/main/java/com/button/filter/AccessFilter.java b/springboot-nacos/api-gateway/src/main/java/com/button/filter/AccessFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..1ca0d78abeaa5976385782e3067e4a06ec3034b7 --- /dev/null +++ b/springboot-nacos/api-gateway/src/main/java/com/button/filter/AccessFilter.java @@ -0,0 +1,76 @@ +package com.button.filter; + +import com.alibaba.fastjson.JSON; +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +public class AccessFilter extends ZuulFilter { + + @Value("${zuul.ignored-path}") + private String ignoredPath; + + @Override + public String filterType() { + return FilterConstants.PRE_TYPE; + } + + @Override + public int filterOrder() { + return FilterConstants.SEND_ERROR_FILTER_ORDER; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() { + RequestContext ctx = RequestContext.getCurrentContext(); + HttpServletRequest request = ctx.getRequest(); + String token = request.getParameter("token"); + // 判断路径是否不需要验证 + if (isIgnore(request.getRequestURI())) { + return null; + } + // token为空或者无效token + if (token == null) { + handleLoginException(); + } + return null; + } + + private void handleLoginException() { + RequestContext requestContext = RequestContext.getCurrentContext(); + requestContext.setSendZuulResponse(false); + requestContext.setResponseStatusCode(400); + Map ex = new HashMap<>(); + ex.put("code", "000"); + ex.put("msg", "未登录"); + String response = JSON.toJSONString(ex); + requestContext.setResponseBody(response); + requestContext.getResponse().setCharacterEncoding("GBK"); + } + /** + * 是否忽略url + */ + private boolean isIgnore(String url) { + boolean isIgnore = false; + String[] ignoreUrls = ignoredPath.split(","); + for (String u : ignoreUrls) { + if (url.startsWith(u)) { + isIgnore = true; + break; + } + } + return isIgnore; + } +} diff --git a/springboot-nacos/api-gateway/src/main/resources/bootstrap.yml b/springboot-nacos/api-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000000000000000000000000000000000..34144dcee8c6326ce068be4f66c90ec79a6bf5bd --- /dev/null +++ b/springboot-nacos/api-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,18 @@ +spring: + cloud: + nacos: + config: + file-extension: yaml +# shared-dataids: application-gateway.yml + server-addr: dev.apibutton.top:8848 + ext-config: + - data-id: gateway-application.yml + group: DEFAULT_GROUP + refresh: true + - data-id: gateway-application-dev.yml + group: DEFAULT_GROUP + refresh: true + namespace: 39b8efb5-95dd-4d75-b320-0f95f9b9074b + discovery: + server-addr: dev.apibutton.top:8848 + namespace: 39b8efb5-95dd-4d75-b320-0f95f9b9074b \ No newline at end of file diff --git a/springboot-nacos/api-gateway/src/main/resources/logback-spring.xml b/springboot-nacos/api-gateway/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000000000000000000000000000000000..659e3981ec19f88a6128c29accef149207edac6f --- /dev/null +++ b/springboot-nacos/api-gateway/src/main/resources/logback-spring.xml @@ -0,0 +1,72 @@ + + + + + + ERROR + DENY + ACCEPT + + + + + [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n + + + + + ${LOG_HOME}/gateway.info.%d{yyyy-MM-dd}.%i.log.zip + + 5 + true + 1GB + + + 30 MB + + + + + + + + ERROR + + + + + [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n + + + + + ${LOG_HOME}/gateway.error.%d{yyyy-MM-dd}.%i.log.zip + + 5 + true + 1GB + + + 30 MB + + + + + + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/springboot-nacos/api-system/README.md b/springboot-nacos/api-system/README.md new file mode 100644 index 0000000000000000000000000000000000000000..038b9a74c0e23d68e28c55ed89f50ce239b100d6 --- /dev/null +++ b/springboot-nacos/api-system/README.md @@ -0,0 +1 @@ +http://localhost:8082/doc.html#/home \ No newline at end of file diff --git a/springboot-nacos/api-system/pom.xml b/springboot-nacos/api-system/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..408d2f9ff87ab661388f84d1da137b88dbc0e381 --- /dev/null +++ b/springboot-nacos/api-system/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.button + springboot-nacos + 0.0.1-SNAPSHOT + + springboot-nacos + api-system + 0.0.1-SNAPSHOT + api-system + api-system + + 1.8 + + + + common + com.button + 0.0.1-SNAPSHOT + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springboot-nacos/api-system/src/main/java/com/button/ApiSystemApplication.java b/springboot-nacos/api-system/src/main/java/com/button/ApiSystemApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..f6811452a1db953200b3da1b500ab9a280ff0a6f --- /dev/null +++ b/springboot-nacos/api-system/src/main/java/com/button/ApiSystemApplication.java @@ -0,0 +1,15 @@ +package com.button; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@EnableDiscoveryClient +@SpringBootApplication +public class ApiSystemApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiSystemApplication.class, args); + } + +} diff --git a/springboot-nacos/api-system/src/main/java/com/button/config/SwaggerConfig.java b/springboot-nacos/api-system/src/main/java/com/button/config/SwaggerConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..75512950813c93c5d7bbf09e2aa2555309237d8e --- /dev/null +++ b/springboot-nacos/api-system/src/main/java/com/button/config/SwaggerConfig.java @@ -0,0 +1,65 @@ +package com.button.config; + +import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; +import io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.ParameterBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.schema.ModelRef; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Parameter; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@EnableSwagger2 +@EnableKnife4j +@ConditionalOnProperty(name="swagger.enabled", havingValue="true") +public class SwaggerConfig { + @Value("${swagger.enabled}") + private boolean enableSwagger; + @Value("${swagger.title}") + private String swaggerTitle; + @Value("${swagger.description}") + private String swaggerDesc; + @Value("${swagger.version}") + private String swaggerVersion; + + @Bean + public Docket createRestApi() { + ParameterBuilder tokenPar = new ParameterBuilder(); + List pars = new ArrayList<>(); + tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("query").required(true).build(); + pars.add(tokenPar.build()); + + return new Docket(DocumentationType.SWAGGER_2) + .enable(enableSwagger) + .apiInfo(apiInfo()) + .groupName(swaggerTitle) + .useDefaultResponseMessages(false) + .select() + .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) + .paths(PathSelectors.any()) + .build() + .globalOperationParameters(pars) + ; + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title(swaggerTitle) + .description(swaggerDesc) + .version(swaggerVersion) + .build(); + + } +} diff --git a/springboot-nacos/api-system/src/main/java/com/button/controller/TestController.java b/springboot-nacos/api-system/src/main/java/com/button/controller/TestController.java new file mode 100644 index 0000000000000000000000000000000000000000..0d0f425a021fd3eda26c17b780bbdfdeb74153fe --- /dev/null +++ b/springboot-nacos/api-system/src/main/java/com/button/controller/TestController.java @@ -0,0 +1,44 @@ +package com.button.controller; + +import com.button.domain.vo.User; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@Slf4j +@Validated +@Api(tags = "测试API") +@RequestMapping(value = "/api/test", produces = "application/json") +public class TestController { + + @GetMapping("/get-list") + @ApiOperation(value = "测试get", notes = "作者:zh") + public String getList(HttpServletRequest request) { + log.info("测试"); + return "SUCCESS"; + } + + + @GetMapping("/get-token") + @ApiOperation(value = "测试get(需要token)", notes = "作者:zh") + public String getListToken(HttpServletRequest request) { + log.info("测试"); + return "SUCCESS-TOKEN"; + } + + @GetMapping("/get-user") + @ApiOperation(value = "测试", notes = "作者:zh") + public User getUser(HttpServletRequest request) { + User user = new User(); + user.setName("张三"); + user.setPassword("123456789"); + return user; + } +} diff --git a/springboot-nacos/api-system/src/main/resources/bootstrap.yml b/springboot-nacos/api-system/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000000000000000000000000000000000..60928a76a33b49c7e12e6631c66182fa8f3b5c41 --- /dev/null +++ b/springboot-nacos/api-system/src/main/resources/bootstrap.yml @@ -0,0 +1,18 @@ +spring: + cloud: + nacos: + config: + file-extension: yaml +# shared-dataids: application-gateway.yml + server-addr: dev.apibutton.top:8848 + ext-config: + - data-id: system-application.yml + group: DEFAULT_GROUP + refresh: true + - data-id: system-application-dev.yml + group: DEFAULT_GROUP + refresh: true + namespace: 39b8efb5-95dd-4d75-b320-0f95f9b9074b + discovery: + server-addr: dev.apibutton.top:8848 + namespace: 39b8efb5-95dd-4d75-b320-0f95f9b9074b \ No newline at end of file diff --git a/springboot-nacos/api-system/src/main/resources/logback-spring.xml b/springboot-nacos/api-system/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000000000000000000000000000000000..fc4af7c983f8a9e26c88c9ee1fa46a362d89e1d4 --- /dev/null +++ b/springboot-nacos/api-system/src/main/resources/logback-spring.xml @@ -0,0 +1,72 @@ + + + + + + ERROR + DENY + ACCEPT + + + + + [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n + + + + + ${LOG_HOME}/system.info.%d{yyyy-MM-dd}.%i.log.zip + + 5 + true + 1GB + + + 30 MB + + + + + + + + ERROR + + + + + [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n + + + + + ${LOG_HOME}/system.error.%d{yyyy-MM-dd}.%i.log.zip + + 5 + true + 1GB + + + 30 MB + + + + + + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/springboot-nacos/common/pom.xml b/springboot-nacos/common/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..3e05f4f7235d65c6c4d14a6f86c443127e272f4a --- /dev/null +++ b/springboot-nacos/common/pom.xml @@ -0,0 +1,69 @@ + + + + + springboot-nacos + com.button + 0.0.1-SNAPSHOT + + 4.0.0 + + common + com.button + 0.0.1-SNAPSHOT + jar + common + + + UTF-8 + 1.7 + 1.7 + + + + + + + + + + maven-clean-plugin + 3.1.0 + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/springboot-nacos/common/src/main/java/com/button/domain/vo/User.java b/springboot-nacos/common/src/main/java/com/button/domain/vo/User.java new file mode 100644 index 0000000000000000000000000000000000000000..39db7967561184fa74082b183db31dea59189592 --- /dev/null +++ b/springboot-nacos/common/src/main/java/com/button/domain/vo/User.java @@ -0,0 +1,9 @@ +package com.button.domain.vo; + +import lombok.Data; + +@Data +public class User { + private String name; + private String password; +} diff --git a/springboot-nacos/pom.xml b/springboot-nacos/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..ab3e4bd7d27879da3ea467a19bbb773240883867 --- /dev/null +++ b/springboot-nacos/pom.xml @@ -0,0 +1,159 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + com.button + springboot-nacos + 0.0.1-SNAPSHOT + pom + springboot-nacos + springboot整合nacos + + + api-gateway + common + api-system + + + + UTF-8 + UTF-8 + 1.8 + Edgware.SR1 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 1.5.1.RELEASE + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + 1.5.1.RELEASE + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter-aop + + + + + + + + + + + + + + + + + + org.slf4j + slf4j-api + + + io.springfox + springfox-swagger2 + 2.7.0 + + + io.springfox + springfox-swagger-ui + 2.7.0 + + + com.github.xiaoymin + knife4j-spring-boot-starter + 2.0.4 + + + + + + + + org.projectlombok + lombok + provided + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + org.springframework.cloud + spring-cloud-commons + 1.3.5.RELEASE + + + com.alibaba + fastjson + 1.2.70 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + +