提交 cf53e5fa 编写于 作者: Q qinyingjie

fix:添加consul

上级 f6fc046e
......@@ -34,6 +34,7 @@
<module>springcloud-service-config-client</module>
<module>springcloud-service-feign</module>
<module>springcloud-service-zuul</module>
<module>springcloud-service-consul</module>
<module>springcloud-service-eureka</module>
</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-consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-consul</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-consul-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.kwan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
package com.kwan.springcloud.controller;
import com.kwan.springcloud.response.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* portal控制器
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/8 11:12
*/
@RestController
@RequestMapping("/consul")
public class ConsulController {
@GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result test() {
return Result.ok("consul 访问成功");
}
}
\ No newline at end of file
#端口号
server:
port: 8500
spring:
application:
name: consul-service #服务名称
cloud:
consul:
host: localhost
port: 8500
\ 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() {
}
}
......@@ -13,7 +13,7 @@ import java.io.Serializable;
import java.util.List;
@RestController
@RequestMapping("kwanGoodsInfo")
@RequestMapping("/kwanGoodsInfo")
public class KwanGoodsInfoController {
/**
* 服务对象
......
......@@ -13,7 +13,7 @@ import java.io.Serializable;
import java.util.List;
@RestController
@RequestMapping("kwanGoodsInfo")
@RequestMapping("/kwanGoodsInfo")
public class KwanGoodsInfoController {
/**
* 服务对象
......
......@@ -13,7 +13,7 @@ import java.io.Serializable;
import java.util.List;
@RestController
@RequestMapping("kwanGoodsInfo")
@RequestMapping("/kwanGoodsInfo")
public class KwanGoodsInfoController {
/**
* 服务对象
......
......@@ -34,4 +34,10 @@ public class PortalController {
public Result selectOne(@PathVariable String id) {
return portalService.addService(id);
}
@GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result test() {
return Result.ok("portal 访问成功");
}
}
\ No newline at end of file
......@@ -34,9 +34,8 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
......
package com.kwan.springcloud;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* FallbackProvider(请求异常处理)
* <p>
* 网关的默认回退FallbackProvider的机制只适用于通过配置文件中配置的url来跨服务调用,而不拦截通过FeignClient跨服务调用,
* 也就是说要自己实现一个FeignClient服务的fallback才可以,但是如果通过FeignClient的服务要是特别多怎么办,每一个都要写一个不得累死,
* 这里想到了通过网关的错误过滤器来统一处理FeignClient跨服务调用出错的问题
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/11 12:48
*/
@Component
public class GoodsServiceFallBackProvider implements FallbackProvider {
@Override
public String getRoute() {
return "goods-service";
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("order-service is not available!".getBytes());
}
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}
@Override
public int getRawStatusCode() throws IOException {
return this.getStatusCode().value();
}
@Override
public String getStatusText() throws IOException {
return this.getStatusCode().getReasonPhrase();
}
@Override
public void close() {
}
};
}
}
package com.kwan.springcloud.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class MyZuulFilter extends ZuulFilter {
/**
* 表示当前的过滤器是否被调用
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* filterOrder表示执行的优先级,值越小表示优先级越高
*/
@Override
public int filterOrder() {
return 0;
}
/**
* pre: 在请求被路由之前调用
* routing: 在请求被路由之中调用
* post: 在请求被路由之后调用
* error: 处理请求发生错误时调用
*/
@Override
public String filterType() {
return "pre";
}
/**
* 真正执行Filter逻辑的方法
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
System.err.println("--------------uri: -------------- " + request.getRequestURI());
return ctx;
}
}
\ No newline at end of file
......@@ -11,14 +11,28 @@ zuul:
sensitiveHeaders: Cookie,Set-Cookie,Authorization
routes:
api-portal:
path: /portal-service/** #访问路径:http:/localhost:5001/hello-service/hello
path: /portal-service/** #访问路径:http:/localhost:8888/portal-service/portal/1
serviceId: portal-service
api-goods:
path: /goods-service/**
serviceld: goods-service
url: http://localhost:9100/,http://localhost:9200/,http://localhost:9300/
retryable: true
semaphore:
max-semaphores: 100
ribbon-isolation-strategy: thread
# prefix: /api
strip-prefix: false
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
\ No newline at end of file
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
ribbon:
ConnectTimeout: 250 #连接超时时间(ms),默认值为250ms
ReadTimeout: 2000 #通信超时时间(ms),默认值为2000ms
MaxAutoRetriesNextServer: 0 #对同一服务不同实例重试次数(同一服务下集群个数的重试次数)
MaxAutoRetries: 2 #对同一实例重试的次数(单个集群节点服务重试的次数)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册