├── common-api ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── commonapi │ │ ├── CommonApiApplication.java │ │ ├── api │ │ ├── OrderApi.java │ │ └── UserApi.java │ │ └── entity │ │ ├── User.java │ │ └── Order.java └── pom.xml ├── microservice-user-2 ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── microserviceuser │ │ ├── MicroserviceUserApplication.java │ │ └── controller │ │ └── UserController.java └── pom.xml ├── service-server-1 ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── serviceserver │ │ ├── service │ │ └── UserService.java │ │ ├── controller │ │ └── DemoController.java │ │ ├── ServiceServerApplication.java │ │ └── common │ │ └── WebError.java └── pom.xml ├── service-server-2 ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── serviceserver │ │ ├── service │ │ └── UserService.java │ │ ├── controller │ │ └── DemoController.java │ │ ├── ServiceServerApplication.java │ │ └── common │ │ └── WebError.java └── pom.xml ├── microservice-user-1 ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── microserviceuser │ │ ├── MicroserviceUserApplication.java │ │ └── controller │ │ └── UserController.java └── pom.xml ├── doc_img ├── Eureka.jpg ├── list.jpg ├── zipkin.jpg ├── zuul.jpg ├── 项目结构.jpg ├── hystrix.jpg ├── zipkin_ui.jpg ├── admin_details.jpg ├── admin_wallboard.jpg └── SpringCloudConfig测试.jpg ├── admin-server ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── gala │ │ └── adminserver │ │ └── AdminServerApplication.java └── pom.xml ├── zuul-server ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── gala │ │ │ └── zuulserver │ │ │ └── ZuulServerApplication.java │ │ └── resources │ │ └── application.yml └── pom.xml ├── .gitignore ├── config-center ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── gala │ │ │ └── configcenter │ │ │ └── ConfigCenterApplication.java │ │ └── resources │ │ └── application.yml └── pom.xml ├── eureka-server-2 ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── gala │ │ │ └── eurekaserver │ │ │ └── EurekaServerApplication.java │ │ └── resources │ │ └── application.yml └── pom.xml ├── eurrka-server-1 ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── gala │ │ │ └── eurekaserver │ │ │ └── EurekaServerApplication.java │ │ └── resources │ │ └── application.yml └── pom.xml └── README.md /common-api/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /microservice-user-2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 3 | -------------------------------------------------------------------------------- /service-server-1/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8000 3 | -------------------------------------------------------------------------------- /service-server-2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8001 3 | -------------------------------------------------------------------------------- /microservice-user-1/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9000 3 | 4 | -------------------------------------------------------------------------------- /doc_img/Eureka.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/Eureka.jpg -------------------------------------------------------------------------------- /doc_img/list.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/list.jpg -------------------------------------------------------------------------------- /doc_img/zipkin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/zipkin.jpg -------------------------------------------------------------------------------- /doc_img/zuul.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/zuul.jpg -------------------------------------------------------------------------------- /doc_img/项目结构.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/项目结构.jpg -------------------------------------------------------------------------------- /doc_img/hystrix.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/hystrix.jpg -------------------------------------------------------------------------------- /doc_img/zipkin_ui.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/zipkin_ui.jpg -------------------------------------------------------------------------------- /doc_img/admin_details.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/admin_details.jpg -------------------------------------------------------------------------------- /doc_img/admin_wallboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/admin_wallboard.jpg -------------------------------------------------------------------------------- /doc_img/SpringCloudConfig测试.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GALAace/SpringCloud-Bucket/HEAD/doc_img/SpringCloudConfig测试.jpg -------------------------------------------------------------------------------- /admin-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 81 3 | 4 | spring: 5 | application: 6 | name: admin-server -------------------------------------------------------------------------------- /common-api/src/main/java/com/gala/commonapi/CommonApiApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.commonapi; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class CommonApiApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(CommonApiApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /microservice-user-1/src/main/java/com/gala/microserviceuser/MicroserviceUserApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.microserviceuser; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class MicroserviceUserApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(MicroserviceUserApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /microservice-user-2/src/main/java/com/gala/microserviceuser/MicroserviceUserApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.microserviceuser; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class MicroserviceUserApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(MicroserviceUserApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /admin-server/src/main/java/com/gala/adminserver/AdminServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.adminserver; 2 | 3 | import de.codecentric.boot.admin.server.config.EnableAdminServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | @EnableAdminServer 7 | @SpringBootApplication 8 | public class AdminServerApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(AdminServerApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /zuul-server/src/main/java/com/gala/zuulserver/ZuulServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.zuulserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 | 7 | 8 | @EnableZuulProxy 9 | @SpringBootApplication 10 | public class ZuulServerApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(ZuulServerApplication.class, args); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /config-center/src/main/java/com/gala/configcenter/ConfigCenterApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.configcenter; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.config.server.EnableConfigServer; 6 | 7 | @EnableConfigServer 8 | @SpringBootApplication 9 | public class ConfigCenterApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ConfigCenterApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /eureka-server-2/src/main/java/com/gala/eurekaserver/EurekaServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.eurekaserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @EnableEurekaServer 8 | @SpringBootApplication 9 | public class EurekaServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaServerApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /eurrka-server-1/src/main/java/com/gala/eurekaserver/EurekaServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.eurekaserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @EnableEurekaServer 8 | @SpringBootApplication 9 | public class EurekaServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaServerApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /service-server-2/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | eureka: 2 | client: 3 | service-url: 4 | defaultZone: http://euk1.com:7901/eureka/ 5 | 6 | spring: 7 | application: 8 | name: service-server 9 | cloud: 10 | config: 11 | discovery: 12 | #通过注册中心查找配置中心 13 | enabled: true 14 | #配置中心的服务id 15 | service-id: config-center 16 | #环境 17 | profile: dev 18 | #分支 19 | label: master 20 | #zipkin 21 | zipkin: 22 | base-url: http://localhost:9411/ 23 | #采样比例1 24 | sleuth: 25 | sampler: 26 | rate: 1 -------------------------------------------------------------------------------- /microservice-user-1/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | eureka: 2 | client: 3 | service-url: 4 | defaultZone: http://euk1.com:7901/eureka/ 5 | 6 | spring: 7 | application: 8 | name: microservice-user 9 | cloud: 10 | config: 11 | discovery: 12 | #通过注册中心查找配置中心 13 | enabled: true 14 | #配置中心的服务id 15 | service-id: config-center 16 | #环境 17 | profile: dev 18 | #分支 19 | label: master 20 | #zipkin 21 | zipkin: 22 | base-url: http://localhost:9411/ 23 | #采样比例1 24 | sleuth: 25 | sampler: 26 | rate: 1 -------------------------------------------------------------------------------- /microservice-user-2/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | eureka: 2 | client: 3 | service-url: 4 | defaultZone: http://euk1.com:7901/eureka/ 5 | 6 | spring: 7 | application: 8 | name: microservice-user 9 | cloud: 10 | config: 11 | discovery: 12 | #通过注册中心查找配置中心 13 | enabled: true 14 | #配置中心的服务id 15 | service-id: config-center 16 | #环境 17 | profile: dev 18 | #分支 19 | label: master 20 | #zipkin 21 | zipkin: 22 | base-url: http://localhost:9411/ 23 | #采样比例1 24 | sleuth: 25 | sampler: 26 | rate: 1 -------------------------------------------------------------------------------- /service-server-1/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | eureka: 2 | client: 3 | service-url: 4 | defaultZone: http://euk1.com:7901/eureka/ 5 | 6 | spring: 7 | application: 8 | name: service-server 9 | cloud: 10 | config: 11 | discovery: 12 | #通过注册中心查找配置中心 13 | enabled: true 14 | #配置中心的服务id 15 | service-id: config-center 16 | #环境 17 | profile: dev 18 | #分支 19 | label: master 20 | #zipkin 21 | zipkin: 22 | base-url: http://localhost:9411/ 23 | #采样比例1 24 | sleuth: 25 | sampler: 26 | rate: 1 27 | -------------------------------------------------------------------------------- /common-api/src/main/java/com/gala/commonapi/api/OrderApi.java: -------------------------------------------------------------------------------- 1 | package com.gala.commonapi.api; 2 | 3 | import com.gala.commonapi.entity.Order; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @description: 订单API 11 | * @author: GALA 12 | * @date: 2021.12.3 13 | */ 14 | public interface OrderApi { 15 | 16 | /** 17 | * 订单列表 18 | */ 19 | @GetMapping("/list") 20 | List list(); 21 | 22 | /** 23 | * 保存订单 24 | */ 25 | @PostMapping("/save") 26 | String save(Order order); 27 | } 28 | -------------------------------------------------------------------------------- /common-api/src/main/java/com/gala/commonapi/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.gala.commonapi.entity; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @description: 用户类 7 | * @author: GALA 8 | * @date: 2021.12.3 9 | */ 10 | 11 | @Data 12 | public class User { 13 | 14 | private Long id; 15 | 16 | private String account; 17 | 18 | private String username; 19 | 20 | private String password; 21 | 22 | public User(Long id, String account, String username, String password) { 23 | this.id = id; 24 | this.account = account; 25 | this.username = username; 26 | this.password = password; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /common-api/src/main/java/com/gala/commonapi/api/UserApi.java: -------------------------------------------------------------------------------- 1 | package com.gala.commonapi.api; 2 | 3 | import com.gala.commonapi.entity.User; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @description: 用户相关API 11 | * @author: GALA 12 | * @date: 2021.12.3 13 | */ 14 | public interface UserApi { 15 | 16 | /** 17 | * 用户列表 18 | */ 19 | @GetMapping("/list") 20 | List list(); 21 | 22 | /** 23 | * 保存用户 24 | * 25 | * @param user 26 | */ 27 | @PostMapping("/save") 28 | String save(User user); 29 | 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /common-api/src/main/java/com/gala/commonapi/entity/Order.java: -------------------------------------------------------------------------------- 1 | package com.gala.commonapi.entity; 2 | 3 | import lombok.Data; 4 | 5 | import java.math.BigDecimal; 6 | 7 | /** 8 | * @description: 订单 9 | * @author: GALA 10 | * @date: 2021.12.3 11 | */ 12 | 13 | @Data 14 | public class Order { 15 | 16 | private Long id; 17 | 18 | private String orderNumber; 19 | 20 | private BigDecimal totalPrice; 21 | 22 | private Long userId; 23 | 24 | public Order(Long id, String orderNumber, BigDecimal totalPrice, Long userId) { 25 | this.id = id; 26 | this.orderNumber = orderNumber; 27 | this.totalPrice = totalPrice; 28 | this.userId = userId; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /service-server-1/src/main/java/com/gala/serviceserver/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.service; 2 | 3 | import com.gala.commonapi.api.UserApi; 4 | import com.gala.serviceserver.common.WebError; 5 | import org.springframework.cloud.openfeign.FeignClient; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | 9 | /** 10 | * @description: 用户Service 11 | * @author: GALA 12 | * @date: 2021.12.6 13 | */ 14 | 15 | @Service 16 | @FeignClient(name = "microservice-user", fallbackFactory = WebError.class) 17 | public interface UserService extends UserApi { 18 | 19 | @GetMapping("/port") 20 | String port(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /service-server-2/src/main/java/com/gala/serviceserver/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.service; 2 | 3 | import com.gala.commonapi.api.UserApi; 4 | import com.gala.serviceserver.common.WebError; 5 | import org.springframework.cloud.openfeign.FeignClient; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | 9 | /** 10 | * @description: 用户Service 11 | * @author: GALA 12 | * @date: 2021.12.6 13 | */ 14 | 15 | @Service 16 | @FeignClient(name = "microservice-user", fallbackFactory = WebError.class) 17 | public interface UserService extends UserApi { 18 | 19 | @GetMapping("/port") 20 | String port(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /zuul-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | eureka: 5 | client: 6 | service-url: 7 | defaultZone: http://euk1.com:7901/eureka/ 8 | 9 | spring: 10 | application: 11 | name: zuulserver 12 | #zipkin 13 | zipkin: 14 | base-url: http://localhost:9411/ 15 | #采样比例1 16 | sleuth: 17 | sampler: 18 | rate: 1 19 | boot: 20 | admin: 21 | client: 22 | #SpringBootAdmin 地址 23 | url: http://localhost:81 24 | 25 | #日志 26 | logging: 27 | level: 28 | root: INFO 29 | org.springframework.web.servlet.DispatcherServlet: DEBUG 30 | org.springframework.cloud.sleuth: DEBUG 31 | 32 | 33 | #监控端点 34 | management: 35 | endpoints: 36 | web: 37 | exposure: 38 | include: '*' 39 | endpoint: 40 | health: 41 | show-details: always 42 | -------------------------------------------------------------------------------- /config-center/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 90 3 | 4 | spring: 5 | application: 6 | name: config-center 7 | cloud: 8 | config: 9 | server: 10 | git: 11 | uri: https://gitee.com/GALAace/spring-cloud-config-center.git #配置文件所在的Git仓库地址 12 | default-label: master #配置文件分支 13 | search-paths: configs #配置文件所在目录 14 | # username: Git账号 15 | # password: Git密码 16 | boot: 17 | admin: 18 | client: 19 | #SpringBootAdmin 地址 20 | url: http://localhost:81 21 | 22 | 23 | eureka: 24 | client: 25 | service-url: 26 | defaultZone: http://euk1.com:7901/eureka/ 27 | 28 | 29 | #监控端点 30 | management: 31 | endpoints: 32 | web: 33 | exposure: 34 | include: '*' 35 | endpoint: 36 | health: 37 | show-details: always 38 | -------------------------------------------------------------------------------- /eurrka-server-1/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7901 3 | 4 | spring: 5 | application: 6 | #应用名称 其他服务在Eureka中根据这个名字找到对应的服务 7 | name: eureka-server 8 | boot: 9 | admin: 10 | client: 11 | #SpringBootAdmin 地址 12 | url: http://localhost:81 13 | eureka: 14 | instance: 15 | #主机名 16 | hostname: euk1.com 17 | client: 18 | #是否将自己注册到Eureka Server,默认为true,表明该服务会向eureka注册自己的信息,单节点则false 19 | register-with-eureka: true 20 | #是否从Eureka server获取注册信息,由于多节点,需要同步其他节点数据,用true,单节点则false 21 | fetch-registry: true 22 | #设置服务注册中心的URL,用于client和server端交流 23 | service-url: 24 | defaultZone: http://euk2.com:7902/eureka/ 25 | 26 | #监控端点 27 | management: 28 | endpoints: 29 | web: 30 | exposure: 31 | include: '*' 32 | endpoint: 33 | health: 34 | show-details: always -------------------------------------------------------------------------------- /eureka-server-2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7902 3 | 4 | spring: 5 | application: 6 | #应用名称 其他服务在Eureka中根据这个名字找到对应的服务 7 | name: eureka-server 8 | boot: 9 | admin: 10 | client: 11 | #SpringBootAdmin 地址 12 | url: http://localhost:81 13 | 14 | eureka: 15 | instance: 16 | #主机名 17 | hostname: euk2.com 18 | client: 19 | #是否将自己注册到Eureka Server,默认为true,表明该服务会向eureka注册自己的信息,单节点则false 20 | register-with-eureka: true 21 | #是否从Eureka server获取注册信息,由于多节点,需要同步其他节点数据,用true,单节点则false 22 | fetch-registry: true 23 | #设置服务注册中心的URL,用于client和server端交流 24 | service-url: 25 | defaultZone: http://euk1.com:7901/eureka/ 26 | 27 | #监控端点 28 | management: 29 | endpoints: 30 | web: 31 | exposure: 32 | include: '*' 33 | endpoint: 34 | health: 35 | show-details: always -------------------------------------------------------------------------------- /service-server-1/src/main/java/com/gala/serviceserver/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.controller; 2 | 3 | import com.gala.commonapi.entity.User; 4 | import com.gala.serviceserver.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.context.config.annotation.RefreshScope; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @description: Demo Controller 14 | * @author: GALA 15 | * @date: 2021.12.3 16 | */ 17 | 18 | 19 | @RefreshScope 20 | @RestController 21 | public class DemoController { 22 | 23 | @Autowired 24 | UserService userService; 25 | 26 | @GetMapping("/list") 27 | public List list() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/port") 32 | public String port() { 33 | return userService.port(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /service-server-2/src/main/java/com/gala/serviceserver/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.controller; 2 | 3 | import com.gala.commonapi.entity.User; 4 | import com.gala.serviceserver.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.context.config.annotation.RefreshScope; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @description: Demo Controller 14 | * @author: GALA 15 | * @date: 2021.12.3 16 | */ 17 | 18 | 19 | @RefreshScope 20 | @RestController 21 | public class DemoController { 22 | 23 | @Autowired 24 | UserService userService; 25 | 26 | @GetMapping("/list") 27 | public List list() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/port") 32 | public String port() { 33 | return userService.port(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /service-server-1/src/main/java/com/gala/serviceserver/ServiceServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver; 2 | 3 | import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 7 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 8 | import org.springframework.cloud.openfeign.EnableFeignClients; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | @EnableHystrixDashboard 12 | @EnableFeignClients 13 | @SpringBootApplication 14 | public class ServiceServerApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(ServiceServerApplication.class, args); 18 | } 19 | 20 | //解决/actuator/hystrix.stream无法访问问题 21 | @Bean 22 | public ServletRegistrationBean hystrixMetricsStreamServlet() { 23 | ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); 24 | registration.addUrlMappings("/actuator/hystrix.stream"); 25 | return registration; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /service-server-2/src/main/java/com/gala/serviceserver/ServiceServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver; 2 | 3 | import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 7 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 8 | import org.springframework.cloud.openfeign.EnableFeignClients; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | @EnableHystrixDashboard 12 | @EnableFeignClients 13 | @SpringBootApplication 14 | public class ServiceServerApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(ServiceServerApplication.class, args); 18 | } 19 | 20 | //解决/actuator/hystrix.stream无法访问问题 21 | @Bean 22 | public ServletRegistrationBean hystrixMetricsStreamServlet() { 23 | ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); 24 | registration.addUrlMappings("/actuator/hystrix.stream"); 25 | return registration; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /microservice-user-2/src/main/java/com/gala/microserviceuser/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.gala.microserviceuser.controller; 2 | 3 | import com.gala.commonapi.api.UserApi; 4 | import com.gala.commonapi.entity.User; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @description: 14 | * @author: GALA 15 | * @date: 2021.12.3 16 | */ 17 | 18 | @RestController 19 | public class UserController implements UserApi { 20 | 21 | @Value("${server.port}") 22 | String port; 23 | 24 | @Override 25 | public List list() { 26 | //实现自己的业务 27 | User user1 = new User(Long.valueOf(10001), "aaa", "张三", "1234"); 28 | User user2 = new User(Long.valueOf(10002), "bbb", "李四", "1234"); 29 | User user3 = new User(Long.valueOf(10003), "ccc", "王五", "1234"); 30 | List userList = new ArrayList<>(); 31 | userList.add(user1); 32 | userList.add(user2); 33 | userList.add(user3); 34 | System.out.println(port); 35 | return userList; 36 | } 37 | 38 | @Override 39 | public String save(User user) { 40 | //实现自己的业务 41 | return "ok"; 42 | } 43 | 44 | /** 45 | * 服务端口号 46 | */ 47 | @GetMapping("/port") 48 | public String port() { 49 | return "调用端口:" + port; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /service-server-1/src/main/java/com/gala/serviceserver/common/WebError.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.common; 2 | 3 | import com.gala.commonapi.entity.User; 4 | import com.gala.serviceserver.service.UserService; 5 | import com.netflix.hystrix.exception.HystrixTimeoutException; 6 | import feign.hystrix.FallbackFactory; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @description: 使用fallbackFactory检查具体错误 13 | * @author: GALA 14 | * @date: 2021.12.6 15 | */ 16 | 17 | @Component 18 | public class WebError implements FallbackFactory { 19 | @Override 20 | public UserService create(Throwable throwable) { 21 | 22 | return new UserService() { 23 | @Override 24 | public String port() { 25 | System.out.println(throwable); 26 | if (throwable instanceof HystrixTimeoutException) { 27 | System.out.println("InternalServerError"); 28 | return "远程服务报错"; 29 | } else if (throwable instanceof RuntimeException) { 30 | return "请求时异常:" + throwable; 31 | } else { 32 | return null; 33 | } 34 | } 35 | 36 | @Override 37 | public List list() { 38 | return null; 39 | } 40 | 41 | @Override 42 | public String save(User user) { 43 | return null; 44 | } 45 | }; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /service-server-2/src/main/java/com/gala/serviceserver/common/WebError.java: -------------------------------------------------------------------------------- 1 | package com.gala.serviceserver.common; 2 | 3 | import com.gala.commonapi.entity.User; 4 | import com.gala.serviceserver.service.UserService; 5 | import com.netflix.hystrix.exception.HystrixTimeoutException; 6 | import feign.hystrix.FallbackFactory; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @description: 使用fallbackFactory检查具体错误 13 | * @author: GALA 14 | * @date: 2021.12.6 15 | */ 16 | 17 | @Component 18 | public class WebError implements FallbackFactory { 19 | @Override 20 | public UserService create(Throwable throwable) { 21 | 22 | return new UserService() { 23 | @Override 24 | public String port() { 25 | System.out.println(throwable); 26 | if (throwable instanceof HystrixTimeoutException) { 27 | System.out.println("InternalServerError"); 28 | return "远程服务报错"; 29 | } else if (throwable instanceof RuntimeException) { 30 | return "请求时异常:" + throwable; 31 | } else { 32 | return null; 33 | } 34 | } 35 | 36 | @Override 37 | public List list() { 38 | return null; 39 | } 40 | 41 | @Override 42 | public String save(User user) { 43 | return null; 44 | } 45 | }; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /microservice-user-1/src/main/java/com/gala/microserviceuser/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.gala.microserviceuser.controller; 2 | 3 | import com.gala.commonapi.api.UserApi; 4 | import com.gala.commonapi.entity.User; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @description: 14 | * @author: GALA 15 | * @date: 2021.12.3 16 | */ 17 | 18 | @RestController 19 | public class UserController implements UserApi { 20 | 21 | @Value("${server.port}") 22 | String port; 23 | 24 | @Override 25 | public List list() { 26 | //实现自己的业务 27 | User user1 = new User(Long.valueOf(10001), "aaa", "张三", "1234"); 28 | User user2 = new User(Long.valueOf(10002), "bbb", "李四", "1234"); 29 | User user3 = new User(Long.valueOf(10003), "ccc", "王五", "1234"); 30 | List userList = new ArrayList<>(); 31 | userList.add(user1); 32 | userList.add(user2); 33 | userList.add(user3); 34 | System.out.println(port); 35 | return userList; 36 | } 37 | 38 | @Override 39 | public String save(User user) { 40 | //实现自己的业务 41 | return "ok"; 42 | } 43 | 44 | /** 45 | * 返回服务端口号 46 | */ 47 | @GetMapping("/port") 48 | public String port() { 49 | try { 50 | System.out.println("调用" + port + "端口,进入sleep"); 51 | Thread.sleep(6000); 52 | } catch (InterruptedException e) { 53 | e.printStackTrace(); 54 | } 55 | return "调用端口:" + port; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /common-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | common-api 13 | 0.0.1-SNAPSHOT 14 | common-api 15 | common-api 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.projectlombok 28 | lombok 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-compiler-plugin 38 | 39 | 1.8 40 | 1.8 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /admin-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.7 9 | 10 | 11 | com.gala 12 | admin-server 13 | 0.0.1-SNAPSHOT 14 | admin-server 15 | admin-server 16 | 17 | 1.8 18 | 2.4.3 19 | 20 | 21 | 22 | 23 | de.codecentric 24 | spring-boot-admin-starter-server 25 | 26 | 27 | 28 | de.codecentric 29 | spring-boot-admin-server-ui 30 | 31 | 32 | 33 | 34 | 35 | 36 | de.codecentric 37 | spring-boot-admin-dependencies 38 | ${spring-boot-admin.version} 39 | pom 40 | import 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /eureka-server-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | eureka-server-2 13 | 0.0.1-SNAPSHOT 14 | eureka-server-2 15 | eureka-server 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-server 25 | 26 | 27 | 28 | de.codecentric 29 | spring-boot-admin-starter-client 30 | 2.2.1 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-actuator 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | ${spring-cloud.version} 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | spring-milestones 62 | Spring Milestones 63 | https://repo.spring.io/milestone 64 | 65 | false 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /eurrka-server-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | eureka-server-1 13 | 0.0.1-SNAPSHOT 14 | eureka-server-1 15 | eureka-server 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-server 25 | 26 | 27 | 28 | 29 | de.codecentric 30 | spring-boot-admin-starter-client 31 | 2.2.1 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-actuator 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | ${spring-cloud.version} 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | spring-milestones 62 | Spring Milestones 63 | https://repo.spring.io/milestone 64 | 65 | false 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /config-center/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | config-center 13 | 0.0.1-SNAPSHOT 14 | config-center 15 | config-center 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-client 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-config-server 30 | 31 | 32 | 33 | de.codecentric 34 | spring-boot-admin-starter-client 35 | 2.2.1 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-actuator 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.cloud 47 | spring-cloud-dependencies 48 | ${spring-cloud.version} 49 | pom 50 | import 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | spring-milestones 66 | Spring Milestones 67 | https://repo.spring.io/milestone 68 | 69 | false 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /zuul-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | zuul-server 13 | 0.0.1-SNAPSHOT 14 | zuul-server 15 | zuul-server 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-client 25 | 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-netflix-zuul 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-sleuth 37 | 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-starter-zipkin 43 | 44 | 45 | 46 | 47 | de.codecentric 48 | spring-boot-admin-starter-client 49 | 2.2.1 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-actuator 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.cloud 62 | spring-cloud-dependencies 63 | ${spring-cloud.version} 64 | pom 65 | import 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-maven-plugin 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /microservice-user-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | microservice-user 13 | 0.0.1-SNAPSHOT 14 | microservice-user 15 | microservice-user 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-client 25 | 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-config-client 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-sleuth 37 | 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-starter-zipkin 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-actuator 49 | 50 | 51 | 52 | 53 | de.codecentric 54 | spring-boot-admin-starter-client 55 | 2.2.1 56 | 57 | 58 | 59 | 60 | com.gala 61 | common-api 62 | 0.0.1-SNAPSHOT 63 | 64 | 65 | 66 | 67 | org.projectlombok 68 | lombok 69 | true 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.springframework.cloud 78 | spring-cloud-dependencies 79 | ${spring-cloud.version} 80 | pom 81 | import 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.springframework.boot 91 | spring-boot-maven-plugin 92 | 93 | 94 | 95 | org.projectlombok 96 | lombok 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /microservice-user-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | microservice-user 13 | 0.0.1-SNAPSHOT 14 | microservice-user 15 | microservice-user 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-netflix-eureka-client 25 | 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-config-client 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-sleuth 37 | 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-starter-zipkin 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-actuator 49 | 50 | 51 | 52 | 53 | de.codecentric 54 | spring-boot-admin-starter-client 55 | 2.2.1 56 | 57 | 58 | 59 | 60 | com.gala 61 | common-api 62 | 0.0.1-SNAPSHOT 63 | 64 | 65 | 66 | 67 | org.projectlombok 68 | lombok 69 | true 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.springframework.cloud 78 | spring-cloud-dependencies 79 | ${spring-cloud.version} 80 | pom 81 | import 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.springframework.boot 91 | spring-boot-maven-plugin 92 | 93 | 94 | 95 | org.projectlombok 96 | lombok 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /service-server-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | service-server-1 13 | 0.0.1-SNAPSHOT 14 | service-server-1 15 | service-server 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter 24 | 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-openfeign 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-config-client 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-actuator 48 | 49 | 50 | 51 | 52 | de.codecentric 53 | spring-boot-admin-starter-client 54 | 2.2.1 55 | 56 | 57 | 58 | 59 | org.springframework.cloud 60 | 61 | spring-cloud-starter-netflix-hystrix 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.cloud 68 | 69 | spring-cloud-starter-netflix-hystrix-dashboard 70 | 71 | 72 | 73 | 74 | 75 | org.springframework.cloud 76 | spring-cloud-starter-sleuth 77 | 78 | 79 | 80 | 81 | org.springframework.cloud 82 | spring-cloud-starter-zipkin 83 | 84 | 85 | 86 | 87 | com.gala 88 | common-api 89 | 0.0.1-SNAPSHOT 90 | 91 | 92 | 93 | 94 | org.projectlombok 95 | lombok 96 | true 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | org.springframework.cloud 105 | spring-cloud-dependencies 106 | ${spring-cloud.version} 107 | pom 108 | import 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | org.springframework.boot 117 | spring-boot-maven-plugin 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /service-server-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.gala 12 | service-server-1 13 | 0.0.1-SNAPSHOT 14 | service-server-1 15 | service-server 16 | 17 | 1.8 18 | Hoxton.SR3 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter 24 | 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-openfeign 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-config-client 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-actuator 48 | 49 | 50 | 51 | 52 | de.codecentric 53 | spring-boot-admin-starter-client 54 | 2.2.1 55 | 56 | 57 | 58 | 59 | org.springframework.cloud 60 | 61 | spring-cloud-starter-netflix-hystrix 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.cloud 68 | 69 | spring-cloud-starter-netflix-hystrix-dashboard 70 | 71 | 72 | 73 | 74 | 75 | org.springframework.cloud 76 | spring-cloud-starter-sleuth 77 | 78 | 79 | 80 | 81 | org.springframework.cloud 82 | spring-cloud-starter-zipkin 83 | 84 | 85 | 86 | 87 | com.gala 88 | common-api 89 | 0.0.1-SNAPSHOT 90 | 91 | 92 | 93 | 94 | org.projectlombok 95 | lombok 96 | true 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | org.springframework.cloud 105 | spring-cloud-dependencies 106 | ${spring-cloud.version} 107 | pom 108 | import 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | org.springframework.boot 117 | spring-boot-maven-plugin 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpringCloud技术栈全家桶Demo 2 | 3 | ## 项目介绍 4 | 5 | ### 包含组件: 6 | 7 | - 注册中心:Eureka 8 | - 远程调用:Feign 9 | - 负载均衡:Ribbon 10 | - 断路器:Hystrix 11 | - 网关:Zuul 12 | - 配置中心:SpringCloud Config 13 | - 链路追踪:Sleuth、Zipkin 14 | - 健康监控:SpringBootAdmin 15 | 16 | ### 项目结构: 17 | 18 | 项目结构 19 | 20 | 21 | 22 | ## 运行 23 | 24 | //todo 25 | 26 | 27 | 28 | ## 详细搭建步骤 29 | 30 | ### 注册中心(Eureka) 31 | 32 | 0.因为是在一台pc上做集群,所以在开始前需要修改一下hosts文件 33 | 34 | ``` 35 | #Eureka 36 | 127.0.0.1 euk1.com 37 | 127.0.0.1 euk2.com 38 | ``` 39 | 40 | 1.使用Spring Initializr创建一个SpringBoot工程,引入eureka-server依赖 41 | 42 | ```xml 43 | 44 | org.springframework.cloud 45 | spring-cloud-starter-netflix-eureka-server 46 | 47 | ``` 48 | 49 | 2.修改application.yml 50 | 51 | ```yaml 52 | server: 53 | port: 7901 54 | 55 | spring: 56 | application: 57 | #应用名称 其他服务在Eureka中根据这个名字找到对应的服务 58 | name: eureka-server 59 | 60 | eureka: 61 | instance: 62 | #主机名 63 | hostname: euk1.com 64 | client: 65 | #是否将自己注册到Eureka Server,默认为true,表明该服务会向eureka注册自己的信息,单节点则false 66 | register-with-eureka: true 67 | #是否从Eureka server获取注册信息,由于多节点,需要同步其他节点数据,用true,单节点则false 68 | fetch-registry: true 69 | #设置服务注册中心的URL,用于client和server端交流 70 | service-url: 71 | defaultZone: http://euk2.com:7902/eureka/ 72 | ``` 73 | 74 | 3.启动类EurekaServerApplication增加注解 75 | 76 | ```java 77 | @EnableEurekaServer 78 | ``` 79 | 80 | 4.创建EurekaServer2,步骤同上。修改EurekaServer2的application.yml 81 | 82 | ```yml 83 | server: 84 | port: 7902 85 | 86 | spring: 87 | application: 88 | #应用名称 其他服务在Eureka中根据这个名字找到对应的服务 89 | name: eureka-server 90 | 91 | eureka: 92 | instance: 93 | #主机名 94 | hostname: euk2.com 95 | client: 96 | #是否将自己注册到Eureka Server,默认为true,表明该服务会向eureka注册自己的信息,单节点则false 97 | register-with-eureka: true 98 | #是否从Eureka server获取注册信息,由于多节点,需要同步其他节点数据,用true,单节点则false 99 | fetch-registry: true 100 | #设置服务注册中心的URL,用于client和server端交流 101 | service-url: 102 | defaultZone: http://euk1.com:7901/eureka/ 103 | ``` 104 | 105 | 5.分别启动两个工程,访问 http://localhost:7901/或http://localhost:7902/得到下图则配置成功 106 | 107 | ![Eureka](./doc_img/Eureka.jpg) 108 | 109 | ### 配置中心(SpringCloud Config) 110 | 111 | 在单体应用,配置写在配置文件中,没有什么大问题。如果要切换环境 可以切换不同的profile,但在微服务中: 112 | 113 | 1. 微服务比较多。成百上千,配置很多,需要集中管理; 114 | 2. 管理不同环境的配置; 115 | 3. 需要动态调整配置参数,更改配置不停服。 116 | 117 | 1.这里新建一个仓库[SpringCloud-ConfigCenter](https://gitee.com/GALAace/spring-cloud-config-center)存放配置文件,文件命名规则为 118 | 119 | > ``` 120 | > /{name}-{profiles}.properties 121 | > /{name}-{profiles}.yml 122 | > /{name}-{profiles}.json 123 | > /{label}/{name}-{profiles}.yml 124 | > ``` 125 | > 126 | > name 服务名称 127 | > 128 | > profile 环境名称,开发、测试、生产:dev qa prd 129 | > 130 | > lable 仓库分支、默认master分支 131 | 132 | 2.使用Spring Initializr创建一个SpringBoot工程,引入config-server依赖 133 | 134 | ```xml 135 | 136 | org.springframework.cloud 137 | spring-cloud-config-server 138 | 139 | ``` 140 | 141 | 3.修改application.yml 142 | 143 | ```yaml 144 | server: 145 | port: 90 146 | 147 | spring: 148 | application: 149 | name: config-center 150 | cloud: 151 | config: 152 | server: 153 | git: 154 | uri: https://gitee.com/GALAace/spring-cloud-config-center.git #配置文件所在的Git仓库地址 155 | default-label: master #配置文件分支 156 | search-paths: configs #配置文件所在目录 157 | # username: Git账号 158 | # password: Git密码 159 | 160 | eureka: 161 | client: 162 | service-url: 163 | defaultZone: http://euk1.com:7901/eureka/ 164 | 165 | ``` 166 | 167 | 4.启动类ConfigCenterApplication增加注解 168 | 169 | ```java 170 | @EnableConfigServer 171 | ``` 172 | 173 | 5.启动工程,访问http://localhost:90/testconfig-dev.yml 可以得到我们在配置文件仓库中的配置 174 | 175 | ![SpringCloudConfig测试](./doc_img/SpringCloudConfig测试.jpg) 176 | 177 | 178 | 179 | ### 服务 180 | 181 | 服务分为业务服务(service-server)下面称Consumer,和具体执行功能的服务称Provider(microservice-xxx); 182 | 183 | 由Consumer作为客户端调用作为服务端的Provider,他们都需要注册在Eureka上; 184 | 185 | 使用声明式服务调用,Provider方提供公用API包,Feign通过SpringMVC的注解来加载URI 186 | 187 | 所有我们先创建一个项目作为API 188 | 189 | #### Api 190 | 191 | 1.使用Spring Initializr创建一个SpringBoot工程,引入依赖 192 | 193 | ```xml 194 | 195 | 196 | org.springframework.boot 197 | spring-boot-starter-web 198 | 199 | 200 | 201 | org.projectlombok 202 | lombok 203 | 204 | ``` 205 | 206 | 注意:SpringBoot默认打build配置可能引起打包后其他项目无法依赖的问题,需要修改pom.xml 207 | 208 | ```xml 209 | 210 | 211 | 212 | org.apache.maven.plugins 213 | maven-compiler-plugin 214 | 215 | 1.8 216 | 1.8 217 | 218 | 219 | 220 | 221 | ``` 222 | 223 | 2.创建实体和API interface,具体代码参考[common-api](https://github.com/GALAace/SpringCloud-Bucket/tree/main/common-api) 224 | 225 | 3.使用maven install打包到本地仓库,待Consumer和Provider使用 226 | 227 | #### Provider 228 | 229 | 这里举例的4个Provider结构都是类似的,这里以microservice-user-1为例 230 | 231 | 1.使用Spring Initializr创建一个SpringBoot工程,引入依赖 232 | 233 | ```xml 234 | 235 | 236 | org.springframework.cloud 237 | spring-cloud-starter-netflix-eureka-client 238 | 239 | 240 | 241 | 242 | org.springframework.cloud 243 | spring-cloud-config-client 244 | 245 | 246 | 247 | 248 | org.springframework.cloud 249 | spring-cloud-starter-sleuth 250 | 251 | 252 | 253 | 254 | org.springframework.cloud 255 | spring-cloud-starter-zipkin 256 | 257 | 258 | 259 | 260 | org.springframework.boot 261 | spring-boot-starter-actuator 262 | 263 | 264 | 265 | 266 | de.codecentric 267 | spring-boot-admin-starter-client 268 | 2.2.1 269 | 270 | 271 | 272 | 273 | com.gala 274 | common-api 275 | 0.0.1-SNAPSHOT 276 | 277 | 278 | 279 | 280 | org.projectlombok 281 | lombok 282 | true 283 | 284 | ``` 285 | 286 | 2.修改application.yml 287 | 288 | ```java 289 | server: 290 | port: 9000 291 | ``` 292 | 293 | 3.新建一个bootstrap.yml 294 | 295 | bootstrapd加载的优先级比application高,我们在这里配置注册中心和配置中心的信息 296 | 297 | ```yaml 298 | eureka: 299 | client: 300 | service-url: 301 | defaultZone: http://euk1.com:7901/eureka/ 302 | 303 | spring: 304 | application: 305 | name: microservice-user 306 | cloud: 307 | config: 308 | discovery: 309 | #通过注册中心查找配置中心 310 | enabled: true 311 | #配置中心的服务id 312 | service-id: config-center 313 | #环境 314 | profile: dev 315 | #分支 316 | label: master 317 | ``` 318 | 319 | 2.新建一个UserController,实现引入的自定义依赖common-api的UserApi接口 320 | 321 | ```java 322 | @RestController 323 | public class UserController implements UserApi { 324 | @Override 325 | public List list() { 326 | //实现自己的业务 327 | return null; 328 | } 329 | 330 | @Override 331 | public String save(User user) { 332 | //实现自己的业务 333 | return null; 334 | } 335 | } 336 | ``` 337 | 338 | #### Consumer 339 | 340 | 1.使用Spring Initializr创建一个SpringBoot工程,引入依赖 341 | 342 | ```xml 343 | 344 | 345 | org.springframework.cloud 346 | spring-cloud-starter-netflix-eureka-client 347 | 348 | 349 | 350 | 351 | org.springframework.cloud 352 | spring-cloud-starter-openfeign 353 | 354 | 355 | 356 | 357 | org.springframework.cloud 358 | spring-cloud-config-client 359 | 360 | 361 | 362 | 363 | org.springframework.boot 364 | spring-boot-starter-actuator 365 | 366 | 367 | 368 | 369 | de.codecentric 370 | spring-boot-admin-starter-client 371 | 2.2.1 372 | 373 | 374 | 375 | 376 | org.springframework.cloud 377 | 378 | spring-cloud-starter-netflix-hystrix 379 | 380 | 381 | 382 | 383 | 384 | org.springframework.cloud 385 | 386 | spring-cloud-starter-netflix-hystrix-dashboard 387 | 388 | 389 | 390 | 391 | 392 | com.gala 393 | common-api 394 | 0.0.1-SNAPSHOT 395 | 396 | 397 | 398 | 399 | org.projectlombok 400 | lombok 401 | true 402 | 403 | ``` 404 | 405 | ##### 远程调用(Feign) 406 | 407 | 1.启动类ConfigCenterApplication增加注解 408 | 409 | ```java 410 | @EnableFeignClients 411 | ``` 412 | 413 | 2.创建接口,增加注解@FeignClient(name = "{微服务id}") 414 | 415 | ```java 416 | @Service 417 | @FeignClient(name = "microservice-user") 418 | public interface UserService extends UserApi { 419 | 420 | } 421 | ``` 422 | 423 | 3.创建Controller,引入UserService 424 | 425 | ```java 426 | @RestController 427 | public class DemoController { 428 | 429 | @Autowired 430 | UserService userService; 431 | 432 | @GetMapping("/list") 433 | public List list(){ 434 | return userService.list(); 435 | } 436 | 437 | } 438 | ``` 439 | 440 | 4.访问http://localhost:8000/list 可以看到Provider端写的测试数据 441 | 442 | ![list](./doc_img/list.jpg) 443 | 444 | ##### Ribbon 445 | 446 | ###### 负载均衡 447 | 448 | 默认的负载均衡策略是ZoneAvoidanceRule(区域权衡策略):复合判断Server所在区域的性能和Server的可用性,轮询选择服务器。 449 | 450 | 除此以外还有: 451 | 452 | BestAvailableRule(最低并发策略):会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务。逐个找服务,如果断路器打开,则忽略。 453 | 454 | RoundRobinRule(轮询策略):以简单轮询选择一个服务器。按顺序循环选择一个server。 455 | 456 | RandomRule(随机策略):随机选择一个服务器。 457 | 458 | AvailabilityFilteringRule(可用过滤策略):会先过滤掉多次访问故障而处于断路器跳闸状态的服务和过滤并发的连接数量超过阀值得服务,然后对剩余的服务列表安装轮询策略进行访问。 459 | 460 | WeightedResponseTimeRule(响应时间加权策略):据平均响应时间计算所有的服务的权重,响应时间越快服务权重越大,容易被选中的概率就越高。刚启动时,如果统计信息不中,则使用RoundRobinRule(轮询)策略,等统计的信息足够了会自动的切换到WeightedResponseTimeRule。响应时间长,权重低,被选择的概率低。反之,同样道理。此策略综合了各种因素(网络,磁盘,IO等),这些因素直接影响响应时间。 461 | 462 | RetryRule(重试策略):先按照RoundRobinRule(轮询)的策略获取服务,如果获取的服务失败则在指定的时间会进行重试,进行获取可用的服务。如多次获取某个服务失败,就不会再次获取该服务。主要是在一个时间段内,如果选择一个服务不成功,就继续找可用的服务,直到超时。 463 | 464 | 切换负载均衡策 465 | 466 | 注解方式 467 | 468 | ```java 469 | @Bean 470 | public IRule myRule(){ 471 | //return new RoundRobinRule(); 472 | //return new RandomRule(); 473 | return new RetryRule(); 474 | } 475 | ``` 476 | 477 | 配置文件方式(优先级高于注解) 478 | 479 | ```yaml 480 | microservice-user: #微服务id 481 | ribbon: 482 | NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #随机策略 483 | ``` 484 | 485 | 验证 486 | 487 | 1.在microservice-user-1和microservice-user-2的UserController中增加一个方法,返回当前服务的端口号 488 | 489 | ```java 490 | @Value("${server.port}") 491 | String port; 492 | 493 | /** 494 | * 返回服务端口号 495 | */ 496 | @GetMapping("/port") 497 | public String port() { 498 | return "调用端口:" + port; 499 | } 500 | ``` 501 | 502 | 2.在service-server的UserService中增加方法 503 | 504 | ```java 505 | @GetMapping("/port") 506 | String port(); 507 | ``` 508 | 509 | 3.在service-server的DemoController中增加方法 510 | 511 | ```java 512 | @GetMapping("/port") 513 | public String port(){ 514 | return userService.port(); 515 | } 516 | ``` 517 | 518 | 4.多次访问http://localhost:8000/port查看响应结果 519 | 520 | ###### 超时重试 521 | 522 | Feign默认支持Ribbon;Ribbon的重试机制和Feign的重试机制有冲突,所以源码中默认关闭Feign的重试机制,使用Ribbon的重试机制 523 | 524 | 1.修改service-server的配置文件 525 | 526 | ```yaml 527 | #ribbon 528 | microservice-user: #微服务id 529 | ribbon: 530 | # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #随机策略 531 | #连接超时时间(ms) 532 | ConnectTimeout: 1000 533 | #业务逻辑超时时间(ms) 534 | ReadTimeout: 5000 535 | #同一台实例最大重试次数,不包括首次调用 536 | MaxAutoRetries: 1 537 | #重试负载均衡其他的实例最大重试次数,不包括首次调用 538 | MaxAutoRetriesNextServer: 1 539 | #是否所有操作都重试 540 | OkToRetryOnAllOperations: false 541 | ``` 542 | 543 | 2.修改microservice-user-1的UserController,microservice-user-2不变用于验证 544 | 545 | ```java 546 | /** 547 | * 返回服务端口号 548 | */ 549 | @GetMapping("/port") 550 | public String port() { 551 | try { 552 | System.out.println("调用" + port + "端口,进入sleep"); 553 | Thread.sleep(6000); 554 | } catch (InterruptedException e) { 555 | e.printStackTrace(); 556 | } 557 | return "调用端口:" + port; 558 | } 559 | ``` 560 | 561 | 3.访问http://localhost:8000/port,当访问到microservice-user-1时,也就是9000端口服务时,在等待6秒后会重试一次, 562 | 563 | 可以看到microservice-user-1的控制台会打印2次“调用9000端口,进入sleep”,但此时9000服务依然超时,ribbon会去调用9001然后页面响应“调用端口:9001” 564 | 565 | ##### 断路器(Hystrix) 566 | 567 | 1.修改配置文件,启用hystrix 568 | 569 | ```yaml 570 | #feign 571 | feign: 572 | hystrix: 573 | enabled: true 574 | ``` 575 | 576 | 2.创建一个WebError类,实现FallbackFactory,对每个接口编写对应的处理方法 577 | 578 | ```java 579 | @Component 580 | public class WebError implements FallbackFactory { 581 | @Override 582 | public UserService create(Throwable throwable) { 583 | 584 | return new UserService() { 585 | @Override 586 | public String port() { 587 | System.out.println(throwable); 588 | if (throwable instanceof HystrixTimeoutException) { 589 | System.out.println("InternalServerError"); 590 | return "远程服务报错"; 591 | } else if (throwable instanceof RuntimeException) { 592 | return "请求时异常:" + throwable; 593 | } else { 594 | return null; 595 | } 596 | } 597 | 598 | @Override 599 | public List list() { 600 | return null; 601 | } 602 | 603 | @Override 604 | public String save(User user) { 605 | return null; 606 | } 607 | }; 608 | } 609 | } 610 | ``` 611 | 612 | 3.修改UserService的注解 613 | 614 | ```java 615 | @FeignClient(name = "microservice-user",fallbackFactory = WebError.class) 616 | ``` 617 | 618 | 4.启动类ServiceServerApplication增加注解 619 | 620 | ```java 621 | @EnableHystrixDashboard 622 | ``` 623 | 624 | 5.访问http://localhost:8000/actuator/hystrix.stream查看监控端点,如果报错则在启动类增加一个Bean 625 | 626 | ```java 627 | //解决/actuator/hystrix.stream无法访问问题 628 | @Bean 629 | public ServletRegistrationBean hystrixMetricsStreamServlet() { 630 | ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); 631 | registration.addUrlMappings("/actuator/hystrix.stream"); 632 | return registration; 633 | } 634 | ``` 635 | 636 | 6.访问http://localhost:8000/hystrix查看图形化页面 637 | 638 | ![hystrix](./doc_img/hystrix.jpg) 639 | 640 | #### 网关(Zuul) 641 | 642 | zuul默认集成了:ribbon和hystrix 643 | 644 | 1.使用Spring Initializr创建一个SpringBoot工程,引入依赖 645 | 646 | ```xml 647 | 648 | 649 | org.springframework.cloud 650 | spring-cloud-starter-netflix-eureka-client 651 | 652 | 653 | 654 | 655 | org.springframework.cloud 656 | spring-cloud-starter-netflix-zuul 657 | 658 | ``` 659 | 660 | 2.修改配置文件 661 | 662 | ```yaml 663 | server: 664 | port: 80 665 | 666 | eureka: 667 | client: 668 | service-url: 669 | defaultZone: http://euk1.com:7901/eureka/ 670 | 671 | spring: 672 | application: 673 | name: zuulserver 674 | ``` 675 | 676 | 3.启动类增加注解 677 | 678 | ```java 679 | @EnableZuulProxy 680 | ``` 681 | 682 | 4.访问 http://{ip}:{端口}/{服务id}/{接口uri} 如 http://localhost/service-server/list 683 | 684 | ![zuul](./doc_img/zuul.jpg) 685 | 686 | #### 链路追踪(Sleuth、Zipkin) 687 | 688 | 如果能跟踪每个请求,中间请求经过哪些微服务,请求耗时,网络延迟,业务逻辑耗时等。我们就能更好地分析系统瓶颈、解决系统问题。因此链路跟踪很重要 689 | 690 | ##### Sleuth 691 | 692 | Sleuth是Spring cloud的分布式跟踪解决方案。 693 | 694 | 1. span(跨度),基本工作单元。一次链路调用,创建一个span, 695 | 696 | span用一个64位id唯一标识。包括:id,描述,时间戳事件,spanId,span父id。 697 | 698 | span被启动和停止时,记录了时间信息,初始化span叫:root span,它的span id和trace id相等。 699 | 700 | 2. trace(跟踪),一组共享“root span”的span组成的树状结构 称为 trace,trace也有一个64位ID,trace中所有span共享一个trace id。类似于一颗 span 树。 701 | 702 | 3. annotation(标签),annotation用来记录事件的存在,其中,核心annotation用来定义请求的开始和结束。 703 | 704 | - CS(Client Send客户端发起请求)。客户端发起请求描述了span开始。 705 | - SR(Server Received服务端接到请求)。服务端获得请求并准备处理它。SR-CS=网络延迟。 706 | - SS(Server Send服务器端处理完成,并将结果发送给客户端)。表示服务器完成请求处理,响应客户端时。SS-SR=服务器处理请求的时间。 707 | - CR(Client Received 客户端接受服务端信息)。span结束的标识。客户端接收到服务器的响应。CR-CS=客户端发出请求到服务器响应的总时间。 708 | 709 | 其实数据结构是一颗树,从root span 开始。 710 | 711 | 使用: 712 | 713 | 1.每个需要监控的系统都需要引入依赖 714 | 715 | ```xml 716 | 717 | 718 | org.springframework.cloud 719 | spring-cloud-starter-sleuth 720 | 721 | ``` 722 | 723 | 2.在zuul-server、service-server,microservice里都加入依赖,修改一下日志配置 724 | 725 | ```yaml 726 | logging: 727 | level: 728 | root: INFO 729 | org.springframework.web.servlet.DispatcherServlet: DEBUG 730 | org.springframework.cloud.sleuth: DEBUG 731 | ``` 732 | 733 | 3.访问一次http://localhost/service-server/list接口,查看日志 734 | 735 | zuul-server: 736 | 737 | ```java 738 | 2021-12-07 11:53:11.519 DEBUG [zuulserver,6228db7f8ae94ab0,6228db7f8ae94ab0,false] 14644 --- [p-nio-80-exec-4] o.s.web.servlet.DispatcherServlet : GET "/service-server/list", parameters={} 739 | 2021-12-07 11:53:12.023 DEBUG [zuulserver,6228db7f8ae94ab0,6228db7f8ae94ab0,false] 14644 --- [p-nio-80-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK 740 | ``` 741 | 742 | service-server: 743 | 744 | ```java 745 | 2021-12-07 11:53:11.867 DEBUG [service-server,6228db7f8ae94ab0,9b859b488d9eff12,false] 17912 --- [oservice-user-1] o.s.c.s.i.async.SleuthContextListener : Context refreshed or closed [org.springframework.context.event.ContextRefreshedEvent[source=SpringClientFactory-microservice-user, started on Tue Dec 07 11:53:11 CST 2021, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@31e72cbc]] 746 | 2021-12-07 11:53:11.877 DEBUG [service-server,6228db7f8ae94ab0,9b859b488d9eff12,false] 17912 --- [oservice-user-1] o.s.c.s.i.w.c.f.LazyTracingFeignClient : Sending a request via tracing feign client [org.springframework.cloud.sleuth.instrument.web.client.feign.TracingFeignClient@38724594] and the delegate [feign.Client$Default@3767e736] 747 | ``` 748 | 749 | microservice-user: 750 | 751 | ```java 752 | 2021-12-07 11:53:11.963 DEBUG [microservice-user,6228db7f8ae94ab0,292c12ffaa37d7de,false] 9976 --- [nio-9000-exec-2] o.s.web.servlet.DispatcherServlet : GET "/list", parameters={} 753 | 2021-12-07 11:53:11.991 DEBUG [microservice-user,6228db7f8ae94ab0,292c12ffaa37d7de,false] 9976 --- [nio-9000-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK 754 | ``` 755 | 756 | 可以看出traceId, 是一样的 757 | 758 | ``` 759 | [服务名称,traceId(一条请求调用链中 唯一ID),spanID(基本的工作单元,获取数据等),是否让zipkin收集和展示此信息] 760 | ``` 761 | 762 | ##### Zipkin 763 | 764 | Sleuth的日志已经能将调用链路等信息打印出来,但看起来还是比较费事的,我们使用zipkin收集系统的时序数据,从而追踪微服务架构中系统延时等问题。还有一个友好的界面。 765 | 766 | 原理: 767 | 768 | sleuth收集跟踪信息通过http请求发送给zipkin server,zipkin将跟踪信息存储,以及提供RESTful API接口,zipkin ui通过调用api进行数据展示。 769 | 770 | 默认内存存储,可以用mysql,ES等存储。 771 | 772 | 使用: 773 | 774 | 1.每个需要监控的系统都需要引入依赖 775 | 776 | ```xml 777 | 778 | 779 | org.springframework.cloud 780 | spring-cloud-starter-zipkin 781 | 782 | ``` 783 | 784 | 2.每个需要监控的系统都需要修改配置文件 785 | 786 | ```yaml 787 | spring: 788 | #zipkin 789 | zipkin: 790 | base-url: http://localhost:9411/ 791 | #采样比例1 792 | sleuth: 793 | sampler: 794 | rate: 1 795 | ``` 796 | 797 | 3.下载、启动zipkin 798 | 799 | ​ Windows: 800 | 801 | ​ 1.访问官网https://zipkin.io/ 802 | 803 | ​ 2.点击[Quickstart](https://zipkin.io/pages/quickstart.html), 804 | 805 | ​ 3.点击[latest release](https://search.maven.org/remote_content?g=io.zipkin&a=zipkin-server&v=LATEST&c=exec)下载jar包 806 | 807 | ​ 4.用cmd启动 808 | 809 | ​ Liunx: 810 | 811 | ```shell 812 | curl -sSL https://zipkin.io/quickstart.sh | bash -s 813 | java -jar zipkin.jar 814 | ``` 815 | 816 | ​ Docker: 817 | 818 | ```shell 819 | docker run -d -p 9411:9411 openzipkin/zipkin 820 | ``` 821 | 822 | 下图为Windows启动后的截图 823 | 824 | ![zipkin](./doc_img/zipkin.jpg) 825 | 826 | 4.请求一次http://localhost/service-server/list接口,然后访问http://localhost:9411/,可以查看调用链路、依赖等等信息 827 | 828 | ![zipkin_ui](./doc_img/zipkin_ui.jpg) 829 | 830 | #### 健康监控(SpringBootAdmin) 831 | 832 | 1.使用Spring Initializr创建一个SpringBoot工程,引入依赖 833 | 834 | ```xml 835 | 836 | 837 | de.codecentric 838 | spring-boot-admin-starter-server 839 | 840 | 841 | 842 | de.codecentric 843 | spring-boot-admin-server-ui 844 | 845 | ``` 846 | 847 | 2.启动类增加注解 848 | 849 | ``` 850 | @EnableAdminServer 851 | ``` 852 | 853 | 3.在需要监控的微服务上添加依赖 854 | 855 | ```xml 856 | 857 | 858 | de.codecentric 859 | spring-boot-admin-starter-client 860 | 2.2.1 861 | 862 | 863 | 864 | org.springframework.boot 865 | spring-boot-starter-actuator 866 | 867 | ``` 868 | 869 | 4.修改需要监控的微服务的配置(可以将这部分配置放在配置中心统一管理) 870 | 871 | ```yaml 872 | spring: 873 | boot: 874 | admin: 875 | client: 876 | #SpringBootAdmin 地址 877 | url: http://localhost:81 878 | 879 | #监控端点 880 | management: 881 | endpoints: 882 | web: 883 | exposure: 884 | include: '*' 885 | endpoint: 886 | health: 887 | show-details: always 888 | ``` 889 | 890 | 5.访问http://localhost:81 查看监控信息 891 | 892 | ![admin_wallboard](./doc_img/admin_wallboard.jpg) 893 | 894 | ![admin_details](./doc_img/admin_details.jpg) 895 | 896 | 同时也可以配置邮箱、钉钉等服务上下线通知 897 | --------------------------------------------------------------------------------