发表于: 2020-09-21 16:57:53
1 1381
今天完成的事
Zuul Demo。
config Server Demo。
Zuul路由网关配置
<!--路由网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 5000
spring:
application:
name: service-zuul
zuul:
routes:
ribbonapi:
path: /ribbonapi/**
serviceId: eureka-ribbon-client
hiapi:
path: /hiapi/**
serviceId: eureka-client
feignapi:
path: /feignapi/**
serviceId: eureka-feign-client
启动类加注解
启动 eureka-zuul-client、eureka-server、eureka-client(8762,8768两个端口)、eureka-ribbon-client、eureka-feign-client
在浏览器上重复访问下面的链接:
http://localhost:5000/feignapi/hi?name=spring
http://localhost:5000/hiapi/hi?name=spring
http://localhost:5000/ribbonapi/hi?name=spring
Zuul上配置熔断器
@Component
public class MyfallbackProvider implements FallbackProvider {
@Override
public String getRoute() {
return "eureka-client";
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "ok";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("oooops! error,i`m the fallback.".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers=new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}
关闭eureka-client全部实例后测试:
Zuul中使用过滤器
@Component
public class MyFilter extends ZuulFilter {
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext ctx=RequestContext.getCurrentContext();
HttpServletRequest request=ctx.getRequest();
Object accessToken=request.getParameter("token");
if(accessToken==null){
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
} catch (IOException e) {
return null;
}
}
return null;
}
}
测试:
无taken参数:
有token参数:
Config Server 从本地读取配置文件
config-server模块依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类加上注解
在工程的配置文件application.yml作相关配置,包括制定程序名为confi-server,端口号为8769.通过spring.profiles.active=native来配置Config Server从本地读取配置,读取配置路径为classpath下的shared目录。
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
application:
name: config-server
server:
port: 8769
shared文件下新建一个config-client-dev.yml配置文件
server:
port: 8762
foo: foo version 1
再创建一个config-client客户端
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
在bootstrap.yml配置文件中指定了程序名为config-client,向Url地址为http://localhost:8769的Config Server读取配置文件。如果没有读取成功,则执行快速失败(fail-fast)。
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8769
fail-fast: true
profiles:
active: dev
在config-client模块写一个接口验证,读取配置文件的foo变量,并通过API接口返回
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/foo")
public String hi(){
return foo;
}
}
从远程git仓库读取配置文件:
修改config-server的.yml配置文件
已经传到码云的config-client-dev.yml文件
测试
评论