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
@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

当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了 。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值