上篇文章的图再放一次,多看,多理解,熟记于心。
上一篇文章已经创建好了商场(Eureka注册中心),文章地址:Spring Cloud微服务架构——Eureka注册中心,现在我们可以让商家入驻商场了,即服务注册。
快速构建一个Spring Boot工程,方法可以参考上篇文章。
org.springframework.boot
spring-boot-starter-parent
1.3.5.RELEASE
com.jqcode
eureka-provider1
0.0.1
eureka-provider1
eureka provider1
1.8
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
org.springframework.cloud
spring-cloud-dependencies
Brixton.RELEASE
pom
import
@RestController
public class FoodController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private DiscoveryClient client; // 用于服务注册
@RequestMapping(value = "/eat", method = RequestMethod.GET)
public String foodService(String customerName){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("host:" + instance.getHost() + ",service_id:" + instance.getServiceId());
return "尊敬的顾客:" + customerName + ",您好!欢迎光临海底捞6666号分店!";
}
}
@EnableDiscoveryClient // 激活Eureka中DiscoveryClient的实现
@SpringBootApplication
public class EurekaProvider1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider1Application.class, args);
}
}
#服务提供者1启动端口
server.port=9001
#服务名
spring.application.name=food-service
#注册中心服务地址
eureka.client.serviceUrl.defaultZone=http://loaclhost:8000/eureka
启动该项目,再次访问:http://localhost:8000
从页面上我们可以看到,我们的餐饮服务已经入驻到了商场(服务提供者已经注册到了Eureka注册中心)