本文共 4944 字,大约阅读时间需要 16 分钟。
先是写eureka-server注册中心应用test-server:
pom文件:
然后是application.yml文件:4.0.0 com.sino.cloud test-server 0.0.1-SNAPSHOT testServer UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE org.springframework.cloud spring-cloud-starter-parent Camden.SR1 pom import org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-maven-plugin
server: port: 8761eureka: client: register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心 fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server service-url: defaultZone: http://localhost:8761/eureka/最后是Application.java类:
package com.sino.cloud;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}run启动后,本地启动http://localhost:8761 就进入了eureka注册中心。,、
然后是客户端test-provider:
先是pom文件:
然后是application.yml文件:4.0.0 com.spring.sino.cloud test-provider 0.0.1-SNAPSHOT testProvider UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE org.springframework.cloud spring-cloud-starter-parent Camden.SR1 pom import org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-actuator com.h2database h2 org.springframework.boot spring-boot-maven-plugin
server: port: 8082spring: application: name: sample-client #为你的应用起个名字,该名字将注册到eureka注册中心eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true #表示把ip注册进入 instance: prefer-ip-address: true #表示把ip注册进入 instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} #自定义实例id,也就是注册中的status值最后是启动类Application.java:
package com.cloud;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class Application { //@EnableDiscoveryClient @EnableEurekaClient @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}刷新localhost:7861页面,看到注册信息。
转载地址:http://opbni.baihongyu.com/