当前位置:   article > 正文

springcloud微服务探索之完整demo的记录与分析_springcloud 完整demo

springcloud 完整demo

Erueka服务器的建立

Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装。主要负责完成微服务架构中的服务治理功能。什么事服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,包括服务的主机与端口号、服务版本号、通讯协议等一些附加信息。注册中心按照服务名分类组织服务清单,同时还需要以心跳检测的方式去监测清单中的服务是否可用,若不可用需要从服务清单中剔除,以达到排除故障服务的效果。

项目都是基于springboot项目的,

1.建立项目

2.配置pom文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  9. </dependency>
  10. </dependencies>

3.书写配置文件

  1. server:
  2. port: 1000
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. register-with-eureka: false
  8. fetch-registry: false
  9. spring:
  10. application:
  11. name: eureka-server

在默认配置下,Eureka Server会将自己也作为客户端来尝试注册自己,我们需要禁用它的客户端禁用行为。

下面是一个Eureka Server的application.properites的相关配置:

#服务注册中心端口号

server.port=1110

#服务注册中心实例的主机名

eureka.instance.hostname=localhost

#是否向服务注册中心注册自己

eureka.client.register-with-eureka=false

#是否检索服务

eureka.client.fetch-registry=false

#服务注册中心的配置内容,指定服务注册中心的位置

启动项目

4.测试,输入地址栏地址

客户端项目建立:

1.新建springboot项目

2.pom文件和上边一只

3.配置文件(yml文件)

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://127.0.0.1:1000/eureka/
  5. spring:
  6. application:
  7. name: client-service1
  8. server:
  9. port: 2000a

 4.启动类和controller放在一起(简单方便)

  1. package com.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. @Configuration
  13. @ComponentScan
  14. @EnableEurekaClient
  15. @EnableAutoConfiguration
  16. @RestController
  17. public class ApplicationClient{
  18. public static void main(String[] args) {
  19. SpringApplication.run(ApplicationClient.class, args);
  20. }
  21. @RequestMapping(value = "hello",method = RequestMethod.GET)
  22. public String hello(){
  23. return "你好,世界";
  24. }
  25. }

启动项目,再次查看上边的服务地址,结果如下:

Feign客户端项目建立

1.建立springboot项目

2.pom文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>1.5.6.RELEASE</version>
  6. </parent>
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.feign.demo</groupId>
  9. <artifactId>feign</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <build>
  12. <plugins>
  13. <plugin>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-maven-plugin</artifactId>
  16. </plugin>
  17. </plugins>
  18. </build>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-feign</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-eureka</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <dependencyManagement>
  40. <dependencies>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-dependencies</artifactId>
  44. <version>Camden.SR6</version>
  45. <type>pom</type>
  46. <scope>import</scope>
  47. </dependency>
  48. </dependencies>
  49. </dependencyManagement>
  50. </project>

2. 配置文件

  1. server:
  2. port: 8081
  3. eureka:
  4. client:
  5. serviceUrl:
  6. defaultZone: http://localhost:1000/eureka/

3.启动类,controller,接口

  1. package com.feign.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.feign.EnableFeignClients;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. @ComponentScan
  11. @EnableAutoConfiguration
  12. @EnableEurekaClient
  13. @EnableFeignClients
  14. @SpringBootApplication
  15. public class FeignApplication {
  16. public static void main(String[] args) {
  17. SpringApplication.run(FeignApplication.class, args);
  18. }
  19. }
  20. package com.feign.demo;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestMethod;
  24. import org.springframework.web.bind.annotation.RestController;
  25. @RestController
  26. public class HelloController {
  27. @Autowired
  28. private IHello iHello;
  29. @RequestMapping(value = "gethello",method = RequestMethod.GET)
  30. public String getHello(){
  31. return iHello.getHello();
  32. }
  33. }
  34. package com.feign.demo;
  35. import org.springframework.cloud.netflix.feign.FeignClient;
  36. import org.springframework.web.bind.annotation.RequestMapping;
  37. import org.springframework.web.bind.annotation.RequestMethod;
  38. @FeignClient("client-service1")
  39. public interface IHello {
  40. @RequestMapping(value = "/hello",method = RequestMethod.GET)
  41. String getHello();
  42. }


@FeignClient("client-service1")里边参数是调用服务的服务名称。在具体方法里可以通过RequestMapping来直接请求对应服务的url. 

4.访问controller接口

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57737
推荐阅读
相关标签
  

闽ICP备14008679号