提交 a6a3112e 编写于 作者: Q qinyingjie

fix:feign和hystrix单独一个模块

上级 be6f985f
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
<module>springcloud-service-goods-9300</module> <module>springcloud-service-goods-9300</module>
<module>springcloud-service-common</module> <module>springcloud-service-common</module>
<module>springcloud-service-portal</module> <module>springcloud-service-portal</module>
<module>springcloud-service-config</module>
<module>springcloud-service-feign</module>
<module>springcloud-service-eureka</module> <module>springcloud-service-eureka</module>
</modules> </modules>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-config</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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);
}
}
#端口号
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
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PortalApplicationTests {
@Test
void contextLoads() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-feign</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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);
}
}
...@@ -16,7 +16,7 @@ import java.io.Serializable; ...@@ -16,7 +16,7 @@ import java.io.Serializable;
* @version : 2.2.0 * @version : 2.2.0
* @date : 2023/1/10 13:45 * @date : 2023/1/10 13:45
*/ */
@FeignClient("goods-service/kwanGoodsInfo") @FeignClient(value = "goods-service/kwanGoodsInfo", fallback = ComputeClientHystrix.class)
public interface ComputeClient { public interface ComputeClient {
@RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) @RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
......
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方式请求 服务不可用 请求失败");
}
}
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方式请求 服务不可用 请求失败");
}
}
#端口号
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
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PortalApplicationTests {
@Test
void contextLoads() {
}
}
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.kwan.springcloud</groupId> <groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId> <artifactId>springcloud-service-common</artifactId>
...@@ -26,23 +25,11 @@ ...@@ -26,23 +25,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
...@@ -51,5 +38,4 @@ ...@@ -51,5 +38,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
...@@ -3,10 +3,8 @@ package com.kwan.springcloud; ...@@ -3,10 +3,8 @@ package com.kwan.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient @EnableEurekaClient
@SpringBootApplication @SpringBootApplication
public class PortalApplication { public class PortalApplication {
......
package com.kwan.springcloud.controller; package com.kwan.springcloud.controller;
import com.kwan.springcloud.response.Result; import com.kwan.springcloud.response.Result;
import com.kwan.springcloud.service.PortalService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; 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; ...@@ -21,23 +18,11 @@ import java.io.Serializable;
* @date : 2023/1/8 11:12 * @date : 2023/1/8 11:12
*/ */
@RestController @RestController
@RequestMapping("portal") @RequestMapping("/portal")
public class PortalController { 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 @Autowired
private RestTemplate restTemplate; private PortalService portalService;
/** /**
* 通过主键查询单条数据 * 通过主键查询单条数据
...@@ -46,9 +31,7 @@ public class PortalController { ...@@ -46,9 +31,7 @@ public class PortalController {
* @return 单条数据 * @return 单条数据
*/ */
@GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) @GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result selectOne(@PathVariable Serializable id) { public Result selectOne(@PathVariable String id) {
//调用远程的controller return portalService.addService(id);
ResponseEntity<Result> forEntity = restTemplate.getForEntity(GOODS_URL_2 + id, Result.class);
return Result.ok(forEntity);
} }
} }
\ No newline at end of file
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();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册