diff --git a/pom.xml b/pom.xml index b9066ab89a7ef4db71f737ee6a8c3926517cd03e..c49c15d166560e5b43325c2b2291c2a0296fdb8e 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,8 @@ springcloud-service-goods-9300 springcloud-service-common springcloud-service-portal + springcloud-service-config + springcloud-service-feign springcloud-service-eureka diff --git a/springcloud-service-config/pom.xml b/springcloud-service-config/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..b07b50d9f13af0eae6de31c2891c5e1fcf81ec65 --- /dev/null +++ b/springcloud-service-config/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.kwan.springcloud + springcloud-service-parent + 1.0.0 + + springcloud-service-config + 0.0.1-SNAPSHOT + springcloud-service-config + + 1.8 + + + + + com.kwan.springcloud + springcloud-service-common + 1.0.0 + compile + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + + org.springframework.cloud + spring-cloud-config-server + 1.4.7.RELEASE + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springcloud-service-config/src/main/java/com/kwan/springcloud/ConfigApplication.java b/springcloud-service-config/src/main/java/com/kwan/springcloud/ConfigApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..f6341b9e2f986a10530f78c6cd7eb41d1203d616 --- /dev/null +++ b/springcloud-service-config/src/main/java/com/kwan/springcloud/ConfigApplication.java @@ -0,0 +1,18 @@ +package com.kwan.springcloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.config.server.EnableConfigServer; + +@EnableDiscoveryClient +@EnableConfigServer +@SpringBootApplication +public class ConfigApplication { + + public static void main(String[] args) { + + SpringApplication.run(ConfigApplication.class, args); + } + +} diff --git a/springcloud-service-config/src/main/resources/application.yaml b/springcloud-service-config/src/main/resources/application.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c1066c8802bcc0a7a5e90cd1c2b28d6efbb6499f --- /dev/null +++ b/springcloud-service-config/src/main/resources/application.yaml @@ -0,0 +1,21 @@ +#端口号 +server: + port: 7001 + +spring: + application: + name: config-service #服务名称 + cloud: + config: + server: + git: + uri: http://gitcode.net/qyj19920704/springcloud-config + searchPaths: config-repo + username: 13113691357 + password: 15671628341qwe + +#服务提供者 +eureka: + client: + service-url: + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file diff --git a/springcloud-service-config/src/test/java/com/kwan/springcloud/PortalApplicationTests.java b/springcloud-service-config/src/test/java/com/kwan/springcloud/PortalApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..1a8e650e3ec9e02f15e843b39496d32bf600724b --- /dev/null +++ b/springcloud-service-config/src/test/java/com/kwan/springcloud/PortalApplicationTests.java @@ -0,0 +1,13 @@ +package com.kwan.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PortalApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springcloud-service-feign/pom.xml b/springcloud-service-feign/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..7e00591b1c3e61e1b1ee416a8b15ed546dd6fb7e --- /dev/null +++ b/springcloud-service-feign/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.kwan.springcloud + springcloud-service-parent + 1.0.0 + + springcloud-service-feign + 0.0.1-SNAPSHOT + springcloud-service-feign + + 1.8 + + + + + com.kwan.springcloud + springcloud-service-common + 1.0.0 + compile + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + + org.springframework.cloud + spring-cloud-starter-feign + 1.4.7.RELEASE + + + + + org.springframework.cloud + spring-cloud-starter-hystrix + 1.4.7.RELEASE + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..a70cd57da4f08203dd23b4d12677e626f5bb24c5 --- /dev/null +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java @@ -0,0 +1,20 @@ +package com.kwan.springcloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@EnableCircuitBreaker +@EnableFeignClients +@EnableEurekaClient +@SpringBootApplication +public class FeignApplication { + + public static void main(String[] args) { + + SpringApplication.run(FeignApplication.class, args); + } + +} diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClient.java similarity index 88% rename from springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java rename to springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClient.java index a563cd3727f25b3ccdee7827136dbb96712277ed..29b8d64b068e4fa3c0a91e202b40b02e7611d9ef 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClient.java @@ -16,7 +16,7 @@ import java.io.Serializable; * @version : 2.2.0 * @date : 2023/1/10 13:45 */ -@FeignClient("goods-service/kwanGoodsInfo") +@FeignClient(value = "goods-service/kwanGoodsInfo", fallback = ComputeClientHystrix.class) public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java new file mode 100644 index 0000000000000000000000000000000000000000..a70fc7e3cc649f94c0e15c745b4948482d77266a --- /dev/null +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java @@ -0,0 +1,15 @@ +package com.kwan.springcloud.api; + +import com.kwan.springcloud.response.Result; +import org.springframework.stereotype.Component; + +import java.io.Serializable; + +@Component +public class ComputeClientHystrix implements ComputeClient { + + @Override + public Result selectOne(Serializable id) { + return Result.error("feign方式请求 服务不可用 请求失败"); + } +} diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/controller/FeignController.java similarity index 100% rename from springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java rename to springcloud-service-feign/src/main/java/com/kwan/springcloud/controller/FeignController.java diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java new file mode 100644 index 0000000000000000000000000000000000000000..37072a0f31532a85d57b4e2b0f5428352022f6fb --- /dev/null +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java @@ -0,0 +1,40 @@ +package com.kwan.springcloud.service; + +import com.kwan.springcloud.response.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Slf4j +@Service +public class FeignService { + + /** + * http直接访问 + */ + private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/"; + + + /** + * eureka访问 + */ + private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/"; + + + @Autowired + RestTemplate restTemplate; + + // @HystrixCommand(fallbackMethod = "addServiceFallback") + public Result addService(String id) { + + //调用远程的controller + return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); + } + + public Result addServiceFallback(String id) { + log.info("失败id={}", id); + return Result.error("ribbon方式请求 服务不可用 请求失败"); + } + +} diff --git a/springcloud-service-feign/src/main/resources/application.yaml b/springcloud-service-feign/src/main/resources/application.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c3ded33e9b3338f3a261448021d0b4b4b3f438dc --- /dev/null +++ b/springcloud-service-feign/src/main/resources/application.yaml @@ -0,0 +1,13 @@ +#端口号 +server: + port: 8081 + +spring: + application: + name: feign-service #服务名称 + +#服务提供者 +eureka: + client: + service-url: + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file diff --git a/springcloud-service-feign/src/test/java/com/kwan/springcloud/FeignApplicationTests.java b/springcloud-service-feign/src/test/java/com/kwan/springcloud/FeignApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..1a8e650e3ec9e02f15e843b39496d32bf600724b --- /dev/null +++ b/springcloud-service-feign/src/test/java/com/kwan/springcloud/FeignApplicationTests.java @@ -0,0 +1,13 @@ +package com.kwan.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PortalApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springcloud-service-portal/pom.xml b/springcloud-service-portal/pom.xml index 4bde00f6e61ff4c3a1be2ddaaa260bfe56f2905e..915faf42d347d405066ce54d1a42ac774dfe93e0 100644 --- a/springcloud-service-portal/pom.xml +++ b/springcloud-service-portal/pom.xml @@ -14,7 +14,6 @@ 1.8 - com.kwan.springcloud springcloud-service-common @@ -26,23 +25,11 @@ spring-boot-starter-test test - - org.springframework.cloud spring-cloud-starter-netflix-eureka-server - - - - org.springframework.cloud - spring-cloud-starter-feign - 1.4.7.RELEASE - - - - @@ -51,5 +38,4 @@ - - + \ No newline at end of file diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java index 9410f71c1c45f2107147d8d98af3fd9c35d75562..d278f2f925dc3b050734e8429d9379a13bea0c36 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java @@ -3,10 +3,8 @@ package com.kwan.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.cloud.openfeign.EnableFeignClients; -@EnableFeignClients @EnableEurekaClient @SpringBootApplication public class PortalApplication { diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java index 4fcab8bb72c0f5cb4ec152437c920490486ef747..091656bb379681569ac13016e7b81442884da50c 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java @@ -1,16 +1,13 @@ package com.kwan.springcloud.controller; import com.kwan.springcloud.response.Result; +import com.kwan.springcloud.service.PortalService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - -import java.io.Serializable; /** @@ -21,23 +18,11 @@ import java.io.Serializable; * @date : 2023/1/8 11:12 */ @RestController -@RequestMapping("portal") +@RequestMapping("/portal") public class PortalController { - /** - * http直接访问 - */ - private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/"; - - /** - * eureka访问 - */ - private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/"; - /** - * 服务对象 - */ @Autowired - private RestTemplate restTemplate; + private PortalService portalService; /** * 通过主键查询单条数据 @@ -46,9 +31,7 @@ public class PortalController { * @return 单条数据 */ @GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) - public Result selectOne(@PathVariable Serializable id) { - //调用远程的controller - ResponseEntity forEntity = restTemplate.getForEntity(GOODS_URL_2 + id, Result.class); - return Result.ok(forEntity); + public Result selectOne(@PathVariable String id) { + return portalService.addService(id); } } \ No newline at end of file diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java new file mode 100644 index 0000000000000000000000000000000000000000..4f7f62a99a1fdc75874b598ee7cef52f03304970 --- /dev/null +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java @@ -0,0 +1,34 @@ +package com.kwan.springcloud.service; + +import com.kwan.springcloud.response.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Slf4j +@Service +public class PortalService { + + /** + * http直接访问 + */ + private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/"; + + + /** + * eureka访问 + */ + private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/"; + + + @Autowired + RestTemplate restTemplate; + + + public Result addService(String id) { + //调用远程的controller + return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); + } + +}