【Spring Cloud】服务生产者
创建spring boot项目并添加依赖
创建项目过程略
添加依赖
<!-- 用于进行WEB开发,向外提供服务接口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加spring cloud eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
服务配置
-
application.yml
spring: application: name: service-producter server: port: 8081 eureka: client: service-url: defaultZone: http://localhost:8000/eureka/ //对应服务注册中心的配置,指定服务注册中心地址
-
启动类添加注解@EnableEurekaClient
@EnableEurekaClient @SpringBootApplication public class ServiceProductApplication { public static void main(String[] args) { SpringApplication.run(ServiceProductApplication.class, args); } }
添加服务接口
/user/
-
创建UserController
package com.icodesoft.serviceproducter.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public String getUserNameById(@PathVariable("id") String id) { return "name: gary, id: " + id; } }
运行结果
-
测试接口
-
服务中心