文章出處
SpringCloud(二)將服務注冊到Eureka:在上篇文章中我們已經寫好了服務站和消費者。本文將會把兩個服務注冊到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@EnableEurekaServerpublic 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: passwd123server: port: 8761 ##Eureka server porteureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://user:passwd123@localhost:${server.port}/eureka/ ##use spring security
ProviderDemoMicroserviceProviderUserApplication.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 Eurekapublic 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 ConsumerDemoMicroserviceConsumerUserApplication.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 Eurekapublic class DemoMicroserviceConsumerUserApplication {@Beanpublic 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: 8000eureka: client: serviceUrl: defaultZone: http://user:passwd123@localhost:8761/eureka/ instance: preferIpAddress: true
當consumer和provider啟動后,eureka log會出現:
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了 .
看文倉www.kanwencang.com網友整理上傳,為您提供最全的知識大全,期待您的分享,轉載請注明出處。
歡迎轉載:http://www.kanwencang.com/bangong/20170111/85019.html
文章列表
全站熱搜