6 Service Registration Center Consul
The core components of spring cloud are described in the previous article, including service registration and discovery components Eureka, fuse hystrix, configuration Center Spring Cloud Config, and service Gateway Zuul.
Work on the basis of the previous article. Upgrade the version of Spring Cloud first: Upgrade the version of the parent module in Pom.xml to 1.5.4.RELEASE while upgrading the Spring-cloud-dependencies component to the DALSTON.SR1 version.
Spring Cloud Consul is a service discovery framework, similar to the Eureka effect. Of course, commonly used service discovery framework or service registry also has ZOOKEEPER,ETCD and so on. The comparison chart is as follows:
The specific differences between the frameworks are not discussed, and the Eureka in the previous article is replaced with Consul.
Download the Consul Service Registration center from the official website https://www.consul.io/downloads.html. Start Consul in development mode:
Consul Agent–dev
Open http://localhost:8500, you can see the Consul UI interface.
Replace the dependency module for the service provider:
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start er-parent</artifactid> <version>1.5.4.RELEASE</version> </parent> <dependencies > <dependency> <groupId>org.springframework.boot</groupId> <artifa Ctid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
; <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spri ng-cloud-starter-consul-discovery</artifactid> </dependency> <dependency> &L T;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artif actid> </dependency> </dependencies> <dependencyManagement> <dependencies&
Gt <dependency> <groupId>org.springframework.cloud</groupId> <artifact
Id>spring-cloud-dependencies</artifactid> <version>Dalston.SR1</version>
<type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Then modify the configuration file to:
Spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
The service interface is modified to:
@RestController public
class Computecontroller {
private final Logger Logger = Loggerfactory.getlogger ( GetClass ());
@Autowired
discoveryclient discoveryclient;
@GetMapping ("/DC") public
String DC () {
String services = "Services:" + discoveryclient.getservices ();
SYSTEM.OUT.PRINTLN (services);
return services;
}
}
Service consumers depend on:
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start er-parent</artifactid> <version>1.5.4.RELEASE</version> </parent> <dependencies > <dependency> <groupId>org.springframework.cloud</groupId> <artif
Actid>spring-cloud-starter-consul-discovery</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-STARTER-WEB&L t;/artifactid> </dependency> <dependency> <groupid>org.springframework.bo Ot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </DEPENDENCY&G
T <dependency> <groupId>org.springframework.boot</groupId> <artifactid>sprin G-boot-starter-test</artifactid> <scope>test</scope> </dependency> </DEPENDENCIES&G
T <dependencyManagement> <dependencies> <dependency> <groupid>or
G.springframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <s cope>import</scope> </dependency> </dependencies> </dependencyManagement>
Configuration file:
Spring.application.name=consul-consumer
server.port=2101
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
Service Consumer Main class:
@SpringBootApplication
@EnableDiscoveryClient Public
class Consumerapplication {
@Bean
Resttemplate resttemplate () {
return new resttemplate ();
}
public static void Main (string[] args) {
new Springapplicationbuilder (Consumerapplication.class). Web (True). Run ( args);
}
}
Consumer Interface class:
@RestController public
class Consumercontroller {
@Resource
loadbalancerclient loadbalancerclient;
@Resource
resttemplate resttemplate;
@GetMapping ("/consumer") public
String DC () {
serviceinstance serviceinstance = Loadbalancerclient.choose (" Consul-provider1 ");
String url = "/http" + serviceinstance.gethost () + ":" + serviceinstance.getport () + "/DC";
System.out.println (URL);
Return Resttemplate.getforobject (URL, string.class);
}
}
Start service providers and service consumers separately, and access Http://localhost:2101/consumer. Back to services: [Consul, Consul-consumer, Consul-provider1].
The above is the consul as a registration center, if the use of Eureka as a registration center, in addition to Eureka Registration center need to build their own outside, the other basic. 7 Hystrix Monitoring
As described earlier, the circuit breaker determines whether a fuse is required based on the request in the Snapshot time window, and the indicator information for these request conditions is an important metric that is recorded during the execution of the Hystrixcommand and Hystrixobservablecommand instances. In addition to the use of Hystrix circuit breaker implementations, they are also very helpful for system operation and maintenance. These metrics information is aggregated in the form of "Scrolling time windows" and "buckets", and residing in memory for a period of time for internal or external query use, Hystrix Dashboard is the consumer of these metrics content.
Create a service instance My-consumer3. Here still with Eureka as the registry, add dependencies:
<parent> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-sta rter-parent</artifactid> <version>Dalston.SR1</version> </parent> <dependencies > <dependency> <groupId>org.springframework.cloud</groupId> <artif
Actid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-hystrix</a rtifactid> </dependency> <dependency> <GROUPID>ORG.SPRINGFRAMEWORK.BOOT&L
T;/groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID&G T;spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework .cloud</groupid> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependenc Y> </dependencies>
The configuration file is:
Spring.application.name=consumer3
server.port=2103
eureka.client.serviceurl.defaultzone=http:// localhost:1111/eureka/
The controller is:
@RestController public
class Consumercontroller {
@Resource
consumerservice consumerservice;
@GetMapping ("/consumer") public
String DC () {
return Consumerservice.consumer ();
}
@Service
class Consumerservice {
@Resource
resttemplate resttemplate;
@HystrixCommand (Fallbackmethod = "fallback") public
String consumer () {
return Resttemplate.getforobject (" Http://compute-service/add?a=1&b=2 ", String.class);
}
Public String fallback () {
return ' fallbck ';
}
}
}
The main class needs to open @enablecircuitbreaker annotations:
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker Public
class ConsumerApplication3 {
@Bean
@LoadBalanced
resttemplate resttemplate () {
return new resttemplate ();
} Public
static void Main (string[] args) {
springapplication.run (Consumerapplication3.class,args);
}
}
After starting the service registry and the Compute-service service instance and this instance, you can invoke the Compute-service service by invoking the consumer interface provided by this instance.
The Mysc-hystrixdashboard instance is then created. Depend on:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-parent</artifactid>
<version>Dalston.SR1</version>
</parent >
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid >
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
< dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-hystrix-dashboard</artifactid>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator</artifactid>
</dependency>
</dependencies>
Configuration file:
Spring.application.name=hystrix-dashboard
server.port=1301
Main class:
@SpringCloudApplication
@EnableHystrixDashboard Public
class Hystrixdashboardapplication {
public static void Main (string[] args) {
springapplication.run (Hystrixdashboardapplication.class,args);
}
}
In fact, the official also provided a monitoring package: Hystrix-dashboard-#.#.#.war, downloaded and placed on the server, the same use. But here you can use the monitoring instance you created for your convenience.
Start the Hystrix monitoring instance and visit http://localhost:1301 to see the Hystrix Dashboard home page. From the page description can be learned that Hystrix dashboard support three different monitoring methods, respectively:
Default cluster monitoring: Enable monitoring of the default cluster via Urlhttp://turbine-hostname:port/turbine.stream.
Specified cluster monitoring: enabled by Urlhttp://turbine-hostname:port/turbine.stream?cluster=[clustername] to enable monitoring of the clustername cluster.
Monitoring of monomer application: through Urlhttp://hystrix-app:port/hystrix.stream, it realizes the monitoring of a specific service instance.
The monitoring of the cluster needs to be integrated turbine to be realized. This first looks at the monitoring of individual service instances. The actuator and Hystrix dependency packages are introduced in the Consumer3 instance, so hystrix monitoring of the instance can be implemented. 7.1 Single-instance monitoring
Start Consumer3, invoke the consumer interface provided by Consumer3 once (if not called at once, the monitoring page is always in loading). Then visit Http://localhost:1301/hystrix, enter the instance address you want to monitor in the input box: Http://localhost:2103/hystrix.stream, you can see the Hystrix monitoring information for that instance.
The Monitoring station home page has two parameters, namely delay and title. Where delay is used to control the latency of the server polling for monitoring information, which defaults to 2000 milliseconds, you can reduce the client's network and CPU consumption by configuring this property. Title corresponds to the content of the monitoring address, by default using the URL of the specific monitoring instance, you can configure this information to show a more appropriate title.
The monitoring page for the specific instance can see a solid circle and a curve.
Solid Circle: A total of two meanings. Its color changes represent the health of the instance, its health from green, yellow, and orange. Red decreases in turn. At the same time, the size of the solid circle changes with the request traffic, the larger the volume, the larger the solid circle. So with the size and color of the solid circle, you can quickly find fault instances and high-pressure instances in a large number of instances.
Curve: Used to record the relative change of flow in two minutes, it can be observed in the flow of the rise and fall trend.
Other: Two columns separated by a vertical line, and the left one in turn represents the number of successful requests, short circuits and fuses. The right-hand side represents the number of request timeouts, the number of thread pool rejections, and the number of failures/exceptions. The right percentage represents the error ratio for the last 10 seconds. Host and cluster values indicate the frequency of requests on the host and cluster, and circuit represents the circuit breaker status. The bottom left is the host report under the cluster, and the right side is the percent delay statistic. 7.2 monitoring Data aggregation
There are multiple instances in the actual production environment, where the metric data needs to be aggregated for multiple instances, so the turbine needs to be consolidated.
In the previous Hystrix monitoring example, the implementation of the architecture is shown in the following figure:
Based on the above architecture, turbine is introduced to aggregate the Hystrix data of the service. There are two ways of aggregating.
collect aggregations over HTTP
Create a turbine project that relies on:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-parent</artifactid>
<version>Dalston.SR1</version>
</parent >
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid >
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
< dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator</artifactid>
</dependency>
</dependencies>
Create the application main class turbineapplication and use the @enableturbine annotation to turn on turbine:
@Configuration
@EnableAutoConfiguration
@EnableTurbine
@EnableDiscoveryClient Public
class turbineapplication {public
static void Main (string[] args) {
Springapplication.run (turbineapplication.class , args);
}
}
Add the relevant configuration of Eureka and turbine in Application.properties, as follows:
Spring.application.name=turbine
server.port=8989
management.port=8990
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/
Turbine.app-config=consumer3
turbine.cluster-name-expression= "Default"
turbine.combine-host-port=true
Parameter description:
turbine.app-config Specifies the name of the service for which monitoring information needs to be collected.
turbine.cluster-name-expression Specifies that the cluster name is default, and when the number of services is very large, multiple turbine services can be started to build different aggregation clusters. This parameter can be used to differentiate between these different aggregation clusters, and this parameter can locate different aggregation clusters in the Hystrix dashboard, only by specifying them in the URL of the Hystrix stream via the cluster parameter.
Turbine.combine-host-port is set to true to allow services on the same host to be distinguished by a combination of host name and port number, which, by default, distinguishes different services by host. This allows different services on the local computer to be aggregated into a single service to be counted when debugging locally.
Start the Service registry Server1 and Server2, service provider Provider2, service consumer Consumer3,hystrix monitoring, and turbine aggregation instances, in http://localhost:1301/ Hystrix monitoring on the first page of the Http://localhost:8989/turbine.stream, you will see aggregated data monitoring for the service Consumer3. The schema is:
collecting aggregations through the message agent
Spring Cloud also implements the collection implementation based on the message agent when it encapsulates the turbine. Therefore, all the monitoring information that needs to be collected can be output to the message agent, and then the turbine service obtains these monitoring information asynchronously from the message agent, and finally aggregates and outputs the monitoring information into the Hystrix dashboard. By introducing the message broker, the monitoring architecture implemented by the turbine and Hystrix dashboard can be changed to the following: