一.服务注册和发现 netflix Eureka
有一种保护模式
服务端:
1.导坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐eurekaserver</artifactId>
</dependency>
2.配置文件
3.启动类
加上注解 @EnableEurekaServer
服务注册:
1.添加坐标
2.配置文件
3.启动类增加注解@EnableEurekaClient
二.服务调用 netflix Feign
负载均衡的调用服务
在调用的项目中
1.导坐标
2.启动类中增加注解@EnableFeignClient
3.写接口
@FeignClient("tensquere-base")
public interface LabelClient {
@RequestMapping(value="/label/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable("id") String id);
}
三.熔断器 netflix Hystrix
Feign 本身支持Hystrix,不需要额外引入依赖
1.增加配置文件
feign: hystrix: enabled: true
2.增加接口的实现类
@component
public class LabelClientImpl implements LabelClient {
@Override
public Result findById(String id) {
return new Result(false, StatusCode.ERROR,"熔断器启动了");
}
}
3.修改接口上的注解
@FeignClient(value="tensquare‐base",fallback = LabelClientImpl.class)
四.微服务网关 netflix Zuul
1.导坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐eurekaclient</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐zuul</artifactId>
</dependency>
2.增加配置文件
zuul:
routes: tensquare‐gathering: #活动
path: /gathering/** #配置请求URL的请求规则
serviceId: tensquare‐gathering #指定Eureka注册中心中的服务id
3.启动类
@EnableZuulProxy
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.过滤类
@Component public class WebFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";// 前置过滤器 pre route post error
}
@Override
public int filterOrder() {
return 0;// 优先级为0,数字越大,优先级越低
}
@Override
public boolean shouldFilter() {
return true;// 是否执行该过滤器,此处为true,说明需要过滤
}
@Override
public Object run() throws ZuulException {
System.out.println("zuul过滤器...");
return null;
}
}
五.分布式配置 springCloud config
配置中心微服务
1.导坐标
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐config‐server</artifactId>
</dependency>
</dependencies>
2.配置文件
3.启动类
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
客户端
1.导坐标
2.添加bootstrap.yml ,删除application.yml
六.消息通知总线 springCloud Bus
服务端
1.导坐标
2.配置文件
客户端
1.导坐标
2.马云上的配置文件增加配置
rabbitmq:
host: 192.168.184.144
修改马云上的配置文件后 要执行 Url: http://127.0.0.1:12000/actuator/bus-refresh Method: post
如果是自定义配置文件 则要在类上增加注解@RefreshScope