From a96570acd84829d880ef4e64b575e0ea10b0ab01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sun, 5 Mar 2023 18:14:26 +0800 Subject: [PATCH] servicelocatorfactorybeandemo --- servicelocatorfactorybean/pom.xml | 107 +++++++++++++ .../feign/TLogFeignConsumerRunner.java | 17 +++ .../TlogFeignConsumerController.java | 30 ++++ .../yomahub/tlog/example/feign/en/Client.java | 49 ++++++ .../tlog/example/feign/en/ContentType.java | 11 ++ .../yomahub/tlog/example/feign/en/Parser.java | 17 +++ .../tlog/example/feign/en/ParserConfig.java | 20 +++ .../tlog/example/feign/en/ParserFactory.java | 5 + .../tlog/example/feign/en/impl/CSVParser.java | 19 +++ .../example/feign/en/impl/JSONParser.java | 18 +++ .../src/main/resources/application.properties | 9 ++ .../src/main/resources/logback.xml | 47 ++++++ .../tlog-example-logback-feign-consumer.iml | 141 ++++++++++++++++++ 13 files changed, 490 insertions(+) create mode 100644 servicelocatorfactorybean/pom.xml create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/controller/TlogFeignConsumerController.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Client.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ContentType.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Parser.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserConfig.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserFactory.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/CSVParser.java create mode 100644 servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/JSONParser.java create mode 100644 servicelocatorfactorybean/src/main/resources/application.properties create mode 100644 servicelocatorfactorybean/src/main/resources/logback.xml create mode 100644 servicelocatorfactorybean/tlog-example-logback-feign-consumer.iml diff --git a/servicelocatorfactorybean/pom.xml b/servicelocatorfactorybean/pom.xml new file mode 100644 index 0000000..bf6147c --- /dev/null +++ b/servicelocatorfactorybean/pom.xml @@ -0,0 +1,107 @@ + + + + + + 8 + 8 + 2.1.8.RELEASE + Greenwich.SR3 + 2.1.0.RELEASE + 1.8 + 1.7.25 + 2.12.0 + 1.2.17 + 1.6.1 + 2.7.6 + 2.8.4 + 4.0.1 + 3.4.6 + 0.1 + 1.0.0 + 1.5.0 + + + 4.0.0 + com.yomahub.tlog.example + tlog-example-logback-feign-consumer + 1.0 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + org.springframework.boot + spring-boot-starter-logging + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.yomahub + tlog-feign-spring-boot-starter + ${tlog.version} + + + com.yomahub + tlog-web-spring-boot-starter + ${tlog.version} + + + org.projectlombok + lombok + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + + diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java new file mode 100644 index 0000000..62ccb0f --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java @@ -0,0 +1,17 @@ +package com.yomahub.tlog.example.feign; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +@EnableDiscoveryClient +public class TLogFeignConsumerRunner { + + public static void main(String[] args) { + SpringApplication.run(TLogFeignConsumerRunner.class, args); + + } +} diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/controller/TlogFeignConsumerController.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/controller/TlogFeignConsumerController.java new file mode 100644 index 0000000..e809443 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/controller/TlogFeignConsumerController.java @@ -0,0 +1,30 @@ +package com.yomahub.tlog.example.feign.controller; + +import com.yomahub.tlog.example.feign.en.Client; +import com.yomahub.tlog.example.feign.en.ContentType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class TlogFeignConsumerController { + + private Logger log = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private Client client; + + @RequestMapping("/hi") + public String sayHello(@RequestParam String name){ + log.info("invoke consumer controller method sayHello"); + List all = client.getAll(ContentType.CSV); + return "ok"; + } + + +} diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Client.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Client.java new file mode 100644 index 0000000..f79623c --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Client.java @@ -0,0 +1,49 @@ +package com.yomahub.tlog.example.feign.en; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.Reader; +import java.util.List; + +@Service +public class Client { +// private Parser csvParser, jsonParser; +// +// @Autowired +// public Client(Parser csvParser, Parser jsonParser) { +// this.csvParser = csvParser; +// this.jsonParser = jsonParser; +// } + + @Autowired + private ParserFactory parserFactory; + + public List getAll(ContentType contentType) { + + Reader reader = null; +// switch (contentType) { +// case CSV: +// return csvParser.parse(reader); +// case JSON: +// return jsonParser.parse(reader); +// } + List parse = parserFactory.getParser(contentType).parse(reader); + + return parse; + + } +} + + +// Parser parser = parserFactory.getParser(contentType); +// List parse = parser.parse(reader); + + +// private ParserFactory parserFactory; +// +// @Autowired +// public Client(ParserFactory parserFactory) { +// this.parserFactory = parserFactory; +// } \ No newline at end of file diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ContentType.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ContentType.java new file mode 100644 index 0000000..f8f2fd2 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ContentType.java @@ -0,0 +1,11 @@ +package com.yomahub.tlog.example.feign.en; + +/** + * @className: ContentType + * @author: 蜗牛 + * @date: 2023/3/4 + **/ +public enum ContentType { + JSON, + CSV +} diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Parser.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Parser.java new file mode 100644 index 0000000..dade2ea --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/Parser.java @@ -0,0 +1,17 @@ +package com.yomahub.tlog.example.feign.en; + +import java.io.Reader; +import java.util.List; + +/** + * Spring项目中用了服务定位模式, + * 消除代码中的if else,经理对我刮目相看! + * + * 比如根据文件的类型使用 CSV解析器或者JSON解析器, + * 在调用的客户端一般都是用if else去做判断, + * 比如类型等于JSON,我就用JSON解析器, + * 那如果新加一个类型的解析器,是不是调用的客户端还要修改呢? + */ +public interface Parser { + List parse(Reader r); +} diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserConfig.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserConfig.java new file mode 100644 index 0000000..4fdd556 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserConfig.java @@ -0,0 +1,20 @@ +package com.yomahub.tlog.example.feign.en; + + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.config.ServiceLocatorFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ParserConfig { + + @Bean("parserFactory") + public FactoryBean serviceLocatorFactoryBean() { + ServiceLocatorFactoryBean factoryBean = new ServiceLocatorFactoryBean(); + // 设置服务定位接口 + factoryBean.setServiceLocatorInterface(ParserFactory.class); + return factoryBean; + } + +} \ No newline at end of file diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserFactory.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserFactory.java new file mode 100644 index 0000000..cc409f0 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/ParserFactory.java @@ -0,0 +1,5 @@ +package com.yomahub.tlog.example.feign.en; + +public interface ParserFactory { + Parser getParser(ContentType contentType); +} \ No newline at end of file diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/CSVParser.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/CSVParser.java new file mode 100644 index 0000000..5cb78d5 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/CSVParser.java @@ -0,0 +1,19 @@ +package com.yomahub.tlog.example.feign.en.impl; + + +import com.yomahub.tlog.example.feign.en.Parser; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.io.Reader; +import java.util.List; + +@Component("CSV") +@Slf4j +public class CSVParser implements Parser { + @Override + public List parse(Reader r) { + log.info("用CSVParser解析。。。"); + return null; + } +} \ No newline at end of file diff --git a/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/JSONParser.java b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/JSONParser.java new file mode 100644 index 0000000..0ec31d5 --- /dev/null +++ b/servicelocatorfactorybean/src/main/java/com/yomahub/tlog/example/feign/en/impl/JSONParser.java @@ -0,0 +1,18 @@ +package com.yomahub.tlog.example.feign.en.impl; + +import com.yomahub.tlog.example.feign.en.Parser; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.io.Reader; +import java.util.List; + +@Component("JSON") +@Slf4j +public class JSONParser implements Parser { + @Override + public List parse(Reader r) { + log.info("用JSONParser解析。。。"); + return null; + } +} \ No newline at end of file diff --git a/servicelocatorfactorybean/src/main/resources/application.properties b/servicelocatorfactorybean/src/main/resources/application.properties new file mode 100644 index 0000000..962f5fa --- /dev/null +++ b/servicelocatorfactorybean/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=tlog-logback-feign-consumer +server.port=3111 +#eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka/ + +tlog.pattern=[$currIp][$spanId][$traceId] +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +http.connectTimeOutMillis=8000 +http.readTimeOutMillis=8000 diff --git a/servicelocatorfactorybean/src/main/resources/logback.xml b/servicelocatorfactorybean/src/main/resources/logback.xml new file mode 100644 index 0000000..02bc9ab --- /dev/null +++ b/servicelocatorfactorybean/src/main/resources/logback.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + ${LOG_HOME}/${APP_NAME}.log + + + ${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.%i.log + + 30 + + 1000MB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + diff --git a/servicelocatorfactorybean/tlog-example-logback-feign-consumer.iml b/servicelocatorfactorybean/tlog-example-logback-feign-consumer.iml new file mode 100644 index 0000000..f04e74f --- /dev/null +++ b/servicelocatorfactorybean/tlog-example-logback-feign-consumer.iml @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file