SpringCloud: Build a complete micro-service architecture system based on the Spring Cloud netflix family bucket

Keywords: Java Spring Cloud Microservices

Overall architecture

Service Planning

Registration Services Example Host port Explain Access Address
EUREKASERVER EurekaServer1 ek1.com 9001 eureka Service 1 http://ek1.com:9001
EUREKASERVER EurekaServer2 ek2.com 9002 eureka Service 2 http://ek2.com:9002
SCSERVER sc-server1 scServer1 8001 Remote Service 1 Action: http://scserver1:8001/user/getName
Manual offline: http://scserver1:8001/health/adjust?status=down
SCSERVER sc-server2 scServer2 8002 Remote Service 2 Action: http://scserver2:8001/user/getName
SCCLIENT1 sc-client1 scclient1 7001 Client service 1;
Supports RestTemplate,Feign calls remote services through Ribbon load balancing;
Support Hystrix Service Fault Tolerant Governance, Monitoring, etc.
http://scclient1:7001/test/getUserNameByFeign
http://scclient1:7001/test/getUserNameByRestTemplate
http://scclient1:7001/hystrix
http://scclient1:7001/actuator/hystrix.stream
SCCLIENT2 sc-client2 scclient2 7002 Client Service 2 Support getting configuration from config configuration center;
Test actions: http://scclient2:7002/test/getConfig
SCGATEWAY sc-gateway scgateway 8888 zuul Gateway Service; Supports current limiting, etc. http://scgateway:8888/scclient1/test/getUserNameByFeign
http://scgateway:8888/scserver/user/getName
Turn on prefix access: http://scgateway:8888/api/v1/scserver/user/getName
Route monitoring: http://scgateway:8888/actuator/routes
SCCONFIG sc-config scconfig 9999 Configuration Center Configuration read from local: http://scconfig:9999/scClient2/local
Configuration read from git: http://scconfig:9999/netflix/scClient2-local.properties
SCADMIN sc-admin scadmin 8080 Spring Boot Admin´╝Ť
Support mail, pin warning;
TODO supports integrated logback real-time display of logs, etc.
http://scadmin:8080/wallboard
- - sczipkin 9411 zipkin link tracking http://sczipkin:9411/zipkin

Full Host Configuration

192.168.0.12 ek1.com
192.168.0.12 ek2.com
192.168.0.12 scServer1
192.168.0.12 scServer2
192.168.0.12 scclient1
192.168.0.12 scclient2
192.168.0.12 scgateway
192.168.0.12 sczipkin
192.168.0.12 scadmin
192.168.0.12 scconfig

Configure the Eureka Server registry (cluster mode)

Host Configuration

192.168.0.12 ek1.com
192.168.0.12 ek2.com

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

ek1 application.properties

#Server Port
server.port=9001
#Applying the name, the two highly available eureka nodes must be consistent
spring.application.name=eurekaServer

#eureka multi-node configuration
#Whether to register
eureka.client.enabled=true
#Whether to register yourself with another Eureka Server, default to true needs
eureka.client.register-with-eureka=true
#Whether to obtain registration information from eureka server requires
eureka.client.fetch-registry=true
#Set the URL of the service registry for client and server side communication
#This node should initiate requests to other nodes
eureka.client.serviceUrl.defaultZone=http://ek2.com:9002/eureka/
#Host name, required
eureka.instance.hostname=ek1.com
#Group name
#eureka.instance.app-group-name=eurekaServerGroup
#Whether to turn on self-protection
eureka.server.enable-self-preservation=true
#Trigger self-protection threshold
eureka.server.renewal-percent-threshold=0.85
#Failure Service Interval
eureka.server.eviction-interval-timer-in-ms=6000

ek2 application.properties

#Server Port
server.port=9002
#Apply name, two eureka nodes must be consistent
spring.application.name=eurekaServer

#eureka multi-node configuration
#Whether to register yourself with another Eureka Server, default to true needs
eureka.client.register-with-eureka=true
#Whether to obtain registration information from eureka server requires
eureka.client.fetch-registry=true
#Set the URL of the service registry for client and server side communication
#This node should initiate requests to other nodes
eureka.client.serviceUrl.defaultZone=http://ek1.com:9001/eureka/
#Host name, required
eureka.instance.hostname=ek2.com
#Group name
#eureka.instance.app-group-name=eurekaServerGroup
management.endpoint.shutdown.enabled=true

Apply Open EurekaServer

@EnableEurekaServer // Enable Eureka server

EurekaServer configuration optimization

TODO

EurekaServer address

Configure Eureka Clients

Host Configuration

