首先在父工程定义好 SpringCloud 版本
1 2 3 4 5 6 7 8 9 10 11
| <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
服务发现——Netflix Eureka
创建 Eureka 模块
pom 文件添加
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
|
application.yml 添加配置
1 2 3 4 5 6 7 8
| server: port: 6868 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:${server.port}/eureka/
|
启动类上添加注解
其他模块注册到 Eureka 上面
pom 文件添加
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
|
application.yml 添加配置
1 2 3 4 5 6 7 8
| server: port: 6868 eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true
|
启动类上添加注解
服务调用——Netflix Feign
在需要的调用其他模块的模块中修改
pom 文件添加
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
|
application.yml 添加配置
1 2 3 4 5 6 7 8 9 10 11
| server: port: 9001 spring: application: name: tensquare-base eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true
|
启动类上添加注解
1 2
| @EnableDiscoveryClient @EnableFeignClients
|
创建 client 包,里面创建接口
1 2 3 4 5 6 7
| @FeignClient(value = "TENSQUARE-BASE", fallback = BaseClientImpl.class) public interface BaseClient {
@RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET) public Result findById(@PathVariable("labelId") String labelId);
}
|
熔断器——Netflix Hystrix
application.yml 添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server: port: 9001 spring: application: name: tensquare-base eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true feign: hystrix: enabled: true
|
创建 client 接口实现类
1 2 3 4 5 6 7
| @Component public class BaseClientImpl implements BaseClient { @Override public Result findById(String labelId) { return new Result(false, StatusCode.ERROR, "熔断器"); } }
|
修改之前写的接口上的注解,加入 fallback 参数为实现类
1 2 3 4 5 6 7
| @FeignClient(value = "TENSQUARE-BASE", fallback = BaseClientImpl.class) public interface BaseClient {
@RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET) public Result findById(@PathVariable("labelId") String labelId);
}
|
服务网关——Netflix Zuul
创建网关模块
pom 文件添加
1 2 3 4 5 6 7 8
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
|
application.yml 添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| server: port: 9011 spring: application: name: tensquare-manager eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true zuul: routes: TENSQUARE-BASE: path: /base/** serviceId: TENSQUARE-BASE TENSQUARE-USER: path: /user/** serviceId: TENSQUARE-USER TENSQUARE-QA: path: /qa/** serviceId: TENSQUARE-QA
|
启动类上添加注解
Zuul 过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| @Component public class WebFilter extends ZuulFilter { @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 currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); String authorization = request.getHeader("Authorization"); if (authorization != null && !"".equals(authorization)) { currentContext.addZuulRequestHeader("Authorization", authorization); } return null;
RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest();
if (request.getMethod().equals("OPTIONS")) { return null; }
if (request.getRequestURL().indexOf("login") > 0) { return null; }
String authorization = request.getHeader("Authorization"); if (authorization != null && !"".equals(authorization)) { if (authorization.startsWith("Bearer")) { String token = authorization.substring(7); System.out.println(token); try { Claims claims = jwtUtil.parseJWT(token); String roles = (String) claims.get("roles"); System.out.println(roles); if (roles.equals("admin")) { currentContext.addZuulRequestHeader("Authorization", authorization); return null; } } catch (Exception e) { currentContext.setSendZuulResponse(false); } } } currentContext.setSendZuulResponse(false); currentContext.setResponseStatusCode(403); currentContext.setResponseBody("权限不足"); currentContext.getResponse().setContentType("text/html;charset=utf-8"); return null; } }
|
分布式配置——Spring Cloud Config
将配置文件提交到码云 格式为{application}-{profile}.yml,例如 base-dev.yml
开启 RebbitMQ
创建配置文件微服务
pom 文件添加
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
|
application.yml 添加配置
1 2 3 4 5 6 7 8 9 10
| server: port: 12000 spring: application: name: tensquare-config cloud: config: server: git: uri: https://gitee.com/jwhuang10/tensquare_dev.git
|
启动类上添加注解
配置客户端
pom 文件添加
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
|
添加 bootstrap.yml ,删除 application.yml
1 2 3 4 5 6 7
| spring: cloud: config: name: base profile: dev label: master uri: http://127.0.0.1:12000
|
消息总线 —— Spring Cloud Bus
修改配置文件微服务
pom 文件添加
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
|
修改 application.yml
1 2 3 4 5 6 7 8 9 10
| rabbitmq: host: 192.168.0.10 management: endpoints: web: exposure: include: "*" endpoint: bus-refresh: enabled: true
|
配置客户端
pom 文件添加
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
修改码云上面对应的 application.yml,添加 rabbitmq 配置
1 2 3 4 5 6 7
| server: port: 9001 spring: application: name: tensquare-base rabbitmq: host: 192.168.0.10
|
每次修改码云上的配置文件,发送 POST 请求到http://127.0.0.1:12000/actuator/bus-refresh,即可修改微服务配置
修改 application.yml 的自定义配置项
需要在 controller 上添加以下注解