SpringCloud-Eureka 單機模式 系統配置信息 集羣模式 後續補充!

系統配置信息

  • springboot版本:2.1.6.RELEASE
  • jdk:1.8
  • 系統:Windows10

工程結構

  • 父工程 halo-cloud-parent
  • 子工程<註冊中心> halo-cloud-server
  • 子工程<服務消費者> halo-cloud-consumer
  • 子工程<服務提供者> halo-cloud-provider

halo-coud-parent

  • 依賴引入
  <!--打包方式-->
  <packaging>pom</packaging>
  <modules>
        <!--moudle-->
        <module>halo-cloud-server</module>
    </modules>
  <!--web依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>
 <dependencyManagement>
        <!--引入springcloud依賴的-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

halo-cloud-server

  • 依賴
  <!--將其對應的父工程換爲parent即可-->
    <parent>
        <groupId>com.cloud</groupId>
        <artifactId>halo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
     <!--eureka server 依賴座標-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

application.yml

server:  # 服務端口
  port: 9090
spring:
  application:  # 應用名字,eureka 會根據它作爲服務id
    name: EurekaServer
eureka:
  instance:
    hostname: localhost
  client:
    service-url:   #  eureka server 的地址, 咱們單實例模式就寫自己好了
      defaultZone:  http://${eureka.instance.hostname}:${server.port}/eureka
    register-with-eureka: false# 不向eureka server 註冊自己
    fetch-registry: false# 不向eureka server 獲取服務列表

修改啓動類,加上註解:@EnableEurekaServer或者

啓動

遇到的問題:

  • 第一次啓動啓動的時候,會出現一下的錯誤,不要慌!


打開IDEA最右邊maven,找到server,clean、install,重新啓動即可。


halo-cloud-provider

  • pom.xml
 <parent>
        <groupId>com.cloud</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
  • application.yml
server:
  port: 7070
spring:
  application:
    name: spring-cloud-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9090/eureka
    fetch-registry: true
    register-with-eureka: true

啓動類添加註解:@EnableEurekaClient

halo-cloud-consumer

  • pom.xml
 <parent>
        <groupId>com.cloud</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • application.yml
server:
  port: 8080

spring:
  application:
    name: spring-cloud-consumer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9090/eureka
    fetch-registry: true
    register-with-eureka: true

啓動類添加註解:@EnableEurekaClient

我們可以看到服務提供者個消費者都註冊到了註冊中心。

消費者調用服務

服務提供者 halo-cloud-provider

修改內容

  • 新增 ProviderController
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Value("${server.port}")
    private Integer port;


    @GetMapping("/helloProvider")
    public Integer helloProvider(){
        return port;
    }
}

測試:

服務消費者 halo-cloud-consumer

修改內容

  • 新增 RestTemplateConfiguration
  • 新增 ConsumerController
@Configuration
public class RestTemplateConfiguration {
    @Bean
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/helloConsumer")
    public Integer getTodayStatistic() {
        // 使用discoveryClient 類能夠與eureka server 交互, getInstances 獲取註冊到eureka server
        // 的"spring-cloud-order-service-provider" 實例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("spring-cloud-provider");

        // 獲取第一個服務信息
        ServiceInstance instanceInfo = instances.get(0);
        //獲取ip
        String ip = instanceInfo.getHost();
        //獲取port
        int port = instanceInfo.getPort();
        String url = "http://" + ip + ":" + port + "/provider/helloProvider/";
        return restTemplate.getForObject(url, Integer.class);

    }
}

測試:

集羣模式 後續補充!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章