1. 程式人生 > >SpringCloud學習點滴——euraka之服務提供者註冊

SpringCloud學習點滴——euraka之服務提供者註冊

 

 

 

這裡我學習了怎麼向服務註冊中心註冊一個服務提供者

這裡我用到的是之前搭建的spring boot 的例項那個服務作為服務提供者,eureka-service作為註冊中心

1、服務提供者增加eureka依賴和spring cloud的依賴

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

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

2、Controller類裡面新增DiscoveryClient類來發現服務呼叫的列印
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());

@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
return "Hello world";
}
}

3、啟動類新增eureka客戶端依賴
@EnableEurekaClient
@SpringBootApplication
public class SpringBootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootdemoApplication.class, args);
}
}

4、配置application.yml檔案
spring:
application:
name: hello-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/


5、啟動註冊中心以及hello-service,
  (1)首先看到eureka-service有DiscoverClient列印的資料
    

   (2)helllo-service列印是這樣的

    

    (3)訪問http://localhost:1111/,看到服務中心有hello-service'這個服務

    

    (4)訪問http://localhost:8080/hello/.可以看到hello-service控制檯列印如下資料

完成!