這里我學習了怎么向服務注冊中心注冊一個服務提供者
這里我用到的是之前搭建的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控制台打印如下數據

完成!