192.168.0.12 scServer1
192.168.0.12 scServer2

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

scServer1 application.properties

server.port=8001
spring.application.name=scServer

#eureka client config
#Whether to register
eureka.client.enabled=true
#Whether to register yourself with another Eureka Server, default to true needs
eureka.client.register-with-eureka=true
#Whether to obtain registration information from eureka server requires
eureka.client.fetch-registry=true
#Set the URL of the service registry for client and server side communication
#Important Note: client registers with the configured first server address by default, fails the first registration and then registers with the second and the third (maximum of 3 retries)
eureka.client.serviceUrl.defaultZone=http://ek1.com:9002/eureka/,http://ek2.com:9002/eureka/
#Host name, required
eureka.instance.hostname=scServer1
#Renewal send interval defaults to 30 seconds, heartbeat interval
eureka.instance.lease-renewal-interval-in-seconds=5
#Represents how often a client pulls service registration information. The default is 30 seconds. If you want to get service registration status quickly, you can reduce this value, for example, 5 seconds
eureka.client.registry-fetch-interval-seconds=5
#Renewal expiration time (default 90 seconds)
eureka.instance.lease-expiration-duration-in-seconds=90
#Register instance IP on Eureka Server (multi-network card is applicable, other services can be accessed over IP)
#eureka.instance.prefer-ip-address=true
#eureka.instance.ip-address=192.168.0.12
#Whether to turn on health detection, corresponding to the Status state of the EurekaServer console
eureka.client.healthcheck.enabled=true

scServer2 application.properties

server.port=8002
spring.application.name=scServer

#eureka client config
#Whether to register
eureka.client.enabled=true
#Whether to register yourself with another Eureka Server, default to true needs
eureka.client.register-with-eureka=true
#Whether to obtain registration information from eureka server requires
eureka.client.fetch-registry=true
#Set the URL of the service registry for client and server side communication
#Important Note: client registers with the configured first server address by default, fails the first registration and then registers with the second and the third (maximum of 3 retries)
eureka.client.serviceUrl.defaultZone=http://ek1.com:9002/eureka/,http://ek2.com:9002/eureka/
#Host name, required
eureka.instance.hostname=scServer2
#Renewal send interval defaults to 30 seconds, heartbeat interval
eureka.instance.lease-renewal-interval-in-seconds=5
#Represents how often a client pulls service registration information. The default is 30 seconds. If you want to get service registration status quickly, you can reduce this value, for example, 5 seconds
eureka.client.registry-fetch-interval-seconds=5
#Renewal expiration time (default 90 seconds)
eureka.instance.lease-expiration-duration-in-seconds=90
#Register instance IP on Eureka Server (multi-network card is applicable, other services can be accessed over IP)
#eureka.instance.prefer-ip-address=true
#eureka.instance.ip-address=192.168.0.12
#Whether to turn on health detection, corresponding to the Status state of the EurekaServer console
eureka.client.healthcheck.enabled=true

Test Address

Configure client service calls (RestTemplate-based) and load balancing (Ribbon)

Explain:

  • Refer to sc-client1 application code implementation;
  • Ribbon dependency information already exists in the spring-cloud-starter-netflix-eureka-client dependency (no duplicate guide is required).

RestTemplate Client Configuration

/**
 * Web To configure
 *
 * @author binglang
 * @date 2021/11/24 10:44
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {
    // Turn on load balancing
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Client Call

    /**
     * Test RestTemplate service call
     */
    @GetMapping(value = "/getUserNameByRestTemplate")
    // Integrate Hystrix Configuration Service Demotion
    // @HystrixCommand(fallbackMethod = "fallback")
    public String getUserNameByRestTemplate() {
        String serviceUrl = "http://SCSERVER/user/getName";
        String username = restTemplate.getForObject(serviceUrl, String.class);

        return username;
    }

Test Address

Configure client service calls (Feign-based) and load balancing (Ribbon)

Description: Refer to sc-client1 application code implementation.

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Apply Open Feign Client

@EnableFeignClients

Implementation process

// Two-party library sc-common defines the service public interface
com.binglangaimo.sccommon.service.feign.ICommonUserService

// Service provider sc-server1,sc-server2 must implement this interface
com.binglangaimo.scserver1.controller.UserController

// Service caller sc-client1 defines an interface with @FeignClient annotation
com.binglangaimo.scclient1.service.TestFeignService

// Service Calls
com.binglangaimo.scclient1.controller.TestController#getUserNameByFeign()

Test Address

Configure Service Fault Tolerant Hystrix

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

Apply Open Hystrix Circuit Breaker

@EnableCircuitBreaker // Turn on Hystrix circuit breaker

