SpringCloud二:服务提供者

1.创建一个名称为eureka-service的springboot项目后在properties加入相关配置

#服务名称
spring.application.name=eureka-service
#端口号
server.port=8762
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#启动服务发现的功能,开启了才能调用其它服务
#spring.cloud.config.discovery.enabled=true
#发现的服务的名字--对应注测中心的服务名字
#spring.cloud.config.discovery.serviceId=eurka-server

2.添加provider需要的pom文件

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

3.在SpringBoot的启动类上加上@EnableEurekaClient注解

@SpringBootApplication
@EnableEurekaClient
public class EurekaServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServiceApplication.class, args);
	}
}

4.启动eureka-service项目后刷新注册中心界面,就会发现eureka-service已经注册到注册中心了

5.写一个接口调用,模拟一下

@RestController
public class demoController {

    @RequestMapping(value = "find",method = RequestMethod.GET)
    public String findInfo(){
        return  "service";
    }
}

6.在浏览器输入http://localhost:8762/find 

这里用到的版本是Spring Boot2.0.3.RELEASE,Spring CloudFinchley.RELEASE。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章