这里我学习了怎么向服务注册中心注册一个服务提供者
这里我用到的是之前搭建的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控制台打印如下数据
完成!