• eureka—-服务的注册中心,springcloud的所有模块都必须注册到eureka,包括eureka自己
  • feign—-写进controller层替代原来的controller层方法,可以掩盖服务真正的路径,并自动拉取和使用服务
  • gateway
  • ribbon
  • hystrix

Eureka

1.1 导入pom文件

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>

1.2 编写application.yml

  1. eureka:
  2. client:
  3. service-url:
  4. defaultZone: ${defaultZone:http://127.0.0.1:10087/eureka}
  5. # true表示注册自己,如果eureka只有一个,则不注册自己并且不拉取服务
  6. register-with-eureka: true
  7. # true表示拉取服务
  8. fetch-registry: true
  9. spring:
  10. application:
  11. name: eureka-server
  12. server:
  13. port: ${port:10086}

1.3 编写启动类

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServerApplication.class);
  6. }
  7. }

1.4 启动方法

启动第一个注册中心,以10086端口启动,注册服务到http://127.0.0.1:10087/eureka
启动第二个注册中心,以10087端口启动,注册服务到http://127.0.0.1:10086/eureka.使用jvm配置的方法启动:-Dport=10087 -DdefaultZone=http://127.0.0.1:10086/eureke

服务提供方

1.1 导入pom文件

  1. <!--eureka依赖-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <!--这里使用的是client,因为虽然是服务提供方,但是相对于注册中心,他只是注册中心的一个成员-->
  5. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  6. </dependency>
  7. <!--web依赖-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>

1.2 导入application.yml

  1. server:
  2. port: 9091
  3. spring:
  4. application:
  5. name: user-service
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://127.0.0.1:10086/eureka
  10. instance:
  11. ip-address: 127.0.0.1
  12. prefer-ip-address: true

1.3 编写启动类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class userServiceApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(userServiceApplication.class);
  6. }
  7. }

Feign(服务消费方)

1.1 导入pom文件

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-openfeign</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  12. </dependency>

1.2 编写application.yml

  1. server:
  2. port: 10100
  3. spring:
  4. application:
  5. name: feign
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://127.0.0.1:10086/eureka

含义:这个项目以端口10100启动,从http://127.0.0.1:10086/eureka上注册和拉取服务

1.3 编写启动类

  1. @SpringBootApplication
  2. @EnableFeignClients
  3. public class ConsumerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConsumerApplication.class, args);
  6. }
  7. }

1.4 编写FeignClient

  1. //注解的含义是,从注册中心拉取'user-service'并且根据
  2. @FeignClient("user-service")
  3. public interface Client {
  4. @RequestMapping("/user/{id}")
  5. public Map get(@PathVariable("id") int id);
  6. }

1.5 编写FeignController

  1. @RestController
  2. @RequestMapping("/feign")
  3. public class Controller {
  4. @Autowired
  5. //注入上一步的FeignClient
  6. private Client client;
  7. @RequestMapping("{id}")
  8. public Map get(@PathVariable int id){
  9. //访问本端口/feign/8,会调用FeignClient接口,真正的去调取服务user-service/user/8
  10. return client.get(id);
  11. }
  12. }

1.6 开启熔断保护

(feign已经默认开启了ribbon负载均衡)
1.6.1 修改application.yml文件

  1. //application.yml
  2. feign:
  3. hystrix:
  4. enabled: true #开启hystrix熔断保护,默认是关闭的

1.6.2 新建异常处理类

  1. //注入到spring中
  2. @Component
  3. //继承之前写的Client接口,这个Client没有特别的含义,是1.4步骤写的类
  4. public class ClientFallback implements Client {
  5. @Override
  6. public Map get(int id) {
  7. HashMap<Object, Object> map = new HashMap<>();
  8. map.put("msg","信息出错了");
  9. return map;
  10. }
  11. }

1.6.3 修改Client类

  1. @FeignClient(value="user-service",fallback = ClientFallback.class) //在这里加了一个fallback,其他未变
  2. public interface Client {
  3. @RequestMapping("/user/{id}")
  4. public Map get(@PathVariable int id);
  5. }

1.7 日志级别

1.7.1 在application.yml中添加

  1. logging:
  2. level:
  3. com.example: debug

1.7.2 编写logconfig类

  1. @Configuration
  2. public class LogConfig {
  3. @Bean
  4. Logger.Level feignLogLevel(){
  5. return Logger.Level.FULL;
  6. }
  7. }

这里指定的Level级别是FULL,Feign支持4种级别:

  • NONE:不记录任何日志信息,这是默认值。
  • BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
  • HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
  • FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据

1.7.3 修改Client类

  1. @FeignClient(value="user-service",fallback = ClientFallback.class,
  2. configuration = LogConfig.class)

1.8 最终的模块结构

image.png

易错点总结:

  1. 启动类的注解有以下几种
    1. @SpringBootApplication 最基本的启动类注解,所有的启动类都必须有
    2. @EnableEurekaServer 注册中心eureka使用的注解,表明这是一个注册中心
    3. @EnableEurekaClient 注册中心的注册者类注解
    4. @EnableDiscoveryClient 注册中心的9注册者类注解,最好使用它替代上一个注解
    5. @EnableFeignClients feign模块的启动类注解,其作用是扫描feign客户端和注册到注册中心(相当于集成了@EnableDiscoveryClient)