From 53558cc55e5a4e03db2a1b692ba6564f4d0fc30a Mon Sep 17 00:00:00 2001 From: Richie <280645618@qq.com> Date: Sun, 21 Jan 2024 17:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5SpringCloudGateway?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/pom.xml | 70 +++++++++++++++++++ api/src/main/java/com/qihang/api/Api.java | 20 ++++++ api/src/main/resources/application.yaml | 24 +++++++ api/src/test/java/com/qihang/AppTest.java | 38 ++++++++++ oms-api/pom.xml | 2 +- .../src/main/java/com/qihang/oms/api/Api.java | 2 +- .../com/qihang/oms/api/config/WebConfig.java | 16 +++++ oms-api/src/main/resources/application.yaml | 10 ++- pom.xml | 1 + 9 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 api/pom.xml create mode 100644 api/src/main/java/com/qihang/api/Api.java create mode 100644 api/src/main/resources/application.yaml create mode 100644 api/src/test/java/com/qihang/AppTest.java create mode 100644 oms-api/src/main/java/com/qihang/oms/api/config/WebConfig.java diff --git a/api/pom.xml b/api/pom.xml new file mode 100644 index 00000000..18990dcb --- /dev/null +++ b/api/pom.xml @@ -0,0 +1,70 @@ + + 4.0.0 + + com.qihang + qihang-oms + 1.0-SNAPSHOT + + + api + jar + + api + http://maven.apache.org + + + 17 + UTF-8 + UTF-8 + 3.0.2 + 2022.0.0.0 + + + + + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + ${spring-cloud-alibaba.version} + + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + 4.0.0 + + + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + diff --git a/api/src/main/java/com/qihang/api/Api.java b/api/src/main/java/com/qihang/api/Api.java new file mode 100644 index 00000000..eae9f356 --- /dev/null +++ b/api/src/main/java/com/qihang/api/Api.java @@ -0,0 +1,20 @@ +package com.qihang.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * Hello world! + * + */ +@EnableDiscoveryClient +@SpringBootApplication +public class Api +{ + public static void main( String[] args ) + { + System.out.println( "Hello World! Api" ); + SpringApplication.run(Api.class, args); + } +} diff --git a/api/src/main/resources/application.yaml b/api/src/main/resources/application.yaml new file mode 100644 index 00000000..e33d3598 --- /dev/null +++ b/api/src/main/resources/application.yaml @@ -0,0 +1,24 @@ +server: + port: 8080 + +spring: + application: + name: api-service + + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + username: nacos + password: nacos + gateway: + discovery: + locator: + enabled: true + routes: + - id: tao_oms_route + uri: lb://tao-oms # lb 表示从 nacos 中按照名称获取微服务,并遵循负载均衡策略,user-service 对应用户微服务应用名 + predicates: + - Path=/tao-api/** # 使用断言 + filters: + - StripPrefix=1 # 使用过滤器 \ No newline at end of file diff --git a/api/src/test/java/com/qihang/AppTest.java b/api/src/test/java/com/qihang/AppTest.java new file mode 100644 index 00000000..14e13c7a --- /dev/null +++ b/api/src/test/java/com/qihang/AppTest.java @@ -0,0 +1,38 @@ +package com.qihang; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/oms-api/pom.xml b/oms-api/pom.xml index b88b70fc..af9d1fc7 100644 --- a/oms-api/pom.xml +++ b/oms-api/pom.xml @@ -83,7 +83,7 @@ - + diff --git a/oms-api/src/main/java/com/qihang/oms/api/Api.java b/oms-api/src/main/java/com/qihang/oms/api/Api.java index 6edbeae1..d0f97041 100644 --- a/oms-api/src/main/java/com/qihang/oms/api/Api.java +++ b/oms-api/src/main/java/com/qihang/oms/api/Api.java @@ -16,7 +16,7 @@ import org.springframework.web.client.RestTemplate; * */ @EnableFeignClients(basePackages = "com.qihang.oms.api") -//@EnableDiscoveryClient +@EnableDiscoveryClient @ComponentScan(basePackages={"com.qihang"}) @SpringBootApplication public class Api diff --git a/oms-api/src/main/java/com/qihang/oms/api/config/WebConfig.java b/oms-api/src/main/java/com/qihang/oms/api/config/WebConfig.java new file mode 100644 index 00000000..27fe41cd --- /dev/null +++ b/oms-api/src/main/java/com/qihang/oms/api/config/WebConfig.java @@ -0,0 +1,16 @@ +//package com.qihang.oms.api.config; +// +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.http.codec.ServerCodecConfigurer; +//import org.springframework.http.codec.json.Jackson2JsonDecoder; +//import org.springframework.http.codec.json.Jackson2JsonEncoder; +//import org.springframework.web.reactive.config.WebFluxConfigurer; +// +//@Configuration +//public class WebConfig { +// @Bean +// public ServerCodecConfigurer serverCodecConfigurer() { +// return ServerCodecConfigurer.create(); +// } +//} diff --git a/oms-api/src/main/resources/application.yaml b/oms-api/src/main/resources/application.yaml index 1cd0f65e..51a90fe1 100644 --- a/oms-api/src/main/resources/application.yaml +++ b/oms-api/src/main/resources/application.yaml @@ -1,4 +1,3 @@ - spring: cloud: loadbalancer: @@ -8,6 +7,12 @@ spring: serverAddr: 127.0.0.1:8848 discovery: server-addr: 127.0.0.1:8848 + usename: nacos + passowrd: nacos +# gateway: +# discovery: +# locator: +# enabled: true # gateway 可以从 nacos 发现微服务 config: import: - nacos:qihang-oms.yaml?refresh=true @@ -16,6 +21,7 @@ spring: server: port: 8082 + #mybatis-plus: # mapper-locations: classpath*:mapper/**/*Mapper.xml -# type-aliases-package: com.qihang.core.domain.entity;com.qihang.erp.api.domain \ No newline at end of file +# type-aliases-package: com.qihang.core.domain.entity;com.qihang.erp.api.domain diff --git a/pom.xml b/pom.xml index ce3a95f0..5a467729 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ tao-oms oms-api oms-core + api