spring cloud服務提供與調用示例

本文建立方式採用intellij IDEA  建立項目java

1.建立基於Eureka的註冊中心。web

  在打開項目中右鍵,選擇new 選擇moudlespring

而後下一步apache

輸入要建立的項目的信息瀏覽器

 

 選擇web下面的web,選擇cloud  discovery下面的 Eureka server  下一步,建立項目app

而後同步pom.xml文件內容、maven

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.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-server</artifactId>
        </dependency>

        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

 

 在啓動文件中選擇spring-boot

@SpringBootApplication @EnableEurekaServer public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }

配置文件測試

server:
  port: 8000
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
spring:
  application:
    name: spring-cloud-eureka

  建立完,咱們去運行下,運行正常後,咱們去訪問localhost:8000, 到下面的界面,這樣咱們Eureka 註冊中心就建立成功,fetch

 

下面咱們去建立server端,和client;

server端呢建立中與Eureka選擇不一樣的在於cloud  discovery中,這裏須要選擇cloud Discovery

而後建立完,去同步對應的pom.xml文件

在啓動類編寫以下

@SpringBootApplication @EnableDiscoveryClient public class ServeroneApplication { public static void main(String[] args) { SpringApplication.run(ServeroneApplication.class, args); } }


配置文件
server: port:
8001 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8000/eureka/ spring: application: name: spring-serverserver

咱們須要編寫一個提供服務的接口

@RestController public class HelloController { @RequestMapping("/hello") public String indesx(@RequestParam String name) { return "hello "+name+",this is first messge"; } }

這樣咱們就能夠實現咱們的server端

而後咱們去建立client端

client端多須要在server上建立多一個Feign

那麼咱們在啓動類,以下

@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

配置文件

server: port: 8002 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8000/eureka/
spring: application: name: spring-client

那麼咱們去寫調用server的服務

@FeignClient(name= "spring-serverserver") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }

實現接口

@RestController public class ConsumerController { @Autowired HelloRemote lloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return lloRemote.hello(name); } }

這樣咱們就實現了服務的註冊與調用。

 

那麼咱們去啓動服務進行測試,服務註冊成功,咱們去啓動服務調用端

服務調用端成功,

那麼咱們去測試下,咱們先去測試看單獨訪問服務是否正常

輸入http://localhost:8001/hello?name=liwanlei  

顯示

那麼咱們看下另一個調用這個服務的服務

http://localhost:8002/hello/name

那麼咱們看下返回

這樣咱們調試成功。

戶端已經成功的經過feign調用了遠程服務,而且將結果返回到了瀏覽器。

相關文章
相關標籤/搜索