springcloud 简单案例

    xiaoxiao2023-10-23  174

    一.创建cloudparent pom项目

    添加依赖 <groupId>com.abc</groupId> <artifactId>cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

    二.创建cloudserver springboot模块 :启动注册中心eureka

    pom依赖 <parent> <artifactId>cloud-parent</artifactId> <groupId>com.abc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <- 引入该依赖 默认会引入 该依赖所需要的所有的springboot依赖 -> </dependencies> 修改application.yml server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false #声明不提供服务 fetchRegistry: false #声明不应用服务 serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eureka-server 启动类 @SpringBootApplication @EnableEurekaServer public class EurekaServerApp { public static void main(String[] args) { SpringApplication.run(EurekaServerApp.class,args); } } 访问http://localhost:8761

    三. 创建service-hello springboot model 创建微服务应用并注册

    pom文件 <parent> <artifactId>cloud-parent</artifactId> <groupId>com.abc</groupId> <version>1.0-SNAPSHOT</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> 修改application.yml server: port: 8763 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: service-hello 启动类,服务接口,及接口实现 @SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hello") public String hello(@RequestParam(value = "name") String name) { //如果参数为实体类用: @RequestBody 1.如果直接 访问时需使用application/json 格式访问 //2.如果通过问服务调用,则直接传入类即可 return "hi " + name+" "+port; } }

    四.创建client-feign springboot模块 调用微服务

    修改pom依赖 <parent> <artifactId>cloud-parent</artifactId> <groupId>com.abc</groupId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> 修改application.yml server: port: 8765 eureka: client: service-url: defaultZone: http://localhost:8761/eureka spring: application: name: client-feign 启动类 及 微服务调用类 @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ClientFeignApplication { public static void main(String[] args) { SpringApplication.run(ClientFeignApplication.class, args); } } @FeignClient(value = "service-hello") public interface FeiHiService { @RequestMapping(value = "/hello",method = RequestMethod.GET) String sayHi(@RequestParam(value = "name") String name); } @RestController public class FeiHiController { @Autowired FeiHiService hiService; @RequestMapping("/hello") public String sayHello(String name){ String word = hiService.sayHi(name); return word; } }
    最新回复(0)