Crazy SpringCloud Notes 05 (Hystrix and Zuul and Config)

Keywords: Java Spring Cloud http

8. Hystrix: service is broken

Problems with Distributed Systems

Applications in a complex distributed architecture have dozens of dependencies, each of which will inevitably fail at some point!

8.1 Service Avalanches

When invoking between multiple micro-services, assume that micro-service A invokes micro-service B and micro-service C, and micro-service B and micro-service C invoke other micro-services, which is called "fan out". If the call response time of a micro-service on a fan-out link is too long or unavailable, the call to micro-service A will consume more and more system resources.This causes a system crash, the so-called avalanche effect.

For high-traffic applications, a single back-end dependency can cause all resources on all servers to be saturated in a few seconds.Worse than failures, these applications can also result in increased latencies between services, backup queues, thread and other system resource constraints, and more cascading failures across the system, all indicating the need to isolate and manage failures and latencies in order to achieve a single dependency failure without affecting the entire application or system.

What we need is the abandoned guard!

8.2 What is Hystrix?

( Hystrix is an open source library for dealing with latency and fault tolerance in distributed systems where many dependencies inevitably fail to invoke, such as timeouts, exceptions, etc. It ensures that if a dependency fails, the entire system service will not fail and cascade failures will be avoided to improve the flexibility of the distributed system.

Circuit Breakers themselves are switch devices that, when a service unit fails, return to the caller a service-expected, processable alternative response (FallBack) through the failure monitoring of the breaker (similar to a fuse break), rather than a long wait or throwing an exception that the calling method cannot handle.This ensures that the service caller's threads will not be consumed for a long time and unnecessarily, thereby avoiding the spread of failures in distributed systems and even avalanches.

8.3 What can Hystrix do?

  • service degradation
  • Service Fusion
  • Service Limiting
  • Near real-time monitoring
  • ...

When everything works, the request flow can look like this:

When there is a potentially blocking service in many back-end systems, it can block entire user requests:

As the volume of traffic increases, the potential for a single back-end dependency causes all resources on all servers to become saturated in seconds.

Each point in the application that may cause network requests through the network or client libraries is the source of potential failures.Worse than failure, these applications can also cause delays between services to increase, backing up queues, threads, and other system resources, resulting in more cascading failures across systems.

When you wrap each base dependency using Hystrix, the architecture shown in the diagram above changes similar to the diagram below.Each dependency is isolated from each other, limited to the resources it can populate when a delay occurs, and included in fallback logic, which determines the response to any type of failure in the dependency:

Official website information:https://github.com/Netflix/Hystrix/wiki

8.4 Service Fusion

What is service meltdown?

( The breakdown mechanism is a microservice link protection mechanism that bets on avalanche effect.

When a microservice in the fan out link is unavailable or the response time is too long, the service is degraded, which in turn breaks the call to the node's microservice and quickly returns the wrong response information.The call link is restored after the node's microservice call response is detected to be normal.In the SpringCloud framework, the fusing mechanism is implemented through Hystrix.Hystrix monitors the status of calls between microservices, and when a failed call fails to a certain threshold, by default, 20 calls fail within five seconds, the fuse mechanism starts.The comment for the melting mechanism is: @HystrixCommand.

Service breakdown solves the following problems:

  • When the dependent object is unstable, it can fail quickly.
  • After a quick failure, it is possible to dynamically test whether the dependent object is recovered based on a certain algorithm.

Getting Started Cases

Create a new springcloud-provider-dept-hystrix-8001 module and copy the pom.xml, resource, and Java code from springcloud-provider-dept-8001 to initialize and adjust.

Import hystrix dependencies

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#708090 "><!--Import Hystrix Dependencies --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-hystrix<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Adjust yml profile

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#0077aa">server</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">port</span><span style="color:#999999">:</span> <span style="color:#986801">8001</span>

<span style="color:#708090"># mybatis configuration </span>
<span style="color:#0077aa">mybatis</span><span style="color:#999999">:</span>
  <span style="color:#708090"># pojo package </span>under springcloud-api module
  <span style="color:#0077aa">type-aliases-package</span><span style="color:#999999">:</span> com.haust.springcloud.pojo
  <span style="color:#708090"># mybatis-config.xml core profile class path </span>under this module
  <span style="color:#0077aa">config-location</span><span style="color:#999999">:</span> classpath<span style="color:#999999">:</span>mybatis/mybatis<span style="color:#999999">-</span>config.xml
  <span style="color:#708090"># The mapper profile class path under this module </span>
  <span style="color:#0077aa">mapper-locations</span><span style="color:#999999">:</span> classpath<span style="color:#999999">:</span>mybatis/mapper/*.xml

<span style="color:#708090"># spring configuration </span>
<span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">application</span><span style="color:#999999">:</span>
    <span style="color:#708090">#Project name </span>
    <span style="color:#0077aa">name</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>provider<span style="color:#999999">-</span>dept
  <span style="color:#0077aa">datasource</span><span style="color:#999999">:</span>
    <span style="color:#708090"># Druid Data Source</span>
    <span style="color:#0077aa">type</span><span style="color:#999999">:</span> com.alibaba.druid.pool.DruidDataSource
    <span style="color:#0077aa">driver-class-name</span><span style="color:#999999">:</span> com.mysql.jdbc.Driver
    <span style="color:#0077aa">url</span><span style="color:#999999">:</span> jdbc<span style="color:#999999">:</span>mysql<span style="color:#999999">:</span>//localhost<span style="color:#999999">:</span>3306/db01<span style="color:#999999">?</span>useUnicode=true<span style="color:#ee9900">&characterEncoding</span>=utf<span style="color:#999999">-</span><span style="color:#986801">8</span>
    <span style="color:#0077aa">username</span><span style="color:#999999">:</span> root
    <span style="color:#0077aa">password</span><span style="color:#999999">:</span> root

<span style="color:#708090"># Eureka configuration: Configure service registry address </span>
<span style="color:#0077aa">eureka</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">client</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">service-url</span><span style="color:#999999">:</span>
      <span style="color:#708090"># Registry Address 7001-7003</span>
      <span style="color:#0077aa">defaultZone</span><span style="color:#999999">:</span> http<span style="color:#999999">:</span>//eureka7001.com<span style="color:#999999">:</span>7001/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7002.com<span style="color:#999999">:</span>7002/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7003.com<span style="color:#999999">:</span>7003/eureka/
  <span style="color:#0077aa">instance</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">instance-id</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>provider<span style="color:#999999">-</span>dept<span style="color:#999999">-</span>hystrix<span style="color:#999999">-</span><span style="color:#986801">8001 </span><span style="color:#708090">#Modify the default description information on Eureka </span>
    <span style="color:#0077aa">prefer-ip-address</span><span style="color:#999999">:</span> <span style="color:#ee9900">true </span><span style="color:#708090">#Change to true to show ip address by default instead of localhost</span>

<span style="color:#708090">#info configuration </span>
<span style="color:#0077aa">info</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">app.name</span><span style="color:#999999">:</span> haust<span style="color:#999999">-</span>springcloud <span style="color:#708090">#Name of project </span>
  <span style="color:#0077aa">company.name</span><span style="color:#999999">:</span> com.haust <span style="color:#708090">#Company name </span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

prefer-ip-address: false:

prefer-ip-address: true:

Modify controller

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#708090">/**
 * @Auther: csp1999
 * @Date: 2020/05/17/22:06
 * @Description: Provide Restful Services
 */</span>
<span style="color:#999999">@RestController</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> DeptController <span style="color:#999999">{</span>

    <span style="color:#999999">@Autowired</span>
    <span style="color:#0077aa">private</span> DeptService deptService<span style="color:#999999">;</span>

    <span style="color:#708090">/**
     * Query department information based on id
     * If an exception occurs based on the id query, follow the hystrixGet alternative code
     * @param id
     * @return
     */</span>
    <span style="color:#999999">@HystrixCommand</span><span style="color:#999999">(</span>fallbackMethod <span style="color:#a67f59">=</span> <span style="color:#50a14f">"hystrixGet"</span><span style="color:#999999">)</span>
    <span style="color:#999999">@RequestMapping</span><span style="color:#999999">(</span><span style="color:#50a14f">"/dept/get/{id}"</span><span style="color:#999999">)</span><span style="color:#708090 ">//Query by id</span>
    <span style="color:#0077aa">public</span> Dept <span style="color:#dd4a68">get</span><span style="color:#999999">(</span><span style="color:#999999">@PathVariable</span><span style="color:#999999">(</span><span style="color:#50a14f">"id"</span><span style="color:#999999">)</span> Long id<span style="color:#999999">)</span><span style="color:#999999">{</span>
        Dept dept <span style="color:#a67f59">=</span> deptService<span style="color:#999999">.</span><span style="color:#dd4a68">queryById</span><span style="color:#999999">(</span>id<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#0077aa">if</span> <span style="color:#999999">(</span>dept<span style="color:#a67f59">==</span>null<span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#0077aa">throw</span> <span style="color:#0077aa">new</span> RuntimeException<span style="color:#999999">(</span><span style="color:#50a14f">"this id=>"</span><span style="color:#a67f59">+</span>id<span style="color:#a67f59">+</span><span style="color:#50a14f">",The user does not exist or the information cannot be found~"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
        <span style="color:#0077aa">return</span> dept<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#708090">/**
     * Query alternatives based on id (fused)
     * @param id
     * @return
     */</span>
    <span style="color:#0077aa">public</span> Dept <span style="color:#dd4a68">hystrixGet</span><span style="color:#999999">(</span><span style="color:#999999">@PathVariable</span><span style="color:#999999">(</span><span style="color:#50a14f">"id"</span><span style="color:#999999">)</span> Long id<span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#0077aa">return</span> <span style="color:#0077aa">new</span> Dept<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#dd4a68">setDeptno</span><span style="color:#999999">(</span>id<span style="color:#999999">)</span>
                <span style="color:#999999">.</span><span style="color:#dd4a68">setDname</span><span style="color:#999999">(</span><span style="color:#50a14f">"this id=>"</span><span style="color:#a67f59">+</span>id<span style="color:#a67f59">+</span><span style="color:#50a14f">",No corresponding information,null---@Hystrix~"</span><span style="color:#999999">)</span>
                <span style="color:#999999">.</span><span style="color:#dd4a68">setDb_source</span><span style="color:#999999">(</span><span style="color:#50a14f">"stay MySQL No such database in"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Add support comment @EnableCircuitBreaker for main startup class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#708090">/**
 * @Auther: csp1999
 * @Date: 2020/05/17/22:09
 * @Description: Startup Class
 */</span>
<span style="color:#999999">@SpringBootApplication</span>
<span style="color:#999999">@EnableEurekaClient</span> <span style="color:#708090'>// EnableEurekaClient client's startup class, automatically registers the service with the registry after the service starts</span>
<span style="color:#999999">@EnableDiscoveryClient</span> <span style="color:#708090 ">//Service Discovery~</span>
<span style="color:#999999">@EnableCircuitBreaker</span> <span style="color:#708090 ">//Add support note for fusing </span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> HystrixDeptProvider_8001 <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>HystrixDeptProvider_8001<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Test:

After using the fuse, when accessing an id that does not exist, the front page shows the data as follows:

Instead of a fused springcloud-provider-dept-8001 module accessing the same address, the following occurs:

Therefore, it is necessary to use a fuse to avoid errors in the entire application or web page caused by an exception or error in a microservice background.

8.5 Service Degradation

What is service downgrade?

Service downgrade means that when server pressure increases dramatically, some services and pages are not handled strategically according to actual business conditions and traffic, or handled in a simple way, thereby releasing server resources to ensure the normal or efficient operation of core business.To put it plainly, try to give system resources to high priority services as much as possible.

Resources are limited and requests are unlimited.If you do not downgrade the service during the concurrent peak period, on the one hand, it will certainly affect the performance of the overall service. Severe cases may cause downtime of some important services unavailable.Therefore, in the rush hour, in order to ensure the availability of core function services, some services should be downgraded.For example, when Double 11 was active, downgraded all non-transactional service systems, such as viewing ants in the forest, viewing historical orders, and so on.

What are the scenarios in which service downgrades are mainly used?When the overall load of the entire micro-service architecture exceeds the preset upper threshold or when upcoming traffic is expected to exceed the preset threshold, some non-essential or non-urgent services or tasks can be delayed or suspended in order to ensure that important or basic services can function properly.

Depending on the business, the downgrade can delay services, such as adding credits to users, but putting them in a cache and waiting for the service to stabilize before executing.Or shut down services at a granular scale, such as turning off recommendations from related articles.

As can be seen from the figure above, when the amount of access to service A increases dramatically and B and C visit less, in order to alleviate the pressure on service A, it is necessary for B and C to temporarily shut down some of the service functions to undertake some of the services of A, thus sharing the pressure on A, called service downgrade.

Issues to consider for service downgrade

  • 1) Which services are core services and which are non-core services
  • 2) Which services can support downgrade, which services cannot support downgrade, what is the downgrade strategy
  • 3) Is there a more complex business release scenario in addition to service downgrade, and what is the strategy?

Automatic downgrade classification

1) Timeout demotion: Mainly configure timeout time and number and mechanism of timeout retries, and use asynchronous mechanism to detect replies

2) Failure downgrade: mainly some unstable api, when the number of failed calls reaches a certain threshold, the same asynchronous mechanism should be used to detect the response

3) Fault downgrade: For example, if the remote service to be invoked is dropped (network failure, DNS failure, http service return error status code, rpc service throws an exception), it can be directly downgraded.The downgraded solutions are: default values (e.g., inventory service hangs up, default spot returns), background data (e.g., advertisements hang up, returning some static pages prepared in advance), caching (some cached data that was temporarily stored before)

4) Flow restriction downgrade: when killing seconds or purchasing some restricted goods, the system may crash because of too much visits. Flow restriction will be used to limit access at this time. When the threshold of flow restriction is reached, subsequent requests will be downgraded;The downgraded solutions can be queued pages (divert users to queued pages for a retry), out of stock (directly inform users that they are out of stock), error pages (if the activity is too hot, try again later).

Getting Started Cases

Create a new downgraded configuration class DeptClientServiceFallBackFactory.java in the service package under the springcloud-api module

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#708090">/**
 * @Auther: csp1999
 * @Date: 2020/05/20/9:18
 * @Description: Hystrix Service Demotion~
 */</span>
<span style="color:#999999">@Component</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> DeptClientServiceFallBackFactory <span style="color:#0077aa">implements</span> FallbackFactory <span style="color:#999999">{</span>

    <span style="color:#999999">@Override</span>
    <span style="color:#0077aa">public</span> DeptClientService <span style="color:#dd4a68">create</span><span style="color:#999999">(</span>Throwable cause<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        <span style="color:#0077aa">return</span> <span style="color:#0077aa">new</span> DeptClientService<span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
            <span style="color:#999999">@Override</span>
            <span style="color:#0077aa">public</span> Dept <span style="color:#dd4a68">queryById</span><span style="color:#999999">(</span>Long id<span style="color:#999999">)</span> <span style="color:#999999">{</span>
                <span style="color:#0077aa">return</span> <span style="color:#0077aa">new</span> Dept<span style="color:#999999">(</span><span style="color:#999999">)</span>
                        <span style="color:#999999">.</span><span style="color:#dd4a68">setDeptno</span><span style="color:#999999">(</span>id<span style="color:#999999">)</span>
                        <span style="color:#999999">.</span><span style="color:#dd4a68">setDname</span><span style="color:#999999">(</span><span style="color:#50a14f">"id=>"</span> <span style="color:#a67f59">+</span> id <span style="color:#a67f59">+</span> <span style="color:#50a14f">"Without the corresponding information, the client provided demotion information and the service has now been shut down"</span><span style="color:#999999">)</span>
                        <span style="color:#999999">.</span><span style="color:#dd4a68">setDb_source</span><span style="color:#999999">(</span><span style="color:#50a14f">"no data~"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>
            <span style="color:#999999">@Override</span>
            <span style="color:#0077aa">public</span> List<span style="color:#dd4a68"><span style="color:#999999"><</span>Dept<span style="color:#999999">></span></span> <span style="color:#dd4a68">queryAll</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
                <span style="color:#0077aa">return</span> null<span style="color:#999999">;</span>
            <span style="color:#999999">}</span>

            <span style="color:#999999">@Override</span>
            <span style="color:#0077aa">public</span> Boolean <span style="color:#dd4a68">addDept</span><span style="color:#999999">(</span>Dept dept<span style="color:#999999">)</span> <span style="color:#999999">{</span>
                <span style="color:#0077aa">return</span> <span style="color:#0184bb">false</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>
        <span style="color:#999999">}</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Specify the downgraded configuration class DeptClientServiceFallBackFactory in DeptClientService

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@Component</span> <span style="color:#708090 ">//Register in spring Container </span>
<span style="color:#708090 ">/@FeignClient: Micro-service client comment, value: Specify the name of the micro-service so that the Feign client can find the corresponding micro-service directly </span>
<span style="color:#999999">@FeignClient</span><span style="color:#999999">(</span>value <span style="color:#a67f59">=</span> <span style="color:#50a14f">"SPRINGCLOUD-PROVIDER-DEPT"</span><span style="color:#999999">,</span>fallbackFactory <span style="color:#a67f59">=</span> DeptClientServiceFallBackFactory<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">)</span><span style="color:#708090 ">//fallbackFactory specifies demotion configuration class </span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">interface</span> DeptClientService <span style="color:#999999">{</span>

    <span style="color:#999999">@GetMapping</span><span style="color:#999999">(</span><span style="color:#50a14f">"/dept/get/{id}"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">public</span> Dept <span style="color:#dd4a68">queryById</span><span style="color:#999999">(</span><span style="color:#999999">@PathVariable</span><span style="color:#999999">(</span><span style="color:#50a14f">"id"</span><span style="color:#999999">)</span> Long id<span style="color:#999999">)</span><span style="color:#999999">;</span>

    <span style="color:#999999">@GetMapping</span><span style="color:#999999">(</span><span style="color:#50a14f">"/dept/list"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">public</span> List<span style="color:#dd4a68"><span style="color:#999999"><</span>Dept<span style="color:#999999">></span></span> <span style="color:#dd4a68">queryAll</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>

    <span style="color:#999999">@GetMapping</span><span style="color:#999999">(</span><span style="color:#50a14f">"/dept/add"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">public</span> Boolean <span style="color:#dd4a68">addDept</span><span style="color:#999999">(</span>Dept dept<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Turn on demotion in the springcloud-consumer-dept-feign module:

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#0077aa">server</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">port</span><span style="color:#999999">:</span> <span style="color:#986801">80</span>

<span style="color:#708090"># Eureka configuration </span>
<span style="color:#0077aa">eureka</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">client</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">register-with-eureka</span><span style="color:#999999">:</span> <span style="color:#ee9900">false </span><span style="color:#708090"># Do not register yourself with Eureka </span>
    <span style="color:#0077aa">service-url</span><span style="color:#999999">:</span> <span style="color:#708090"># Randomly select one of the three registries to access </span>
      <span style="color:#0077aa">defaultZone</span><span style="color:#999999">:</span> http<span style="color:#999999">:</span>//eureka7001.com<span style="color:#999999">:</span>7001/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7002.com<span style="color:#999999">:</span>7002/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7003.com<span style="color:#999999">:</span>7003/eureka/

<span style="color:#708090"># Turn on demoted feign.hystrix</span>
<span style="color:#0077aa">feign</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">hystrix</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">enabled</span><span style="color:#999999">:</span> <span style="color:#ee9900">true</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

8.6 Difference between service breakdown and downgrade

  • Service Fuse --> Server: A service timed out or abnormal, causing a fuse ~, similar to a fuse (self-fuse)
  • Service downgrade - > Client: Considering the load requested from the overall website, when a service is broken or shut down, the service will no longer be invoked. At this time, on the client side, we can prepare a FallBackFactory and return a default value (default value).This can cause overall service degradation, but it's better to use it for good than to hang it up directly.
  • The trigger causes are different. Service outage is usually caused by a service (downstream service) failure, while service downgrade is generally considered from the overall load;The hierarchy of management objectives is different. Fusion is actually a framework-level process, with each microservice requiring (no hierarchy) and downgrading typically requiring a hierarchy of businesses (for example, downgrading typically starts with the most peripheral service).
  • The implementation is different, and service downgrades are code-invasive (completed by the controller or automatically downgraded), with a fuse commonly referred to as self-fusing.

Fuse, Downgrade, Limit Current:

Limit flow: Limit concurrent request access and reject when exceeding the threshold;

Demotion: Services are prioritized at the expense of non-core services (unavailable) to ensure the stability of core services;Considering the overall load;

Fuse: Dependent downstream service failures trigger the fuse to avoid causing the system crash;System Auto Execution and Recovery

8.7 Dashboard Stream Monitoring

New springcloud-consumer-hystrix-dashboard module

Add Dependency

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#708090 "><!--Hystrix Dependency --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-hystrix<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#708090 "><!--Dashboard dependency --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-hystrix-dashboard<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#708090"><!--Ribbon--></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-ribbon<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#708090"><!--Eureka--></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-eureka<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#708090 "><!--Entity Class+web--></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.haust<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>springcloud-api<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.0-SNAPSHOT<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#708090 "><!--Hot Deployment --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-devtools<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Main Startup Class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@SpringBootApplication</span>
<span style="color:#708090 ">//Open Dashboard</span>
<span style="color:#999999">@EnableHystrixDashboard</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> DeptConsumerDashboard_9001 <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>DeptConsumerDashboard_9001<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Add the following code to the main startup class under the springcloud-provider-dept-hystrix-8001 module to add monitoring

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@SpringBootApplication</span>
<span style="color:#999999">@EnableEurekaClient</span> <span style="color:#708090 ">//EnableEurekaClient client startup class, automatically registers service </span>with the registry after service startup
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> DeptProvider_8001 <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>DeptProvider_8001<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#708090 ">//Add a Servlet</span>
    <span style="color:#999999">@Bean</span>
    <span style="color:#0077aa">public</span> ServletRegistrationBean <span style="color:#dd4a68">hystrixMetricsStreamServlet</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        ServletRegistrationBean registrationBean <span style="color:#a67f59">=</span> <span style="color:#0077aa">new</span> ServletRegistrationBean<span style="color:#999999">(</span><span style="color:#0077aa">new</span> HystrixMetricsStreamServlet<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#708090 ">//Visit this page is the monitoring page </span>
        registrationBean<span style="color:#999999">.</span><span style="color:#dd4a68">addUrlMappings</span><span style="color:#999999">(</span><span style="color:#50a14f">"/actuator/hystrix.stream"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
       
        <span style="color:#0077aa">return</span> registrationBean<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Visit:http://localhost:9001/hystrix

Enter the monitoring page:

The effect is as follows:

9. Zull Routing Gateway

Summary

What is zuul?

Zull contains two main functions: routing the request (for jumping) and filtering:

The routing function is responsible for forwarding external requests to specific micro-service instances, which is the basis for achieving a unified entry for external access, while the filter function is responsible for intervening in the processing of requests, which is the basis for implementing such functions as request verification, service aggregation and so on.Zuul integrates with Eureka, registering Zuul as an application under Eureka service governance, and getting messages from other services in Eureka, i.e. subsequent access to microservices, is achieved through a Jump from Zuul.

Note: The Zuul service will eventually be registered with Eureka

Provides: Proxy + Routing + Filtering Three functions!

What can Zuul do?

  • Route
  • filter

Official documents:https://github.com/Netflix/zuul/

Getting Started Cases

Create a new springcloud-zuul module and import dependencies

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependencies</span><span style="color:#999999">></span></span>
    <span style="color:#708090 "><!--Import zuul dependencies --></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-zuul<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090 "><!--Hystrix Dependency --></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-hystrix<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090 "><!--Dashboard dependency --></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-hystrix-dashboar<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090"><!--Ribbon--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-ribbon<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090"><!--Eureka--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-eureka<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090 "><!--Entity Class+web--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.haust<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>springcloud-api<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.0-SNAPSHOT<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090 "><!--Hot Deployment --></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-devtools<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependencies</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

application.yml

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#0077aa">server</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">port</span><span style="color:#999999">:</span> <span style="color:#986801">9527</span>

<span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">application</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">name</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>zuul <span style="color:#708090">#Microservice name </span>

<span style="color:#708090"># eureka Registry Configuration </span>
<span style="color:#0077aa">eureka</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">client</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">service-url</span><span style="color:#999999">:</span>
      <span style="color:#0077aa">defaultZone</span><span style="color:#999999">:</span> http<span style="color:#999999">:</span>//eureka7001.com<span style="color:#999999">:</span>7001/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7002.com<span style="color:#999999">:</span>7002/eureka/<span style="color:#999999">,</span>http<span style="color:#999999">:</span>//eureka7003.com<span style="color:#999999">:</span>7003/eureka/
  <span style="color:#0077aa">instance</span><span style="color:#999999">:</span> <span style="color:#708090">#Instance id</span>
    <span style="color:#0077aa">instance-id</span><span style="color:#999999">:</span> zuul9527.com
    <span style="color:#0077aa">prefer-ip-address</span><span style="color:#999999">:</span> <span style="color:#ee9900">true </span><span style="color:#708090"># Show ip</span>

<span style="color:#0077aa">info</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">app.name</span><span style="color:#999999">:</span> haust.springcloud <span style="color:#708090"># Project Name </span>
  <span style="color:#0077aa">company.name</span><span style="color:#999999">:</span> Xiyuan Campus of Henan University of Science and Technology <span style="color:#708090"># Company name </span>

<span style="color:#708090"># zull routing gateway configuration </span>
<span style="color:#0077aa">zuul</span><span style="color:#999999">:</span>
  <span style="color:#708090"># Routing-related configuration </span>
  <span style="color:#708090"># Original access route eg:http://www.cspStudy.com:9527/springcloud-provider-dept/dept/get/1</span>
  <span style="color:#708090"># zull route configuration access route eg:http://www.cspstudy.com:9527/haust/mydept/dept/get/1</span>
  <span style="color:#0077aa">routes</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">mydept.serviceId</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>provider<span style="color:#999999">-</span>dept <span style="color:#708090"># Service Provider Route Name </span>in eureka Registry
    <span style="color:#0077aa">mydept.path</span><span style="color:#999999">:</span> /mydept/** <span style="color:#708090"># Change service provider routing name of eureka registry to custom routing name </span>
  <span style="color:#708090"># No longer accessible using this path, *: Ignore, hide all service names ~</span>
  <span style="color:#0077aa">ignored-services</span><span style="color:#999999">:</span> <span style="color:#50a14f">"*"</span>
  <span style="color:#708090"># Set common prefix </span>
  <span style="color:#0077aa">prefix</span><span style="color:#999999">:</span> /haust
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

Main Startup Class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#708090">/**
 * @Auther: csp1999
 * @Date: 2020/05/20/20:53
 * @Description: Zull Routing Gateway Primary Startup Class
 */</span>
<span style="color:#999999">@SpringBootApplication</span>
<span style="color:#999999">@EnableZuulProxy</span> <span style="color:#708090 ">//Turn on Zuul</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> ZuulApplication_9527 <span style="color:#999999">{</span>

    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>ZuulApplication_9527<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Test:

You can see that the Zull Routing Gateway is registered with the Eureka Registry!

The above figure shows the route accessed by the service interface without the Zull routing gateway configuration. You can see that it is not safe to access the microservice (service provider) name directly. You cannot expose the microservice name!

So after the Zull routing gateway is configured, the routes accessed are:

We see that the microservice name is replaced and hidden, replaced by our custom microservice name mydept, with the prefix haust, which makes it possible to encrypt routing fan access!

Refer to the Springcloud Chinese Community zuul component for details:https://www.springcloud.cc/spring-cloud-greenwich.html#_router_and_filter_zuul

10. Spring Cloud Config Distributed Configuration

Dalston.RELEASE

Spring Cloud Config provides server and client support for external configuration in distributed systems.With Config Server, you can manage the external properties of your application in all environments.Conceptual Mapping and Spring on Client and Server Environment and ProertySource are abstract, so they fit well with Spring applications, but can be used with any application that runs in any language.As applications go through the deployment process from developers to testing and production, you can manage the configuration between these environments and determine that the application has everything it needs to run when migrating.The default implementation of the server storage backend uses git, so it easily supports the label version's configuration environment and accesses various tools for managing content.It is easy to add alternative implementations and insert them using the Spring configuration.

Summary

Configuration file issues facing distributed systems

Micro-services mean dividing the business in a single application into individual sub-services, each of which has a relatively small granularity, so there will be a large number of services in the system. Because each service needs the necessary configuration information to run, a centralized and dynamic configuration management facility is essential.Sprcloud provides configServer to solve this problem. Each of our micro-services carries its own application.yml, and hundreds of configuration files have been modified, which is a headache!

What is the SpringCloud config Distributed Configuration Center?

The spring cloud config provides centralized external support for microservices in the microservice architecture, and the configuration server provides a centralized external configuration for all aspects of different microservice applications.

The spring cloud config is divided into two parts, the service side and the client side.

The server side is also known as Distributed Configuration Center, a stand-alone micro-service application, connects configuration servers and provides clients with access interfaces to obtain configuration information, encrypt and decrypt information.

Clients manage application resources and business-related configuration content through a designated configuration center and obtain and load configuration information from the configuration center at startup.Configuration servers use git by default to store configuration information, which helps with versioning of environmental configurations.Configuration content can be easily managed and accessed through git client tools.

What can the spring cloud config Distributed Configuration Center do?

  • Centrally Manage Profiles
  • Different environments, different configurations, dynamic configuration updates, sub-environment deployments, such as/dev/test/prod/beta/release
  • Configuration is dynamically adjusted during runtime, and configuration files no longer need to be written on the machines where each service is deployed. Services pull configuration information from the configuration center.
  • When a configuration changes, the service does not need to restart to be aware of the configuration changes and apply the new configuration
  • Exposing configuration information as a REST interface

Integration of spring cloud config Distributed Configuration Center with GitHub

Since spring cloud config uses git by default to store configuration files (there are other ways, such as self-contained SVN and local files), Git is the most recommended and is accessed in the form of http / https.

Getting Started Cases

Server

New springcloud-config-server-3344 module imports pom.xml dependencies

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependencies</span><span style="color:#999999">></span></span>
    <span style="color:#708090"><!--web--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090"><!--config--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-config-server<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.1.1.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#708090"><!--eureka--></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-eureka<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.4.6.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependencies</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

The application.yml configuration file is created under resource, and the Spring Cloud Config server provides configuration for remote clients from the git repository (which must be provided):

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#0077aa">server</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">port</span><span style="color:#999999">:</span> <span style="color:#986801">3344</span>

<span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">application</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">name</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>config<span style="color:#999999">-</span>server
  <span style="color:#708090"># Connection code cloud remote warehouse </span>
  <span style="color:#0077aa">cloud</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">config</span><span style="color:#999999">:</span>
      <span style="color:#0077aa">server</span><span style="color:#999999">:</span>
        <span style="color:#0077aa">git</span><span style="color:#999999">:</span>
          <span style="color:#708090"># Note that it is https, not ssh</span>
          <span style="color:#0077aa">uri</span><span style="color:#999999">:</span> https<span style="color:#999999">:</span>//gitee.com/cao_shi_peng/springcloud<span style="color:#999999">-</span>config.git 
            <span style="color:#708090"># You can connect to git via config-server, access its resources, and configure ~</span>

<span style="color:#708090"># Cannot execute request on any known server without this configuration: the connection Eureka server address is incorrect </span>
<span style="color:#708090"># Or simply comment out the Eureka dependency and temporarily use no eureka</span>
<span style="color:#0077aa">eureka</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">client</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">register-with-eureka</span><span style="color:#999999">:</span> <span style="color:#ee9900">false</span>
    <span style="color:#0077aa">fetch-registry</span><span style="color:#999999">:</span> <span style="color:#ee9900">false</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Main Startup Class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@EnableConfigServer</span> <span style="color:#708090 ">//Open the spring cloud config server service </span>
<span style="color:#999999">@SpringBootApplication</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> Config_server_3344 <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>Config_server_3344<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Submit the new application.yml created under the springcloud-config folder of the local git warehouse to the code cloud warehouse:

The default strategy for locating resources is to clone a git repository (in spring.cloud.config.server.git.uri) and use it to initialize a mini SpringApplication.Environment s for small applications are used to enumerate property sources and publish them through JSON endpoints.

HTTP services have resources in the following formats:

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-json"><span style="color:#a67f59">/</span><span style="color:#999999">{</span>application<span style="color:#999999">}</span><span style="color:#a67f59">/</span><span style="color:#999999">{</span>profile<span style="color:#999999">}</span><span style="color:#999999">[</span><span style="color:#a67f59">/</span><span style="color:#999999">{</span>label<span style="color:#999999">}</span><span style="color:#999999">]</span>
<span style="color:#a67f59">/</span><span style="color:#999999">{</span>application<span style="color:#999999">}</span><span style="color:#a67f59">-</span><span style="color:#999999">{</span>profile<span style="color:#999999">}</span><span style="color:#999999">.</span>yml
<span style="color:#a67f59">/</span><span style="color:#999999">{</span>label<span style="color:#999999">}</span><span style="color:#a67f59">/</span><span style="color:#999999">{</span>application<span style="color:#999999">}</span><span style="color:#a67f59">-</span><span style="color:#999999">{</span>profile<span style="color:#999999">}</span><span style="color:#999999">.</span>yml
<span style="color:#a67f59">/</span><span style="color:#999999">{</span>application<span style="color:#999999">}</span><span style="color:#a67f59">-</span><span style="color:#999999">{</span>profile<span style="color:#999999">}</span><span style="color:#999999">.</span>properties
<span style="color:#a67f59">/</span><span style="color:#999999">{</span>label<span style="color:#999999">}</span><span style="color:#a67f59">/</span><span style="color:#999999">{</span>application<span style="color:#999999">}</span><span style="color:#a67f59">-</span><span style="color:#999999">{</span>profile<span style="color:#999999">}</span><span style="color:#999999">.</span>properties
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5

Where "Application" is injected as spring.config.name in Spring Application (i.e., usually "Application" in a regular Spring Boot application), "Configuration File" is an active configuration file (or a property of a comma-separated list), and "label" is an optional git tag (default is "master").

Test Accesshttp://localhost:3344/application-dev.yml

Test Accesshttp://localhost:3344/application/test/master

Test Accesshttp://localhost:3344/master/application-dev.yml

Configurations that do not exist for test access are not displayed such as:http://localhost:3344/master/application-aaa.yml

Client

Submit the new config-client.yml created under the springcloud-config folder of the local git warehouse to the code cloud warehouse:

Create a new springcloud-config-client-3355 module and import dependencies

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#708090"><!--config--></span>
<span style="color:#708090"><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-start --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-config<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.1.1.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-actuator<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Create application.yml and bootstrap.yml configuration files under resources

bootstrap.yml Is a system level configuration

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#708090"># System level configuration </span>
<span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">cloud</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">config</span><span style="color:#999999">:</span>
      <span style="color:#0077aa">name</span><span style="color:#999999">:</span> config<span style="color:#999999">-</span>client <span style="color:#708090"># Name of resource to read from git, do not suffix </span>
      <span style="color:#0077aa">profile</span><span style="color:#999999">:</span> dev
      <span style="color:#0077aa">label</span><span style="color:#999999">:</span> master
      <span style="color:#0077aa">uri</span><span style="color:#999999">:</span> http<span style="color:#999999">:</span>//localhost<span style="color:#999999">:</span><span style="color:#986801">3344</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

application.yml Is User Level Configuration

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#708090"># User level configuration </span>
<span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">application</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">name</span><span style="color:#999999">:</span> springcloud<span style="color:#999999">-</span>config<span style="color:#999999">-</span>client
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4

Create ConfigClientController.java under the controller package For testing

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@RestController</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> ConfigClientController <span style="color:#999999">{</span>

    <span style="color:#999999">@Value</span><span style="color:#999999">(</span><span style="color:#50a14f">"${spring.application.name}"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">private</span> String applicationName<span style="color:#999999">;</span> <span style="color:#708090 ">//Get Microservice Name </span>

    <span style="color:#999999">@Value</span><span style="color:#999999">(</span><span style="color:#50a14f">"${eureka.client.service-url.defaultZone}"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">private</span> String eurekaServer<span style="color:#999999">;</span> <span style="color:#708090 ">//Get Eureka Service </span>

    <span style="color:#999999">@Value</span><span style="color:#999999">(</span><span style="color:#50a14f">"${server.port}"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">private</span> String port<span style="color:#999999">;</span> <span style="color:#708090 ">//Get the port number of the server </span>


    <span style="color:#999999">@RequestMapping</span><span style="color:#999999">(</span><span style="color:#50a14f">"/config"</span><span style="color:#999999">)</span>
    <span style="color:#0077aa">public</span> String <span style="color:#dd4a68">getConfig</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#0077aa">return</span> <span style="color:#50a14f">"applicationName:"</span><span style="color:#a67f59">+</span>applicationName <span style="color:#a67f59">+</span>
         <span style="color:#50a14f">"eurekaServer:"</span><span style="color:#a67f59">+</span>eurekaServer <span style="color:#a67f59">+</span>
         <span style="color:#50a14f">"port:"</span><span style="color:#a67f59">+</span>port<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Main Startup Class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@SpringBootApplication</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> ConfigClient <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>ConfigClient<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Test:

Start Server Config_server_3344 Reboot Client ConfigClient

Visit:http://localhost:8201/config/

Small Case

Locally create new config-dept.yml and config-eureka.yml and submit them to the code cloud warehouse

The content of the configuration file is not enumerated here and goes directly to the code.

Create a new springcloud-config-eureka-7001 module and copy the contents of the original springcloud-eureka-7001 module.

1. Empty the application.yml configuration for the module and create a new bootstrap.yml connection remote configuration

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-yml"><span style="color:#0077aa">spring</span><span style="color:#999999">:</span>
  <span style="color:#0077aa">cloud</span><span style="color:#999999">:</span>
    <span style="color:#0077aa">config</span><span style="color:#999999">:</span>
      <span style="color:#0077aa">name</span><span style="color:#999999">:</span> config<span style="color:#999999">-</span>eureka <span style="color:#708090"># Configuration file name in warehouse </span>
      <span style="color:#0077aa">label</span><span style="color:#999999">:</span> master
      <span style="color:#0077aa">profile</span><span style="color:#999999">:</span> dev
      <span style="color:#0077aa">uri</span><span style="color:#999999">:</span> http<span style="color:#999999">:</span>//localhost<span style="color:#999999">:</span><span style="color:#986801">3344</span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Add a spring cloud config dependency to pom.xml

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#708090"><!--config--></span>
<span style="color:#708090"><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config --></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.cloud<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-cloud-starter-config<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.1.1.RELEASE<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span></span></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3. Main Startup Class

<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@SpringBootApplication</span>
<span style="color:#999999">@EnableEurekaServer</span> <span style="color:#708090'>//EnableEurekaServer server startup class that can be registered by others ~</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> ConfigEurekaServer_7001 <span style="color:#999999">{</span>
    <span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>ConfigEurekaServer_7001<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span>args<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span></span></span>

4. Testing

Step 1: Start Config_Server_3344, and visit http://localhost:3344/master/config-eureka-dev.yml test


Part 2: Start ConfigEurekaServer_7001, Access http://localhost:7001/ test


Show the above image successfully

Create a new springcloud-config-dept-8001 module and copy the contents of springcloud-provider-dept-8001

Similarly import the spring cloud config dependency, empty the application.yml, create a new bootstrap.yml configuration file, and configure

spring:
  cloud:
    config:
      name: config-dept
      label: master
      profile: dev
      uri: http://localhost:3344

Main Startup Class

@SpringBootApplication
@EnableEurekaClient //Automatically register in Eureka after service startup!
@EnableDiscoveryClient //Service Discovery~
@EnableCircuitBreaker //
public class ConfigDeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigDeptProvider_8001.class,args);
    }

    //Add a Servlet
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
}

Test (omitted)

Posted by auro on Thu, 02 Sep 2021 15:30:25 -0700