SpringCloud學習之手把手教你用IDEA搭建入門項目(三)


      本篇博客是承接上一篇《手把手教你用IDEA搭建SpringCloud入門項目(二)》,不清楚的請到我的博客空間查看后再看本篇博客,上面兩篇博客成功創建了一個簡單的SpringCloud項目,本篇博客主要是貼出項目的代碼部分,方便讀者更好的實戰操作搭建一個屬於自己的SpringCloud項目

1)項目框架大體如下

2)編寫eureka-server模塊的啟動類

 

package com.xu.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

3)把properties文件改成yml類型文件,並且編輯配置文件如下

application.yml文件編輯如下:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost    #表示這個注冊中心在本地
  client:
    registerWithEureka: false   #注冊中心不注冊自己,默認是注冊自己
    fetchRegistry: false
    serviceUrl:
      #拿到上面的主機地址和端口地址,配置一個注冊中心
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4)啟動注冊中心服務,看到控制台輸出這些信息,則表示注冊中心服務啟動成功

5)注冊中心服務啟動后,在瀏覽器輸入地址:http://localhost:8761/,應該可以看到這個界面,這里可以看到在注冊中心注冊的一些服務

6)服務提供者模塊service-provider其他代碼如下

ServiceProviderApplication.java
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }

}

修改application.properties文件為application.yml,並編輯如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/    #這里是告訴他注冊中心地址
server:
  port: 8763
spring:
  application:
    name: service-hello    #這里給服務取一個帥氣的名字

這里只提供了一個RestApi接口服務,后面服務消費者會調用這個測試API服務:

HelloWorldController.java

package com.xu.serviceprovider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

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

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ", i am from port: " + port;
    }

}

 

啟動這個模塊

然后刷新注冊中心地址http://localhost:8761/,就可以看到他已經被注冊到注冊中心了

7)服務消費者模塊service-consumer其他代碼如下:

ServiceConsumerApplication.java

package com.xu.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {

        return new RestTemplate();
    }
}

service-consumer模塊的application.yml如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

其他代碼如下:

HelloService.java

package com.xu.serviceconsumer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name)
    {
        return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name, String.class);
    }
}

HelloControler.java

package com.xu.serviceconsumer.controller;

import com.xu.serviceconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService(name);
    }
}

啟動這個模塊,成功后如下:

刷新注冊中心地址:http://localhost:8761/,可以看到一個新的服務又被注冊到注冊中心了

打開瀏覽器,在一個新的窗口中輸入地址:http://localhost:8764/hi?name=admin,如果成功則返回如下信息則表示成功調用了服務提供者的服務接口(根據端口可以知道,8763端口是服務提供者端口)

本篇博客到此為止,如有問題請留言大家一起討論學習,諸君共勉!

===============================================================================

如果您覺得此文有幫助,可以小小打賞一下,持續更新更有動力喲!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM