spring cloud eureka搭建

一、服务注册发现服务器

 application.yml

 

 

 

 

server:
port: 8888 #指定服务端口
eureka:
client:
registerWithEureka: false #是否将eureka自身作为应用注册到eureka注册中心
fetchRegistry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
serviceUrl:
defaultZone: http://localhost:8181/eureka/

 

 通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

 

DemoApplication.java

 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

 

界面查看

 

http://localhost:8888/

 

 二、创建服务提供者 (eureka client)

 

server:
port: 7070
spring:
application:
name: cloud-client #为你的应用起个名字,该名字将注册到eureka注册中心

eureka-serviceIP: localhost

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8181/eureka/

 

 

DemoClientApplication.java

 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoClientApplication {

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

 

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.Inet4Address;
import java.net.UnknownHostException;


@RestController
public class DemoController {
private static long timeStample=System.currentTimeMillis();
private String words = "aa,bb";

@RequestMapping("/test")
public String getWord() {
String[] wordArray = words.split(",");
String address= null;
try {
address = Inet4Address.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
int i = (int)Math.round(Math.random() * (wordArray.length - 1));
return wordArray[i]+timeStample+ address;
}
}

 

查看界面

 

 http://localhost:8888/

 

 

posted @ 2018-03-08 16:49  和美信息叶育生博文  阅读(208)  评论(0编辑  收藏  举报