diff --git a/Chapter4-2-6/pom.xml b/Chapter4-2-6/pom.xml new file mode 100644 index 0000000..b204d16 --- /dev/null +++ b/Chapter4-2-6/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + com.didispace + Chapter4-2-6 + 1.0.0 + jar + + Chapter4-2-6 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + + org.springframework.boot + spring-boot-starter-log4j + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mongodb + mongodb-driver + 3.2.2 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-2-6/src/main/java/com/didispace/Application.java b/Chapter4-2-6/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..2a14b75 --- /dev/null +++ b/Chapter4-2-6/src/main/java/com/didispace/Application.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter4-2-6/src/main/java/com/didispace/aspect/RequestLog.java b/Chapter4-2-6/src/main/java/com/didispace/aspect/RequestLog.java new file mode 100644 index 0000000..ece9267 --- /dev/null +++ b/Chapter4-2-6/src/main/java/com/didispace/aspect/RequestLog.java @@ -0,0 +1,90 @@ +package com.didispace.aspect; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.aspectj.lang.JoinPoint; + +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/23 下午4:08. + * @blog http://blog.didispace.com + */ +public class RequestLog { + + public String requestURL; + public String requestURI; + public String queryString; + public String remoteAddr; + public String remoteHost; + public Integer remotePort; + public String localAddr; + public String localName; + public String method; + + public Map parameters; + public Map headers; + + public String classMethod; + public String args; + + public RequestLog(){ + + } + + public RequestLog(HttpServletRequest request, JoinPoint joinPoint) { + // 基本信息 + this.requestURL = request.getRequestURL().toString(); + this.requestURI = request.getRequestURI(); + this.queryString = request.getQueryString(); + this.remoteAddr = request.getRemoteAddr(); + this.remoteHost = request.getRemoteHost(); + this.remotePort = request.getRemotePort(); + this.localAddr = request.getLocalAddr(); + this.localName = request.getLocalName(); + this.method = request.getMethod(); + // 头信息 + this.headers = getHeadersInfo(request); + // 参数 + this.parameters = request.getParameterMap(); + // 切入点信息 + this.classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(); + this.args = Arrays.toString(joinPoint.getArgs()); + } + + /** + * 获取头部信息 + * + * @param request + * @return + */ + private Map getHeadersInfo(HttpServletRequest request) { + Map map = new HashMap<>(); + Enumeration headerNames = request.getHeaderNames(); + while (headerNames.hasMoreElements()) { + String key = (String) headerNames.nextElement(); + String value = request.getHeader(key); + map.put(key, value); + } + return map; + } + + @Override + public String toString() { + ObjectMapper mapper = new ObjectMapper(); + String r; + try { + r = mapper.writeValueAsString(this); + } catch (JsonProcessingException e) { + r = getClass() + "转换JSON失败"; + } + return r; + } + +} diff --git a/Chapter4-2-6/src/main/java/com/didispace/aspect/WebLogAspect.java b/Chapter4-2-6/src/main/java/com/didispace/aspect/WebLogAspect.java new file mode 100644 index 0000000..d5ea75a --- /dev/null +++ b/Chapter4-2-6/src/main/java/com/didispace/aspect/WebLogAspect.java @@ -0,0 +1,86 @@ +package com.didispace.aspect; + +import com.mongodb.BasicDBObject; +import org.apache.log4j.Logger; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +/** + * Web层日志切面 + * + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/17 上午10:42. + * @blog http://blog.didispace.com + */ +@Aspect +@Order(1) +@Component +public class WebLogAspect { + + private Logger logger = Logger.getLogger("mongodb"); + + @Pointcut("execution(public * com.didispace.web..*.*(..))") + public void webLog(){} + + @Before("webLog()") + public void doBefore(JoinPoint joinPoint) throws Throwable { + // 获取HttpServletRequest + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + // 获取要记录的日志内容 + BasicDBObject logInfo = getBasicDBObject(request, joinPoint); + logger.info(logInfo); + } + + + private BasicDBObject getBasicDBObject(HttpServletRequest request, JoinPoint joinPoint) { + // 基本信息 + BasicDBObject r = new BasicDBObject(); + r.append("requestURL", request.getRequestURL().toString()); + r.append("requestURI", request.getRequestURI()); + r.append("queryString", request.getQueryString()); + r.append("remoteAddr", request.getRemoteAddr()); + r.append("remoteHost", request.getRemoteHost()); + r.append("remotePort", request.getRemotePort()); + r.append("localAddr", request.getLocalAddr()); + r.append("localName", request.getLocalName()); + r.append("method", request.getMethod()); + r.append("headers", getHeadersInfo(request)); + r.append("parameters", request.getParameterMap()); + r.append("classMethod", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); + r.append("args", Arrays.toString(joinPoint.getArgs())); + return r; + } + + /** + * 获取头信息 + * + * @param request + * @return + */ + private Map getHeadersInfo(HttpServletRequest request) { + Map map = new HashMap<>(); + Enumeration headerNames = request.getHeaderNames(); + while (headerNames.hasMoreElements()) { + String key = (String) headerNames.nextElement(); + String value = request.getHeader(key); + map.put(key, value); + } + return map; + } + +} + diff --git a/Chapter4-2-6/src/main/java/com/didispace/web/HelloController.java b/Chapter4-2-6/src/main/java/com/didispace/web/HelloController.java new file mode 100644 index 0000000..e53c5db --- /dev/null +++ b/Chapter4-2-6/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,20 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.*; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/19 下午1:27. + * @blog http://blog.didispace.com + */ +@RestController +public class HelloController { + + @RequestMapping(value = "/hello", method = RequestMethod.GET) + @ResponseBody + public String hello(@RequestParam String name) { + return "Hello " + name; + } + +} diff --git a/Chapter4-2-6/src/main/resources/application.properties b/Chapter4-2-6/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/Chapter4-2-6/src/main/resources/log4j.properties b/Chapter4-2-6/src/main/resources/log4j.properties new file mode 100755 index 0000000..36b92b5 --- /dev/null +++ b/Chapter4-2-6/src/main/resources/log4j.properties @@ -0,0 +1,14 @@ +# LOG4J配置 +log4j.rootCategory=INFO, stdout +log4j.logger.mongodb=INFO, mongodb + +# 控制台输出 +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n + +# mongodb输出 +log4j.appender.mongodb=com.didispace.log.MongoAppender +log4j.appender.mongodb.connectionUrl=mongodb://localhost:27017 +log4j.appender.mongodb.databaseName=logs +log4j.appender.mongodb.collectionName=logs_request diff --git a/Chapter4-2-6/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-2-6/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..168dfe7 --- /dev/null +++ b/Chapter4-2-6/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,25 @@ +package com.didispace; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Before + public void setUp() throws Exception { + + + } + + @Test + public void getHello() throws Exception { + + } + +} diff --git a/Chapter9-1-1/compute-service/pom.xml b/Chapter9-1-1/compute-service/pom.xml new file mode 100644 index 0000000..d9b03af --- /dev/null +++ b/Chapter9-1-1/compute-service/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.didispace + compute-service + 1.0.0 + jar + + compute-service + Spring Cloud project + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.RELEASE + + + + UTF-8 + 1.8 + + + + + 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 + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter9-1-1/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java b/Chapter9-1-1/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java new file mode 100755 index 0000000..52aeb49 --- /dev/null +++ b/Chapter9-1-1/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class ComputeServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(ComputeServiceApplication.class, args); + } + +} diff --git a/Chapter9-1-1/compute-service/src/main/java/com/didispace/web/ComputeController.java b/Chapter9-1-1/compute-service/src/main/java/com/didispace/web/ComputeController.java new file mode 100755 index 0000000..06dff1f --- /dev/null +++ b/Chapter9-1-1/compute-service/src/main/java/com/didispace/web/ComputeController.java @@ -0,0 +1,28 @@ +package com.didispace.web; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ComputeController { + + private final Logger logger = Logger.getLogger(getClass()); + + @Autowired + private DiscoveryClient client; + + @RequestMapping(value = "/add" ,method = RequestMethod.GET) + public Integer add(@RequestParam Integer a, @RequestParam Integer b) { + ServiceInstance instance = client.getLocalServiceInstance(); + Integer r = a + b; + logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); + return r; + } + +} \ No newline at end of file diff --git a/Chapter9-1-1/compute-service/src/main/resources/application.yml b/Chapter9-1-1/compute-service/src/main/resources/application.yml new file mode 100755 index 0000000..5abaf2a --- /dev/null +++ b/Chapter9-1-1/compute-service/src/main/resources/application.yml @@ -0,0 +1,15 @@ +# Spring properties +spring: + application: + name: compute-service + +# HTTP Server +server: + port: 2222 # HTTP (Tomcat) port + +# Discovery Server Access +eureka: + client: + serviceUrl: + defaultZone: http://localhost:1111/eureka/ + diff --git a/Chapter9-1-1/compute-service/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-1-1/compute-service/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..a2f4280 --- /dev/null +++ b/Chapter9-1-1/compute-service/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,40 @@ +package com.didispace; + +import com.didispace.web.ComputeController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new ComputeController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +} diff --git a/Chapter9-1-1/eureka-client/pom.xml b/Chapter9-1-1/eureka-client/pom.xml new file mode 100644 index 0000000..a52cebf --- /dev/null +++ b/Chapter9-1-1/eureka-client/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + com.didispace + eureka-client + 1.0.0 + jar + + eureka-client + Spring Cloud project + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.RELEASE + + + + UTF-8 + 1.8 + + + + + 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 + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + org.springframework.cloud + spring-cloud-starter-ribbon + + + + org.springframework.cloud + spring-cloud-starter-feign + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter9-1-1/eureka-client/src/main/java/com/didispace/ClientApplication.java b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/ClientApplication.java new file mode 100755 index 0000000..990445a --- /dev/null +++ b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/ClientApplication.java @@ -0,0 +1,23 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +@EnableEurekaClient +public class ClientApplication { + + @Bean + RestTemplate restTemplate() { + return new RestTemplate(); + } + + public static void main(String[] args) { + SpringApplication.run(ClientApplication.class, args); + } + +} diff --git a/Chapter9-1-1/eureka-client/src/main/java/com/didispace/service/ComputeService.java b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/service/ComputeService.java new file mode 100644 index 0000000..7a78246 --- /dev/null +++ b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/service/ComputeService.java @@ -0,0 +1,19 @@ +package com.didispace.service; + +import feign.Param; +import feign.RequestLine; +import org.springframework.cloud.netflix.feign.FeignClient; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/30 下午3:48. + * @blog http://blog.didispace.com + */ +@FeignClient(serviceId = "computeService", url = "http://COMPUTE-SERVICE/") +public interface ComputeService { + + @RequestLine("GET /add") + Integer add(@Param("a") Integer a, @Param("b") Integer b); + +} diff --git a/Chapter9-1-1/eureka-client/src/main/java/com/didispace/web/ClientController.java b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/web/ClientController.java new file mode 100755 index 0000000..2e03f7c --- /dev/null +++ b/Chapter9-1-1/eureka-client/src/main/java/com/didispace/web/ClientController.java @@ -0,0 +1,30 @@ +package com.didispace.web; + +import com.didispace.service.ComputeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class ClientController { + + @Autowired + RestTemplate restTemplate; + +// @Autowired +// private ComputeService computeService; + + @RequestMapping(value = "/add", method = RequestMethod.GET) + public String add() { + return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody(); + } + +// @RequestMapping("/add2") +// public String add2() { +// return computeService.add(10, 20).toString(); +// } + + +} \ No newline at end of file diff --git a/Chapter9-1-1/eureka-client/src/main/resources/application.yml b/Chapter9-1-1/eureka-client/src/main/resources/application.yml new file mode 100755 index 0000000..a361e8a --- /dev/null +++ b/Chapter9-1-1/eureka-client/src/main/resources/application.yml @@ -0,0 +1,14 @@ +# Spring Properties +spring: + application: + name: client + +# Discovery Server Access +eureka: + client: + serviceUrl: + defaultZone: http://localhost:1111/eureka/ + +# HTTP Server +server: + port: 3333 # HTTP (Tomcat) port \ No newline at end of file diff --git a/Chapter9-1-1/eureka-client/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-1-1/eureka-client/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..d5735ec --- /dev/null +++ b/Chapter9-1-1/eureka-client/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,25 @@ +package com.didispace; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = ClientApplication.class) +public class ApplicationTests { + + @Autowired + RestTemplate restTemplate; + + @Test + public void compute() throws Exception { + + System.out.println(restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody()); + + } + +} diff --git a/Chapter9-1-1/eureka-server/pom.xml b/Chapter9-1-1/eureka-server/pom.xml new file mode 100644 index 0000000..770fb70 --- /dev/null +++ b/Chapter9-1-1/eureka-server/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + com.didispace + eureka-server + 1.0.0 + jar + + eureka-server + Spring Cloud project + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.RELEASE + + + + UTF-8 + 1.8 + + + + + + org.springframework.cloud + spring-cloud-starter + + + + org.springframework.cloud + spring-cloud-starter-eureka-server + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter9-1-1/eureka-server/src/main/java/com/didispace/Application.java b/Chapter9-1-1/eureka-server/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..27e47f5 --- /dev/null +++ b/Chapter9-1-1/eureka-server/src/main/java/com/didispace/Application.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter9-1-1/eureka-server/src/main/resources/application.yml b/Chapter9-1-1/eureka-server/src/main/resources/application.yml new file mode 100755 index 0000000..75c344e --- /dev/null +++ b/Chapter9-1-1/eureka-server/src/main/resources/application.yml @@ -0,0 +1,12 @@ +# 配置应用服务器(Tomcat) +server: + port: 1111 # HTTP端口 + +# 配置发现服务器(Discovery Server) +eureka: + instance: + hostname: localhost + client: # 不是客户端,所以不注册自己 + registerWithEureka: false + fetchRegistry: false + diff --git a/Chapter9-1-1/service-account/src/main/java/com/didispace/AccountApplication.java b/Chapter9-1-1/service-account/src/main/java/com/didispace/AccountApplication.java new file mode 100755 index 0000000..1de615a --- /dev/null +++ b/Chapter9-1-1/service-account/src/main/java/com/didispace/AccountApplication.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; + +@SpringBootApplication +@EnableEurekaClient +public class AccountApplication { + + public static void main(String[] args) { + SpringApplication.run(AccountApplication.class, args); + } + +}