<!--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: "*"