Construction of multiple spring-cloud provider poll

Yesterday I did a Eureka-Server cluster registry, then we know that in a distributed architecture, the biggest feature is a micro-services can be run independently, each module has its own independent database

Today, we look at how to set up multiple providers, meaning that in order to reduce the pressure on the database, we can create more of the same provider, and create multiple databases (storing the same data)

Same polling access to multiple providers through consumer interface load balancing strategy

How to build a eureka service registration and discovery, you can read my previous blog, as well as Eureka Server Clusters

The following step look at today's function

1, first create two identical default provider

 

2, the internal business logic code can be directly copy paste, comprising pom.xml file dependent, MyBatis global configuration file, map file Mapper

3, the file will be copied yml pay attention to change the current database information providers need, as well as the corresponding port number

Data stored in the database should be identical, but here, I have another piece of data changes to the database in order to facilitate post-demo

Data in the table tx

Data users table

# A provider before the 
Server: 
    Port: 8001 
#mybatis setting 
the mybatis: 
    config -location: the CLASSPATH: the mybatis / mybatis.cfg.xml # path where the mybatis profile 
    of the type -aliases- Package Penalty for : the relationship com.ghh.bean # mapping scan all aliases bean class where the package 
    Mapper -locations: the CLASSPATH: the mybatis / Mapper / ** /*.xml # Mapper mapping file 
the Spring: 
    the Application: name: microService-Product # this is very important, that after the service with the service usually call each other according to the name 
      micro-leak service name # Foreign Newspapers in field service registry value under application, eureka will automatically become all uppercase 
    the DataSource:
       
        of the type: com.alibaba.druid.pool.DruidDataSource # current data source operation type
        driver-class-name: com.mysql.cj.jdbc.Driver # mysql driver package 
        url: jdbc: MySQL: //127.0.0.1: 3306 / the Users? serverTimezone% 2B8 = GMT # database name 
        username: root 
        password: root 
        dbcp2 : 
            min-IDLE:. 5 # database connection pool minimum maintain continuous 
            initial-size: 5 # initialization connections 
            max-total: 5 # maximum number of connections 
            max-wait-millis: 150 # waiting for connections to obtain the maximum timeout 
eureka: 
  instance: 
    instance-the above mentioned id: $ {spring.application.name}: $ {#} server.port expose my name and port number 
    prefer-ip-address: true # access path to display ip 

  Client: 
    the Register-with-Eureka: to true # service registration true registration to the service center 
    fetchRegistry: true # true for registration information
    service-url: # registered to a designated registration center or cluster 
      defaultzone: http://Eureka7001.com : 7001 / Eureka /,http://Eureka6001.com : 6001 / Eureka / 

# copied yml change the

server:
    port: 8002 # modify the current port number of the provider 
setting #mybatis of
the mybatis:
config-LOCATION: the CLASSPATH: the mybatis / mybatis.cfg.xml # configuration file path where the mybatis
type-aliases-package: com.ghh.bean # through the relationship mapping scan all aliases bean class where the package
mapper-locations: classpath:. mybatis / mapper / ** / * xml # mapper mapping file
the Spring:
the Application:
name: microService-Product # this is very important, that after the service with the service usually call each other according to the name
micro-leak service name # Foreign Newspapers in field service registry value under application, eureka will automatically become all uppercase
the DataSource:
of the type: com.alibaba.druid.pool.DruidDataSource # current data source operation type
driver-class-name: com.mysql.cj.jdbc.Driver # mysql driver package
URL: JDBC: MySQL: //127.0.0.1: 3306 / TX? serverTimezone GMT =% # 2B8 database name
username: the root
password: the root
dbcp2:
min-IDLE:. 5 # database connection pool minimum maintain continuous
initial-size: 5 # initialization connections
max-total: 5 # maximum number of connections
max-wait-millis: 150 # waiting for a connection get the maximum timeout
Eureka:
instance:
instance-the above mentioned id: microserivce_cloud_07_provide_product_8002
the prefer-ip-address: true # access paths can display ip
Client:
the Register-with-Eureka: true # service registration true registration to the service center
fetchRegistry: true # true acquisition registration information
service-url: # registered to a designated registration center or cluster
defaultZone: http://Eureka7001.com:7001/eureka/,http://Eureka6001.com:6001/eureka/

 

4. After modifying yml file, the consumer module, we need to modify the configuration class before, open load balancing, the default polling way to access different providers

  Here must remember not to forget @LoadBalanced notes,

@Configuration
 public  class RestConfig { 

    @Bean     // achieved via call interface RestTemplate 
   @LoadBalanced      // represents RestTemplate open load balancing 
    public RestTemplate getRestTemplate () {
         return   new new RestTemplate (); 
    } 
}

5, the front spring we have configured: application: name:'ll find two providers are the same, this configuration is the current module name exposed to other modules,

 

 After this time the server will see the start of a multi-service center for this micro-services, and there are two behind, Eureka default will change our name in uppercase

note:

  Remember that we are here in yml file MICROSERVICE-PRODUCT can not be written MICROSERVICE_PRODUCT, underlined Eureka is not supported

6, this time the equivalent of two identical functionality providers are registered, we call the consumer interfaces RestTemplate

 

7, the effect 

 

 

 

 

8 tips when we need information elsewhere, may be obtained by $ {} in the configuration, so then we'll modify the time, as long as the modification of the above information on the line

 

 

 Summary: Polling way to ease the pressure in the final analysis tomcat and mysql, and our future development projects distributed architecture, the various micro-services, will not be in the same tomcat, the realization

Completely decoupling, so Eureka through a service registry, each micro service after starting, to which to register, go to Server heartbeat monitor each micro-services, access to a greater amount of local

The use of multiple providers, multiple databases, and in the way consumers interface uses polling to alleviate tomcat pressure.

 

Work together Kazakhstan, the joint efforts of progress

Guess you like

Origin www.cnblogs.com/guanyuehao0107/p/11819375.html