spring-cloud学习3之注册服务提供者

注:参考卓永超《Spring Cloud 微服务实践》

spring boot 版本为1.3.7 

1、pom 文件中添加必要依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2、编写测试服务

@RestController
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("hello")
    public String hello() {
        ServiceInstance si = discoveryClient.getLocalServiceInstance();
        logger.info("hello,host" + si.getHost() + ",service_id " + si.getServiceId());
        return "hello world";
    }
}

3、使用@EnableDiscoveryClient ,激活Eureka中对DiscoveryClient 的实现

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

	public static void main(String[] args) {
		//SpringApplication.run(EurekaClientApplication.class, args);
		new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
	}
}

4、在application.properties 文件中指定服务名称和注册中心地址

spring.application.name=hello-server
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

5、观察启动服务客户端服务之后 , 观察注册中心console 打印出如下信息

Registered instance HELLO-SERVER/zhlm-PC:hello-server with status UP (replication=false)

或访问http://localhost:8080/hello  服务客户端控制台打印出如下信息

 hello,hostzhlm-PC,service_id hello-server

或在注册中心首页观察到


表示注册中心客户端搭建成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值