# example_study **Repository Path**: zlfmm/example_study ## Basic Information - **Project Name**: example_study - **Description**: Spring Cloud框架学习demo代码。 包含了eureka、zuul、ribbon、feign、admin、config server组件及其配置方法 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-07-18 - **Last Updated**: 2021-04-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 学习springcloud笔记: 一. 父pom文件的引入

UTF-8 UTF-8 1.8 1.8 Edgware.SR3 1.5.10.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import 二. erueka注册中心

1. pom引入; org.springframework.cloud spring-cloud-starter-eureka-server 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文件引入 org.springframework.cloud spring-cloud-starter-ribbon 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文件引入 org.springframework.cloud spring-cloud-starter-feign 2. 主函数添加: @EnableFeignClients(basePackages = {"com.customer.feign"}) 3. 配置接口 @FeignClient(value = "${feign.provider}") 4. 自定义负载均衡规则 @Bean @LoadBalanced public IRule getRule(){ return new RandomRule(); } 六. 服务熔断

(服务端配置) 1. pom引入 org.springframework.cloud spring-cloud-starter-hystrix 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 { @Override public TestFeignService create(Throwable cause) { return new TestFeignService() { @Override public String getDemo() { return "这里是服务降级~~生产者挂掉了!!!"; } }; } } 八. 熔断服务监控

1. pom引入 org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-hystrix-dashboard 2. 主函数添加 @EnableHystrixDashboard 3. 访问 http://localhost:9001/hystrix 九. zuul网关

1. pom引入 org.springframework.cloud spring-cloud-starter-zuul 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文件引入 org.springframework.cloud spring-cloud-config-server org.eclipse.jgit org.eclipse.jgit 4.8.0.201706111038-r 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文件引入 org.springframework.cloud spring-cloud-starter-config 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