博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springcloud 的eureka服务注册demo
阅读量:4074 次
发布时间:2019-05-25

本文共 4944 字,大约阅读时间需要 16 分钟。

先是写eureka-server注册中心应用test-server:

pom文件:

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
然后是application.yml文件:

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文件:

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
然后是application.yml文件:

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/

你可能感兴趣的文章
管理文件
查看>>
Android网络与数据存储_学习笔记_第五周
查看>>
Android网络与数据存储_学习笔记_第六周:SQLite与ContentProvider
查看>>
网络编程数据处理_学习笔记_第七周
查看>>
Android UiAutomator UiObject API
查看>>
多线程_学习笔记_第七周
查看>>
java基础 -- 六哥
查看>>
多进程
查看>>
传感器与LBS
查看>>
接口测试关注点、常用工具
查看>>
应用性能优化
查看>>
git、Gradle、NDK、依赖及补充
查看>>
UIWindow UIView UILable UIButton
查看>>
L1 - bundle id、Xcode hello world Demo
查看>>
charles mock数据
查看>>
L2 - 签名、自动化打ipa文件、证书
查看>>
L3 appium架构、git使用
查看>>
Python
查看>>
1、Objective-C简介
查看>>
00 vim 简明教程
查看>>