在上篇文章中我们已经写好了服务站和消费者。本文将会把两个服务注册到Eureka服务中。
Eureka Server
package com.zhuyang.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}
application.yml
security:
basic:
enabled: true
user:
name: user ##Eureka server username password
password: passwd123
server:
port: 8761 ##Eureka server port
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://user:passwd123@localhost:${server.port}/eureka/ ##use spring security
Provider
DemoMicroserviceProviderUserApplication.java
package com.zhuyang.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //Reigster Service to Eureka
public class DemoMicroserviceProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMicroserviceProviderUserApplication.class, args);
}
}
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://user:passwd123@localhost:8761/eureka/ ##register service to eureka server
instance:
preferIpAddress: true
Consumer
DemoMicroserviceConsumerUserApplication.java
package com.zhuyang.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient //Reigster Service to Eureka
public class DemoMicroserviceConsumerUserApplication {
@Bean
public RestTemplate restTemplate() { // equals to RestTemplate restTemplate = new RestTemplate()
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(DemoMicroserviceConsumerUserApplication.class, args);
}
}
application.yml
server:
port: 8000
eureka:
client:
serviceUrl:
defaultZone: http://user:passwd123@localhost:8761/eureka/
instance:
preferIpAddress: true
Registered instance UNKNOWN/windows10.microdone.cn:8000 with status UP (replication=false)
Registered instance UNKNOWN/windows10.microdone.cn:8000 with status UP (replication=true)
Registered instance MICIROSERVICE-PROVIDER/windows10.microdone.cn:miciroservice-provider:8001 with status UP (replication=false)
Registered instance MICIROSERVICE-PROVIDER/windows10.microdone.cn:miciroservice-provider:8001 with status UP (replication=true)
同时访问http:localhost:8761,将会看到Eureka 控制台中有两个刚刚注册的服务(我家里的机器不知道为什么一直返回的是xml格式)
这样一来我们的两个service就成功注册到eureka server了 。