<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class ConfigServerStar {
public static void main(String[] args) {
SpringApplication.run(ConfigServerStar.class, args);
}
}
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
####注册中心应用名称
name: config
cloud:
config:
server:
git:
###git环境地址
uri: https://gitee.com/XXXXX/testspringcloud.git
####svn项目下属搜索目录
search-paths:
- testspringcloud
####读取分支
label: master
###解决中文乱码
http:
encoding:
charset: UTF-8
enabled: true
force: true
####端口号
server:
port: 9999
<!--网关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!--监控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulStar {
public static void main(String[] args) {
SpringApplication.run(ZuulStar.class,args);
}
// zuul配置能够使用config实现实时更新
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
import com.netflix.discovery.util.StringUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class TokeFilter extends ZuulFilter {
/**
* pre:可以在请求被路由之前调用
* route:在路由请求时候被调用
* post:在route和error过滤器之后被调用
* error:处理请求时发生错误时被调用
*
*/
@Override
public String filterType() {
return "pre";
}
//过滤器执行顺序
@Override
public int filterOrder() {
return 0;
}
//是否开启过滤器
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context=RequestContext.getCurrentContext();
HttpServletRequest request= context.getRequest();
HttpServletResponse response= context.getResponse();
String username=request.getParameter("userName");
if(StringUtils.isEmpty(username)){
context.setSendZuulResponse(false);
context.setResponseBody("没有登录");
return null;
}
return null;
}
}
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ErrorHandlerController implements ErrorController {
// 出异常后进入该方法,交由下面的方法处理
@Override
public String getErrorPath() {
return "/error";
}
@RequestMapping("/error")
public String error() {
return "出现异常";
}
}
###服务注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
###api网关端口号
server:
port: 80
###网关名称
spring:
application:
#git上配置文件名称
name: zuul
cloud:
config:
####读取后缀
profile: dev
####读取config-server注册地址
discovery:
#configServer在注册中心的别名
service-id: config
enabled: true
http:
encoding:
charset: UTF-8
enabled: true
force: true
###默认服务读取eureka注册服务列表 默认间隔30秒
###开启所有监控中心接口
management:
endpoints:
web:
exposure:
include: "*"