spring boot2.*服务注册到consul

使用spring boot搭建项目注册到consul中

1、pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>consule01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>consule01</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.M7</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-consul-discovery</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>

	<repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

 2、配置文件applicaiton.yml

###服务端口号
server:
  port: 8004
spring:
  application:
    name: consul-member
####consul注册中心地址
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:  ##表示服务地址 服务所在主机ip
        hostname: ${spring.application.name}

3、主应用

@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class Consule01Application {
	  @Value("${server.port}")
	  private String serverPort;
	  
	   @RequestMapping("/getPort")
	   public String getMember() {
	         return "server prot:"+serverPort;
	   }
	   
	public static void main(String[] args) {
		SpringApplication.run(Consule01Application.class, args);
	}

}

4、启动结果


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2019-12-02 14:40:07.562  INFO 11892 --- [           main] com.example.demo.Consule01Application    : No active profile set, falling back to default profiles: default
2019-12-02 14:40:07.580  INFO 11892 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7efaad5a: startup date [Mon Dec 02 14:40:07 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5876a9af
2019-12-02 14:40:08.051  INFO 11892 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=868a3060-3c6a-32b5-9c80-3421549cb8b4
2019-12-02 14:40:08.067  INFO 11892 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-12-02 14:40:08.138  INFO 11892 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$21f849e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-02 14:40:08.594  INFO 11892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8004 (http)
2019-12-02 14:40:08.617  INFO 11892 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-12-02 14:40:08.617  INFO 11892 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2019-12-02 14:40:08.625  INFO 11892 --- [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\jre1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_181/bin/server;C:/Program Files/Java/jre1.8.0_181/bin;C:/Program Files/Java/jre1.8.0_181/lib/amd64;E:\tools\Xshell\Xshell-6.0.0098\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Java\jdk1.8.0_181\bin;C:\Program Files\Java\jdk1.8.0_181\jre\bin;E:\Java\MySQL\mysql-5.7.23-winx64\bin;E:\Java\gradle-4.8.1\bin;E:\Java\maven-3.5.2\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;E:\Java\Node\Node-10.15.3\;E:\tools\TortoiseGit\TortoiseGit\bin;E:\ide\SQLServer\Microsoft SQL Serve(x86)\140\Tools\Binn\;E:\ide\SQLServer\Microsoft SQL Serve\140\Tools\Binn\;E:\ide\SQLServer\Microsoft SQL Serve(x86)\140\DTS\Binn\;E:\ide\SQLServer\Microsoft SQL Serve\140\DTS\Binn\;E:\ide\SQLServer\Microsoft SQL Serve\Client SDK\ODBC\130\Tools\Binn\;E:\ide\SQLServer\Microsoft SQL Serve(x86)\150\DTS\Binn\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\dotnet\;C:\Users\lt\AppData\Local\Microsoft\WindowsApps;C:\Users\lt\AppData\Roaming\npm;E:\ide\eclipse4.12\eclipse;;.]
2019-12-02 14:40:08.738  INFO 11892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-12-02 14:40:08.738  INFO 11892 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1158 ms
2019-12-02 14:40:08.861  INFO 11892 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-12-02 14:40:08.864  INFO 11892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-12-02 14:40:08.864  INFO 11892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-12-02 14:40:08.864  INFO 11892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-12-02 14:40:08.865  INFO 11892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-12-02 14:40:08.917  WARN 11892 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-12-02 14:40:08.917  INFO 11892 --- [           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.
2019-12-02 14:40:08.922  WARN 11892 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-12-02 14:40:08.922  INFO 11892 --- [           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.
2019-12-02 14:40:08.998  INFO 11892 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-02 14:40:09.244  INFO 11892 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7efaad5a: startup date [Mon Dec 02 14:40:07 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5876a9af
2019-12-02 14:40:09.307  INFO 11892 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getMember]}" onto public java.lang.String com.example.demo.Consule01Application.getMember()
2019-12-02 14:40:09.311  INFO 11892 --- [           main] 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)
2019-12-02 14:40:09.311  INFO 11892 --- [           main] 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)
2019-12-02 14:40:09.333  INFO 11892 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-02 14:40:09.334  INFO 11892 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-02 14:40:10.973  INFO 11892 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-12-02 14:40:10.981  INFO 11892 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2019-12-02 14:40:10.982  INFO 11892 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'environmentManager' has been autodetected for JMX exposure
2019-12-02 14:40:10.983  INFO 11892 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'refreshScope' has been autodetected for JMX exposure
2019-12-02 14:40:10.985  INFO 11892 --- [           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]
2019-12-02 14:40:10.995  INFO 11892 --- [           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]
2019-12-02 14:40:11.004  INFO 11892 --- [           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=7efaad5a,type=ConfigurationPropertiesRebinder]
2019-12-02 14:40:11.052  INFO 11892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8004 (http) with context path ''
2019-12-02 14:40:11.065  INFO 11892 --- [           main] o.s.c.c.s.ConsulServiceRegistry          : Registering service with consul: NewService{id='consul-member-8004', name='consul-member', tags=[], address='127.0.0.1', port=8004, enableTagOverride=null, check=Check{script='null', interval='10s', ttl='null', http='http://127.0.0.1:8004/actuator/health', tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null'}, checks=null}
2019-12-02 14:40:11.129  INFO 11892 --- [           main] com.example.demo.Consule01Application    : Started Consule01Application in 5.077 seconds (JVM running for 5.533)
发布了19 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/minion_banana/article/details/103347260