代码拉取完成,页面将自动刷新
学习springcloud笔记:
一. 父pom文件的引入
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
<spring-boot.version>1.5.10.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 定义 spring cloud 版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 定义 springboot 版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
二. erueka注册中心
1. pom引入;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2. 主函数注解;
@EnableEurekaServer
3. application.yml文件;
eureka:
instance:
hostname: localhost
ip-address: localhost
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/
register-with-eureka: false
fetch-registry: false
三. eureka集群
修改application.yml文件(**spring.application.name可不相同**)
eureka:
instance:
hostname: localhost
ip-address: localhost
client:
serviceUrl:
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
register-with-eureka: false
fetch-registry: false
四. ribbon负载均衡
(客户端配置)
1. pom文件引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
2. 主函数配置restTemplate(@SpringBootApplication包含了@Configuration注解)
@Bean
@LoadBalanced // 开启rebbon的负载均衡(客户端的软负载均衡)
public RestTemplate restTemplate(){
return new RestTemplate();
}
3.自定义负载均衡规则
3.1 主函数添加:
@RibbonClient(name = "study-provider", configuration = RibbonRoleConfig.class)
3.2 新建类RibbonRoleConfig:
@Configuration
public class RibbonRoleConfig {
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
}
五. feign负载均衡
(客户端配置)
1. pom文件引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2. 主函数添加:
@EnableFeignClients(basePackages = {"com.customer.feign"})
3. 配置接口
@FeignClient(value = "${feign.provider}")
4. 自定义负载均衡规则
@Bean
@LoadBalanced
public IRule getRule(){
return new RandomRule();
}
六. 服务熔断
(服务端配置)
1. pom引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2. 主函数添加:
@EnableCircuitBreaker
3. 接口类方法添加
@HystrixCommand(fallbackMethod = "hystrix_demo")
4. 同接口新建hystrix_demo方法
public String hystrix_demo(){
return "服务出异常了,这是熔断返回值";
}
5. 访问 http://localhost:8081/feign.stream
七. 服务降级
(客户端配置)在feign基础上
1. yml文件修改配置
feign:
hystrix:
enabled: true
2. 接口类修改
@FeignClient(value = "${feign.provider}", fallbackFactory = MyFallBackFactory.class)
3. 新建类MyFallBackFactory
@Component
public class MyFallBackFactory implements FallbackFactory<TestFeignService> {
@Override
public TestFeignService create(Throwable cause) {
return new TestFeignService() {
@Override
public String getDemo() {
return "这里是服务降级~~生产者挂掉了!!!";
}
};
}
}
八. 熔断服务监控
1. pom引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
2. 主函数添加
@EnableHystrixDashboard
3. 访问 http://localhost:9001/hystrix
九. zuul网关
1. pom引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
2. 主函数添加
@EnableZuulProxy
3. yml文件添加
zuul:
routes:
my.serviceId: study-provider
my.path: /my/**
ignored-services: "*" #屏蔽原路由 http://localhost:9527/study-provider/demo, 使用 http://localhost:9527/my/demo
prefix: /api # 统一添加前缀 http://localhost:9527/api/my/demo
十. 配置中心服务端
连接远程仓库
1. pom文件引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--解决git插件报的异常-->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.8.0.201706111038-r</version>
</dependency>
2. 主函数添加
@EnableConfigServer
3. yml文件添加
spring:
application:
name: study-config
cloud:
config:
server:
git:
uri: git@gitee.com:zlfmm/example_study_config.git
4. gitee创建example_study_config远程仓库
4.1 提交文件application.yml文件
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
application:
name: study-config-dev
---
spring:
profiles: test
application:
name: study-config-test
4.2 访问 http://localhost:9999/application-dev.yml
十一. 配置中心客户端
配置服务地址,从而从远程仓库取配置数据
1. pom文件引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2. 主函数添加
@EnableEurekaClient
3. 新建bootstrap.yml文件
spring:
cloud:
config:
name: study-config-client #需要从gitee上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://localhost:9999 # 本微服务启动后先去找9999号服务,通过SpringCloudConfig获取Gitee的服务地址
4. 新建application.yml文件
spring:
application:
name: study-config-client
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。