springcloud(Brixton版) EurekaServer 消费端

ゝ一世哀愁。 2023-08-17 16:25 168阅读 0赞

1.创建springboot项目。

2.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.didispace</groupId>
  6. <artifactId>compute-service</artifactId>
  7. <version>1.0.0</version>
  8. <packaging>jar</packaging>
  9. <name>compute-service</name>
  10. <description>Spring Cloud project</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.3.5.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-eureka</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. </dependencies>
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-dependencies</artifactId>
  37. <version>Brixton.RELEASE</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

3.application.properties

  1. spring.application.name=compute-service
  2. server.port=2222
  3. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

4.controller(这里简单的进行计算两数之和的逻辑处理)

  1. import org.apache.log4j.Logger;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.cloud.client.ServiceInstance;
  4. import org.springframework.cloud.client.discovery.DiscoveryClient;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. public class ComputeController {
  11. private final Logger logger = Logger.getLogger(getClass());
  12. @Autowired
  13. private DiscoveryClient client;
  14. @RequestMapping(value = "/add" ,method = RequestMethod.GET)
  15. public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
  16. ServiceInstance instance = client.getLocalServiceInstance();
  17. Integer r = a + b;
  18. logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
  19. return r;
  20. }
  21. }

5.启动类:

  1. import org.springframework.boot.autoconfigure.SpringBootApplication;
  2. import org.springframework.boot.builder.SpringApplicationBuilder;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. @EnableDiscoveryClient
  5. @SpringBootApplication
  6. public class ComputeServiceApplication {
  7. public static void main(String[] args) {
  8. new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
  9. }
  10. }

6.启动项目,访问http://localhost:1111/ 则可以看见被注册到了eureka服务端。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMzcxNzY2_size_16_color_FFFFFF_t_70

接着访问:http://192.168.8.1:2222/add?a=1&b=2

20190928164508995.png

2019-09-28 16:37:02.654 INFO 9620 —- [nio-2222-exec-5] com.didispace.web.ComputeController : /add, host:192.168.8.1, service_id:compute-service, result:3

这样就完成了服务端和消费端的简单实现。

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读