The springcloud project (eureka service discovery + zuul gateway) is deployed to tomcat in docker

It is the first time to deploy springcloud to the test server docker in recent days. The following are some deployment tips and some error solutions, so as not to forget.

1. First, the springcloud project needs to be modified to deploy to tomcat:

       1. Modify the tomcat import in the pom file and add the jar package of the servlet. In addition, if there is a reference to jetty in the pom file, comment or delete it. Modify the packaging type to war package.

       2. The startup class of Application needs to be modified. If you use the @ComponentScan annotation, it is recommended to modify the application startup class directly.

       3. If there is no database link in the project, the @SpringBootApplication of the application startup class should add parameters, namely @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

      After completing these 3 points, you can basically package and deploy.

      details as follows:

      1. Since tomcat is built in spring-boot-start-web, you need to exclude the tomcat reference and add the servlet jar package

<dependency>  
         <groupId>org.springframework.boot</groupId>  
       <artifactId>spring-boot-starter-web</artifactId>  
         <exclusions>  
             <exclusion>  
               <groupId>org.springframework.boot</groupId>  
               <artifactId>spring-boot-starter-tomcat</artifactId>  
             </exclusion>  
         </exclusions>  
    </dependency>  
    <dependency>  
           <groupId>javax.servlet</groupId>  
           <artifactId>javax.servlet-api</artifactId>  
        </dependency> 

           If there is a reference to jetty, you need to comment or delete it.

      2. Application startup class, which can directly inherit SpringBootServletInitializer and override the configure method

@SpringBootApplication
@EnableEurekaServer
public class UserApplication extends SpringBootServletInitializer{



@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(UserApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}

     3. If the database link is not configured in the project, you need to add the @SpringBootApplication parameter, namely

       @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableEurekaServer
public class DiscoveryApplication extends SpringBootServletInitializer{



@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DiscoveryApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}

        At this point, the project can be packaged and deployed to tomcat.

Second, the eureka service discovery deployment requires special attention to the access address

       During the development process, the eureka-service address is generally (taking 8001 as an example): http://localhost:8001/eureka, and in the eureka-client configuration file: eureka.client.serviceUrl.defaultZone=http://localhost:8001 /eureka, if spring.application.name in the eureka-service configuration is not empty (take discovery as an example), that is, spring.application.name=discovery, then the eureka.client.serviceUrl.defaultZone path of eureka-client should be configured as: http ://localhost:8001/discovery/eureka.

That is, eureka-service is configured as:

spring.application.name=discovery
server.port=8001
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka

   The eureka-client configuration should be:

  spring.cloud.config.discovery.enabled=true
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:8001
/discovery/eureka

At this point, eureka service discovery can discover services.


Three, pay attention to the configuration when deploying the zuul project

server.port=8000
server.contextPath=/mall
spring.application.name=api
eureka.client.serviceUrl.defaultZone=http://localhost:8001/discovery/eureka

endpoints.restart.enabled=true
endpoints.shutdown.enabled=true

spring.zipkin.enabled=false

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds= 10000
ribbon.ConnectTimeout= 1000000
ribbon.ReadTimeout= 1000000

ribbon.MaxAutoRetries= 0
ribbon.MaxAutoRetriesNextServer= 0
ribbon.OkToRetryOnAllOperations= false

eureka.instance.prefer-ip-address=true

zuul.semaphore.max-semaphores=10000
zuul.host.maxTotalConnections=10000
zuul.host.max-per-route-connections=1000

# routes to serviceId
zuul.routes.user-srv.path=/user/**
zuul.routes.user-srv.service-id=user

zuul.routes.user-srv.stripPrefix=false

  If the server.contextPath is used in the configuration, there is no problem in development, but there will be problems when deployed to tomcat. It is recommended to directly remove the server.contextPath configuration.

See also the information

  1. #When stripPrefix=true (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list)  
  2. #When stripPrefix=false (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list)
    (Access to information: http://www.cnblogs.com/xd03122049/p/6036318.html)
     But it does not work in my project. In the project, zuul.routes.user-srv.stripPrefix=false must be added, otherwise it will not be accessible.

        

Four, docker deployment project

First install docker and pull tomcat image.

Secondly, create a folder to place the war package on the server, and assign the address to the webapps directory of the docker tomcat instance, so that the project will start

The specific command is as follows: docker run --name user -p 8020:8080 -v /Java/webapp/user/:/usr/local/tomcat/webapps -d tomcat

Where --name is the name of this instance, you can use it to operate the instance, view logs, restart, etc.

-p is the designated server port corresponding to the docker instance port, the front is the server port, the back is the docker tomcat port

-v specifies the address of the tomcat corresponding to the local war package address. The one in front of the server folder is the docker.tomcat folder.

        -d specifies the mirror to start, if there are more mirrors, you can use -d tomcat; version number specify the version number without adding the latest version by default




Five, common mistakes:

1. The project configuration links the data source, the annotation of Application needs to be changed to @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

Can solve this problem

Error starting ApplicationContext. Todisplay the auto-configuration report re-run your application with 'debug'enabled.

2017-08-08 02:04:40.102 ERROR 1 ---[ost-startStop-1] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************

APPLICATION FAILED TO START

***************************

Description:

Cannot determine embedded database driverclass for database type NONE

Action:

If you want an embedded database please puta supported one on the classpath. If you have database settings to be loadedfrom a particular profile you may need to active it (the profiles"native" are currently active).

08-Aug-2017 02:04:40.102 SEVERE[localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternalContainerBase.addChild: start:

 org.apache.catalina.LifecycleException: Failedto start component[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/config-srv]]

       atorg.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)

       atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)

       atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)

       atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)

       atorg.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:988)

       atorg.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1860)

       atjava.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

       atjava.util.concurrent.FutureTask.run(FutureTask.java:266)

       atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

       atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

       atjava.lang.Thread.run(Thread.java:748)

2. The service found zuul problems, you can refer to the second and third pro-tests.

3. The data source conversion is abnormal. This is how I replaced the HikariDataSource data source with the DataSource data source.

java.lang.ClassCastException: org.apache.tomcat.jdbc.pool.DataSource cannot be cast to com.zaxxer.hikari.HikariDataSource



The first time you use docker to deploy, the first time you deploy the springcloud project will inevitably make many detours. I hope to help everyone avoid detours. The technical level is limited and the experience is insufficient. It is inevitable that there are inappropriate places. Welcome to correct me.

Guess you like

Origin blog.csdn.net/litianquan/article/details/80551252