版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35559756/article/details/80919893
搭建spring cloud2.0基础框架
推荐使用idea作为ide,使用git作为版本管理工具,在idea中安装eclipse code fomatter后配置代码格式化工具,业务框架集成dev-tool做为热启动工具,推荐使用yml作为配置文件格式,更直观简洁
由于是spring cloud2,如果感觉下载依赖缓慢,建议修改一下maven的代理,添加一下到原来的<mirros>
节点中,如下:
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<!--This is used to direct the public snapshots repo in the
profile below over to a different nexus group -->
<id>nexus-public-snapshots</id>
<mirrorOf>public-snapshots</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
</mirror>
<mirror>
<id>UK</id>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>sonatype</id>
<name>sonatype Central</name>
<url>http://repository.sonatype.org/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
高可用的注册中心
首先,你需要看到主要依赖是以下:
<!--eureka server注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
pom.xml文件中的书写格式推荐类似如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tristan</groupId>
<artifactId>registry-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>registry-server</name>
<description>注册中心服务器</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 将版本信息抽出来方便以后进行调整-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--eureka server注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version><!-- 动态引用版本号-->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后,需要配置配置文件,如下:
---
server:
port: 8771
spring:
profiles: peer1
application:
name: registry-server
eureka:
instance:
hostname: peer1
lease-expiration-duration-in-seconds: 15 # 剔除注册记录的最大容忍失联时间
lease-renewal-interval-in-seconds: 5 # 注册中心主动心跳的时间间隔
client:
serviceUrl:
defaultZone: http://localhost:8772/eureka/,http://localhost:8773/eureka/
server:
response-cache-update-interval-ms: 3000 # 从注册可用到真正可用的时间
response-cache-auto-expiration-in-seconds: 180 # 注册方注册保障的有效时间或者失效时间
eviction-interval-timer-in-ms: 3000 # 注册中心主动监测失效的时间间隔
# enable-self-preservation: false # 关闭自我保护
---
server:
port: 8772
spring:
profiles: peer2
application:
name: registry-server
eureka:
instance:
hostname: peer2
lease-expiration-duration-in-seconds: 15 # 剔除注册记录的最大容忍失联时间
lease-renewal-interval-in-seconds: 5 # 注册中心主动心跳的时间间隔
client:
serviceUrl:
defaultZone: http://localhost:8771/eureka/,http://localhost:8773/eureka/
server:
response-cache-update-interval-ms: 3000 # 从注册可用到真正可用的时间
response-cache-auto-expiration-in-seconds: 180 # 注册方注册保障的有效时间或者失效时间
eviction-interval-timer-in-ms: 3000 # 注册中心主动监测失效的时间间隔
# enable-self-preservation: false # 关闭自我保护
---
server:
port: 8773
spring:
profiles: peer3
application:
name: registry-server
eureka:
instance:
hostname: peer3
lease-expiration-duration-in-seconds: 15 # 剔除注册记录的最大容忍失联时间
lease-renewal-interval-in-seconds: 5 # 注册中心主动心跳的时间间隔
client:
serviceUrl:
defaultZone: http://localhost:8771/eureka/,http://localhost:8772/eureka/
server:
response-cache-update-interval-ms: 3000 # 从注册可用到真正可用的时间
response-cache-auto-expiration-in-seconds: 180 # 注册方注册保障的有效时间或者失效时间
eviction-interval-timer-in-ms: 3000 # 注册中心主动监测失效的时间间隔
# enable-self-preservation: false # 关闭自我保护
注册中心只是作为高可用即可,无需特别多台,当一台注册中心宕机时,后台hock设置重启即可,同时其他服务的注册由于同等配置可随时切入。
由于多台互相注册所以连入任何一台都能看到相同的服务状态注册列表
通过springBoot方式启动工程,RegistryServerApplication:
package com.tristan.registryserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //如果不加该注解或者在配置文件中配置则会导致启动失败,不保持启动状态,后台持续报错,访问eureka界面提示404
public class RegistryServerApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryServerApplication.class, args);
}
}
成功启动之后的日志:(开始时的报错为正常现象)
2018-07-05 22:03:08.038 INFO 1280 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8771 (http) with context path ''
2018-07-05 22:03:08.039 INFO 1280 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8771
2018-07-05 22:03:08.041 INFO 1280 --- [ main] c.t.r.RegistryServerApplication : Started RegistryServerApplication in 14.37 seconds (JVM running for 15.419)
2018-07-05 22:03:08.560 INFO 1280 --- [nio-8771-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-05 22:03:08.560 INFO 1280 --- [nio-8771-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-05 22:03:08.579 INFO 1280 --- [nio-8771-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
2018-07-05 22:03:09.176 INFO 1280 --- [nio-8771-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8773 with status UP (replication=false)
2018-07-05 22:03:09.312 INFO 1280 --- [nio-8771-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8772 with status UP (replication=false)
2018-07-05 22:03:09.783 INFO 1280 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_REGISTRY-SERVER/192.168.56.1:registry-server:8771 - registration status: 204
2018-07-05 22:03:10.284 INFO 1280 --- [nio-8771-exec-4] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8771 with status UP (replication=true)
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-07-05 22:03:37.650 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-07-05 22:03:37.666 INFO 1280 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-07-05 22:03:38.016 INFO 1280 --- [ Thread-42] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8771 with status UP (replication=true)
2018-07-05 22:03:38.016 INFO 1280 --- [ Thread-42] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8772 with status UP (replication=true)
2018-07-05 22:03:38.016 INFO 1280 --- [ Thread-42] c.n.e.registry.AbstractInstanceRegistry : Registered instance REGISTRY-SERVER/192.168.56.1:registry-server:8773 with status UP (replication=true)
2018-07-05 22:03:38.017 INFO 1280 --- [ Thread-42] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 3 instances from neighboring DS node
2018-07-05 22:03:38.017 INFO 1280 --- [ Thread-42] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 5
2018-07-05 22:03:38.017 INFO 1280 --- [ Thread-42] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2018-07-05 22:03:38.020 INFO 1280 --- [ Thread-42] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2018-07-05 22:03:41.018 INFO 1280 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
子服务进行注册
首先,配置依赖(注意:普通子服务需要web依赖才能正常启动):
<!--web依赖,服务提供方必备-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服务发现组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--dev-tool热启动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!--基于springBoot容器测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
然后,配置配置文件:
扫描二维码关注公众号,回复:
2909051 查看本文章
application.yml
# 服务注册方的模板配置
server:
port: 8783
spring:
application:
name: template-service
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8773/eureka/,http://127.0.0.1:8772/eureka/,http://127.0.0.1:8771/eureka/
然后,编写启动类:
package com.tristan.templateservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class TemplateServiceApplication {
public static void main(String[] args) {
SpringApplication.run(TemplateServiceApplication.class, args);
}
}
成功启动的日志如下:
2018-07-05 22:47:49.156 INFO 21580 --- [ restartedMain] c.t.t.TemplateServiceApplication : No active profile set, falling back to default profiles: default
2018-07-05 22:47:49.168 INFO 21580 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1dc6e5e: startup date [Thu Jul 05 22:47:49 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@447e08e5
2018-07-05 22:47:50.512 INFO 21580 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=19f23c04-92cb-31ca-bef6-d6dcc3a8c6db
2018-07-05 22:47:50.524 INFO 21580 --- [ restartedMain] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-07-05 22:47:50.604 INFO 21580 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$6437797a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-07-05 22:47:50.957 INFO 21580 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8783 (http)
2018-07-05 22:47:50.975 INFO 21580 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-05 22:47:50.975 INFO 21580 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-05 22:47:50.978 INFO 21580 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\oracle\ora90\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files\nodejs\;C:\Program Files\TortoiseSVN\bin;C:\Users\tristan\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Boot2Docker for Windows;C:\Program Files\Docker Toolbox;C:\Users\tristan\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;.]
2018-07-05 22:47:51.118 INFO 21580 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-05 22:47:51.118 INFO 21580 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1950 ms
2018-07-05 22:47:51.424 WARN 21580 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:47:51.424 INFO 21580 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:47:51.434 INFO 21580 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@3806b53f
2018-07-05 22:47:53.400 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpTraceFilter' to: [/*]
2018-07-05 22:47:53.405 INFO 21580 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-07-05 22:47:53.453 WARN 21580 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:47:53.454 INFO 21580 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:47:53.557 INFO 21580 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:47:53.741 INFO 21580 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1dc6e5e: startup date [Thu Jul 05 22:47:49 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@447e08e5
2018-07-05 22:47:53.796 INFO 21580 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-05 22:47:53.797 INFO 21580 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-05 22:47:53.829 INFO 21580 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:47:53.829 INFO 21580 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:47:54.217 INFO 21580 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-07-05 22:47:54.359 INFO 21580 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2018-07-05 22:47:54.373 INFO 21580 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-07-05 22:47:54.374 INFO 21580 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-07-05 22:47:54.374 INFO 21580 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-05 22:47:54.431 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-05 22:47:54.440 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure
2018-07-05 22:47:54.442 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2018-07-05 22:47:54.442 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure
2018-07-05 22:47:54.444 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
2018-07-05 22:47:54.451 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
2018-07-05 22:47:54.472 INFO 21580 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=1dc6e5e,type=ConfigurationPropertiesRebinder]
2018-07-05 22:47:54.481 INFO 21580 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-07-05 22:47:54.486 INFO 21580 --- [ restartedMain] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-07-05 22:47:54.521 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-07-05 22:47:54.968 INFO 21580 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-07-05 22:47:54.968 INFO 21580 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-07-05 22:47:55.064 INFO 21580 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-07-05 22:47:55.064 INFO 21580 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-07-05 22:47:55.241 INFO 21580 --- [ restartedMain] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-07-05 22:47:55.558 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-07-05 22:47:55.559 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-07-05 22:47:55.728 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-07-05 22:47:55.730 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-07-05 22:47:55.732 INFO 21580 --- [ restartedMain] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-07-05 22:47:55.735 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1530802075734 with initial instances count: 5
2018-07-05 22:47:55.739 INFO 21580 --- [ restartedMain] o.s.c.n.e.s.EurekaServiceRegistry : Registering application template-service with eureka with status UP
2018-07-05 22:47:55.740 INFO 21580 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1530802075740, current=UP, previous=STARTING]
2018-07-05 22:47:55.741 INFO 21580 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_TEMPLATE-SERVICE/192.168.56.1:template-service:8783: registering service...
2018-07-05 22:47:55.770 INFO 21580 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_TEMPLATE-SERVICE/192.168.56.1:template-service:8783 - registration status: 204
2018-07-05 22:47:55.776 INFO 21580 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8783 (http) with context path ''
2018-07-05 22:47:55.777 INFO 21580 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8783
2018-07-05 22:47:55.780 INFO 21580 --- [ restartedMain] c.t.t.TemplateServiceApplication : Started TemplateServiceApplication in 9.806 seconds (JVM running for 10.958)
2018-07-05 22:47:56.258 INFO 21580 --- [n(11)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-05 22:47:56.259 INFO 21580 --- [n(11)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-05 22:47:56.275 INFO 21580 --- [n(11)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
然后,为了测试我们需要写一个测试接口:
在XxxApplication.java同一级创建controller包(如果该包不在逻辑上的启动类包之下会导致无法扫描到,也就是无法被访问),
再创建TestController:
package com.tristan.templateservice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test")
public String test(String test){
return test+LocalDateTime.now().toString();
}
}
成功启动的日志:
2018-07-05 22:50:06.615 INFO 3216 --- [ restartedMain] c.t.t.TemplateServiceApplication : No active profile set, falling back to default profiles: default
2018-07-05 22:50:06.627 INFO 3216 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@10a961a2: startup date [Thu Jul 05 22:50:06 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@408a357f
2018-07-05 22:50:08.001 INFO 3216 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=d589e711-0ad2-3926-8b16-f4b945642d31
2018-07-05 22:50:08.014 INFO 3216 --- [ restartedMain] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-07-05 22:50:08.098 INFO 3216 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$ee4deb5b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-07-05 22:50:08.458 INFO 3216 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8783 (http)
2018-07-05 22:50:08.478 INFO 3216 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-05 22:50:08.478 INFO 3216 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-05 22:50:08.482 INFO 3216 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\oracle\ora90\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files\nodejs\;C:\Program Files\TortoiseSVN\bin;C:\Users\tristan\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Boot2Docker for Windows;C:\Program Files\Docker Toolbox;C:\Users\tristan\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;.]
2018-07-05 22:50:08.603 INFO 3216 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-05 22:50:08.603 INFO 3216 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1976 ms
2018-07-05 22:50:08.892 WARN 3216 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:50:08.893 INFO 3216 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:50:08.903 INFO 3216 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@78b7c8c9
2018-07-05 22:50:10.819 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-05 22:50:10.823 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-05 22:50:10.823 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-05 22:50:10.823 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-05 22:50:10.824 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-05 22:50:10.824 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpTraceFilter' to: [/*]
2018-07-05 22:50:10.824 INFO 3216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-07-05 22:50:10.871 WARN 3216 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:50:10.871 INFO 3216 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:50:10.959 INFO 3216 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:50:11.152 INFO 3216 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@10a961a2: startup date [Thu Jul 05 22:50:06 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@408a357f
2018-07-05 22:50:11.210 INFO 3216 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/test]}" onto public java.lang.String com.tristan.templateservice.controller.TestController.test(java.lang.String)
2018-07-05 22:50:11.212 INFO 3216 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-05 22:50:11.212 INFO 3216 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-05 22:50:11.246 INFO 3216 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:50:11.247 INFO 3216 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 22:50:11.671 INFO 3216 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-07-05 22:50:11.835 INFO 3216 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2018-07-05 22:50:11.850 INFO 3216 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-07-05 22:50:11.850 INFO 3216 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-07-05 22:50:11.851 INFO 3216 --- [ restartedMain] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-05 22:50:11.906 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-05 22:50:11.914 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure
2018-07-05 22:50:11.916 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2018-07-05 22:50:11.917 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure
2018-07-05 22:50:11.920 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
2018-07-05 22:50:11.928 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
2018-07-05 22:50:11.939 INFO 3216 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=10a961a2,type=ConfigurationPropertiesRebinder]
2018-07-05 22:50:11.948 INFO 3216 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-07-05 22:50:11.954 INFO 3216 --- [ restartedMain] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-07-05 22:50:11.988 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-07-05 22:50:12.435 INFO 3216 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-07-05 22:50:12.436 INFO 3216 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-07-05 22:50:12.536 INFO 3216 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-07-05 22:50:12.536 INFO 3216 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-07-05 22:50:12.722 INFO 3216 --- [ restartedMain] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-07-05 22:50:13.039 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-07-05 22:50:13.040 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-07-05 22:50:13.182 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-07-05 22:50:13.185 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-07-05 22:50:13.187 INFO 3216 --- [ restartedMain] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-07-05 22:50:13.190 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1530802213189 with initial instances count: 6
2018-07-05 22:50:13.194 INFO 3216 --- [ restartedMain] o.s.c.n.e.s.EurekaServiceRegistry : Registering application template-service with eureka with status UP
2018-07-05 22:50:13.194 INFO 3216 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1530802213194, current=UP, previous=STARTING]
2018-07-05 22:50:13.196 INFO 3216 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_TEMPLATE-SERVICE/192.168.56.1:template-service:8783: registering service...
2018-07-05 22:50:13.221 INFO 3216 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_TEMPLATE-SERVICE/192.168.56.1:template-service:8783 - registration status: 204
2018-07-05 22:50:13.229 INFO 3216 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8783 (http) with context path ''
2018-07-05 22:50:13.230 INFO 3216 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8783
2018-07-05 22:50:13.233 INFO 3216 --- [ restartedMain] c.t.t.TemplateServiceApplication : Started TemplateServiceApplication in 9.824 seconds (JVM running for 10.983)
2018-07-05 22:50:13.577 INFO 3216 --- [n(12)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-05 22:50:13.577 INFO 3216 --- [n(12)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-05 22:50:13.593 INFO 3216 --- [n(12)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
动态转发的网关
首先,配置依赖:
<dependencies>
<!--服务发现组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--gateway网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--redis组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
然后,配置配置文件:
server:
port: 80 # 对外访问端口
spring:
application:
name: gateway-center # 服务名,契约名
cloud:
gateway:
discovery:
locator:
enabled: true
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8773/eureka/,http://127.0.0.1:8772/eureka/,http://127.0.0.1:8771/eureka/ #配置到eureka注册中心
instance:
prefer-ip-address: true # 配置提供ip
#logging:
# level:
# org.springframework.cloud.gateway: TRACE # 配置后会详细的打印出每次访问时的http信息
然后,编写启动类(提示:idea会自动生成):
package com.tristan.gatewayserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //需要从注册中心动态读取实例并进行具体ip调用,注意:通过浏览器直接透过网关访问子服务时的服务名默认需要全大写
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
成功启动后的日志:
2018-07-05 22:32:32.450 INFO 10740 --- [ main] c.t.g.GatewayServerApplication : No active profile set, falling back to default profiles: default
2018-07-05 22:32:32.460 INFO 10740 --- [ main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@50d68830: startup date [Thu Jul 05 22:32:32 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6057aebb
2018-07-05 22:32:33.003 INFO 10740 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-07-05 22:32:33.222 INFO 10740 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=c111440f-88dd-3c9d-96a0-6cf5c4bd34d3
2018-07-05 22:32:33.240 INFO 10740 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-07-05 22:32:33.354 INFO 10740 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d71ef0d8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-07-05 22:32:33.619 INFO 10740 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-07-05 22:32:33.619 INFO 10740 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2018-07-05 22:32:36.102 INFO 10740 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2018-07-05 22:32:36.499 WARN 10740 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:32:36.499 INFO 10740 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:32:36.507 INFO 10740 --- [ main] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@28b458e6
2018-07-05 22:32:36.681 INFO 10740 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2018-07-05 22:32:36.697 INFO 10740 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-07-05 22:32:36.698 INFO 10740 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-07-05 22:32:36.699 INFO 10740 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping.links(org.springframework.web.server.ServerWebExchange)
2018-07-05 22:32:36.745 INFO 10740 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@50d68830: startup date [Thu Jul 05 22:32:32 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6057aebb
2018-07-05 22:32:36.858 WARN 10740 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2018-07-05 22:32:36.859 INFO 10740 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2018-07-05 22:32:37.450 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-05 22:32:37.458 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure
2018-07-05 22:32:37.460 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure
2018-07-05 22:32:37.462 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2018-07-05 22:32:37.464 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager]
2018-07-05 22:32:37.471 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
2018-07-05 22:32:37.482 INFO 10740 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=50d68830,type=ConfigurationPropertiesRebinder]
2018-07-05 22:32:37.491 INFO 10740 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-07-05 22:32:37.498 INFO 10740 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2018-07-05 22:32:37.529 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2018-07-05 22:32:37.585 INFO 10740 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2018-07-05 22:32:37.585 INFO 10740 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2018-07-05 22:32:37.679 INFO 10740 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2018-07-05 22:32:37.679 INFO 10740 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2018-07-05 22:32:37.862 INFO 10740 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-07-05 22:32:38.177 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-07-05 22:32:38.340 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-07-05 22:32:38.342 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-07-05 22:32:38.345 INFO 10740 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-07-05 22:32:38.348 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1530801158347 with initial instances count: 4
2018-07-05 22:32:38.352 INFO 10740 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application gateway-center with eureka with status UP
2018-07-05 22:32:38.352 INFO 10740 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1530801158352, current=UP, previous=STARTING]
2018-07-05 22:32:38.354 INFO 10740 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_GATEWAY-CENTER/192.168.56.1:gateway-center:80: registering service...
2018-07-05 22:32:38.385 INFO 10740 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_GATEWAY-CENTER/192.168.56.1:gateway-center:80 - registration status: 204
2018-07-05 22:32:39.089 INFO 10740 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:80
2018-07-05 22:32:39.089 INFO 10740 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 80
2018-07-05 22:32:39.090 INFO 10740 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 80
2018-07-05 22:32:39.093 INFO 10740 --- [ main] c.t.g.GatewayServerApplication : Started GatewayServerApplication in 9.819 seconds (JVM running for 10.893)
2018-07-05 22:32:39.720 INFO 10740 --- [on(6)-127.0.0.1] io.lettuce.core.EpollProvider : Starting without optional epoll library
2018-07-05 22:32:39.722 INFO 10740 --- [on(6)-127.0.0.1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
测试:
通过浏览器直接测试转发(所以只能测试get请求),
url为: http://localhost/TEMPLATE-SERVICE/test/test
访问结果: null2018-07-05T22:53:44.009
但是如果url为: http://localhost/TEMPLATE-SERVICE/test/test?test=tristanTest
访问结果为: tristanTest2018-07-05T22:55:36.695
至此配置中心、简单网关,注册、发现、转发、访问、映射参数就ok了,更多的过几天再写