Application Configuration

Description: Refer to sc-client1/src/main/resources/application.properties

#feign configuration
#feign call to turn on hystrix-enabled circuit breakers
feign.hystrix.enabled=true

Three implementations:

Reference code implementation: com.binglangaimo.scclient1.service.TestFeignService

  • Request method with @HystrixCommand comment
  • @FeignClient(value = "SCSERVER", fallback = TestFeignServiceFallBack.class)
  • @FeignClient(qualifier = "testFeignServiceClient", value = "SCSERVER", fallbackFactory = TestFeignServiceFallBackFactory.class)

Configure Hystrix Dashboard

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

Apply Open Hystrix Dashboard

@EnableHystrixDashboard // Open Hystrix Dashboard

Application Configuration

#hystrix configuration, no configuration will error, reference: https://www.cnblogs.com/itsharehome/p/15628220.html
hystrix.dashboard.proxy-stream-allow-list=scclient1

#actuator monitoring parameters
#Open all endpoints (not recommended), production environment only needs to be opened
management.endpoints.web.exposure.include=*

Monitoring Address

Configure Service Tracking Sleuth+Zipkin

Description: Services that need to be tracked need to be configured as follows. You can refer to the sc-client1 implementation.

Explanation of principle

  • sleuth collects tracking information and sends it to zipkin server through http requests;
  • zipkin server stores tracking information (default memory, configurable to use mysql, ES, etc.) and provides a RESTful API;
  • zipkin ui presents data by calling api.

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

Application Configuration

#zipkin configuration
#zipkin server address
spring.zipkin.base-url=http://sczipkin:9411/
#sampling frequency
spring.sleuth.sampler.rate=10

Download and start Zipkin

// Download Command
curl -sSL https://zipkin.io/quickstart.sh | bash -s

// Start zipkin
java -jar zipkin.jar

Test Address

Configure Service Gateway Zuul

Note: Refer to the sc-gateway application implementation.

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

Application Configuration

sc-gateway/src/main/resources/application.properties

Apply Open zuul Gateway

@EnableZuulProxy // Open Zuul Gateway

Test Address

Configure Service Configuration Center Spring Cloud Config

Description: sc-config is the configuration center (Config Server); sc-client2 supports getting the configuration from the Config client.

config server

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Application Configuration

Important Notes:

  • Config Server supports configuration to obtain configuration information from local or remote (git or svn);
  • The project config directory is used to store configuration information (public configurations such as eureke client, actuator are supported by include);
  • Config Server Configuration Remote Access Configuration supports SSH key authentication, but it does not support reading local key files to obtain key information. Only key strings can be configured in the application configuration file. I enhanced the MultipleJGitEnvironmentProperties bean to support this functionality.
server.port=9999
spring.application.name=scConfig

#config registry configuration
#Use local configuration
#spring.profiles.active=native
#spring.cloud.config.server.native.search-locations=file:xxx/WWW/study/springcloud/config
#Configuring with git
spring.cloud.config.server.git.uri=git@gitee.com:binglangaimo/springcloud.git
spring.cloud.config.server.git.strict-host-key-checking=false
spring.cloud.config.server.git.private-key=file:C:/Users/xxx/.ssh/id_rsa #Supports reading key information from local files to ensure the security of key information
spring.cloud.config.server.git.ignore-local-ssh-settings=false
spring.cloud.config.server.git.default-label=netflix
spring.cloud.config.server.git.search-paths=config

Apply Open Config Server

@EnableConfigServer

Test Validation

config client

maven dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

Apply Configuration (bootstrap.properties)

spring.application.name=scClient2
spring.profiles.active=local

#Get Configuration from Registry
spring.cloud.config.uri=http://scconfig:9999
spring.cloud.config.fail-fast=true

Test Get Configuration

Configure Service Master Spring Boot Admin

Explain:

  • Reference sc-admin application implementation;
  • Spring Boot Admin needs to rely on actuators to collect information, so configure actuators for all the services you need to monitor.

maven dependency

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
</dependency>

Application Configuration

server.port=8080
spring.application.name=scAdmin

#Here is the eureka client public configuration, pasted no more

#This is the actuator public configuration, do not paste again

Apply Open AdminServer

@EnableAdminServer // Turn on Admin monitoring

Send pin message

com.binglangaimo.scadmin.notify.DingDingNotifier Implementation Class

Test Address

Code repository

My personal gitee- https://gitee.com/binglangaimo/springcloud . (Emphasis: Code is for learning reference only and should not be used for commercial purposes without authorization from the author).

Posted by Josien on Fri, 03 Dec 2021 17:01:56 -0800