用ideal直接new --> project --> spring Initializr --> next --> 填写groupId,artifact -->next --> 填写project name --> finish.
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
com.whu.learning
microservice_discovery-eureka
0.0.1-SNAPSHOT
microservice_discovery-eureka
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.cloud
spring-cloud-dependencies
Camden.SR4
pom
import
org.springframework.boot
spring-boot-maven-plugin
#server
server.port=8761
#eureka
#表示是否将自己注册到eureka上,默认为true,由于当前 应用就是eureka server,故设为false
eureka.client.registerWithEureka=false
#表示是否从eureka server 获取注册信息,因为这是一个单点eureka server,不需要同步其他eureka server 的数据,故设为false
eureka.client.fetchRegistry=false
#设置与eureka server交互的地址,查询和注册服务都依赖这个地址
eureka.client.serviceUrk.defaultZone=http://localhost:8761/eureka/
@SpringBootApplication
//声明这是一个eureka server
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
}
}
org.springframework.cloud
spring-cloud-starter-eureka
#eureka
#指定注册到eureka server上的微服务的名称
spring.application.name=microservice_provider-user
#与eureka server交互的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#表示将自己ip注册到eureka server上
eureka.instance.prefer-ip-address=true
@SpringBootApplication
//添加此注解,见名知意
@EnableDiscoveryClient
@MapperScan("com.whu.learning.microservice_simpleprovideruser.dao")
public class MicroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
单一eureka server的情况是不适合生产环境的。虽然eureka client 会定时连接eureka server ,获取服务注册表中的信息并将其缓存到本地,但是如果eureka server 宕机,微服务也不可用时,eureka client 中的缓存未更新,就会影响该服务的调用,有可能导致整个系统不可用。因而,高可用的eureka server 集群是必要的。
127.0.0.1 peer1 peer2
spring:
application:
name: microservice_discovery-eureka-2
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8762/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/
java -jar microservice_discovery-eureka-2-0.0.1.SNAPSHOT.jar --spring.profiles.active=peer1
java -jar microservice_discovery-eureka-2-0.0.1.SNAPSHOT.jar --spring.profiles.active=peer2
通过spring.profiles.active指定使用那个profile启动;
注意: 启动第一个的时候会报错,如下:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:815) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:104) ~[eureka-client-1.4.12.jar!/:1.4.12]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_191]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_191]
但是这是正常的…因为第一个eureka server启动时也需要将其注册到其他eureka server中,但是此时只有它一个eureka server,故报这个错,当时我可卡在这整整一天。
5. 访问http://peer1:8761,结果如图:
发现DS Replicas 中已有peer2节点。
前面实例中,eureka server 时允许匿名访问的,本节构建一个登录才能访问的eureka server
org.springframework.boot
spring-boot-starter-security
#security
security.basic.enabled=true
security.user.name=user
security.user.password=password
将微服务的eureka.client.serviceUrk.defaultZone配置为:http://user:password@localhost:8761/eureka/
就行了
springCloud 与Docker微服务架构实战 --周立