springcloud的服務提供者與服務消費者


1、說明

springcloud中由服務消費者調用服務提供者一共有兩種方法rest和feign

2、feign

(1)使用feign的方式進行服務調,搭建服務提供者。

  1. 創建一個web項目(服務提供者)
  2. 修改pom文件
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  3. 在項目啟動類上加@EnableDiscoveryClient注解
  4. 添加配置文件
    spring.application.name=spring-cloud-producer
    server.port=9000
    #將服務注冊的地址
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
    
  5. 編寫測試代碼
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String index(@RequestParam String name) {
            return "這是服務提供者,參數:"+name;
        }
    }
    

     6、效果

(2)使用feign的方式進行服務調,搭建服務消費者。

  1. 創建一個web項目(服務消費者)
  2. 修改pom文件
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
    </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
         <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
         <version>1.4.4.RELEASE</version>
    </dependency>
    
  3. 在項目啟動類上加@EnableDiscoveryClient 啟動服務注冊和發現 @EnableFeignClients 啟用feign進行遠程調用 注解
  4. 添加配置文件
    spring.application.name=spring-cloud-consumer
    server.port=9001
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
    
  5. 編寫測試代碼

              5.1編寫調用接口

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

// name的值是服務提供者的配置文件中的spring.application.name
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}

            5.2編寫調用類

import com.comsuer.comsuer.Service.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {
    @Autowired
    private HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }

}

       6. 效果

      

 3、rest

springcloud使用rest+ribbon實現服務調用和服務提供者的負載均衡

(1)搭建服務提供者

  1. 創建一個web項目
  2. 修改pom文件
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  3. 在啟動類上加@EnableDiscoveryClient 注解
  4. 添加配置文件
    spring.application.name=spring-cloud-producer
    server.port=9000
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
    
  5. 編寫測試代碼
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @PostMapping("/hello")
        @ResponseBody
        public String index(@RequestBody String name) {
            return "第一個提供者"+name;
        }
    }
    
  6. 按照上面的五個步驟再構建一個服務提供者

(2)搭建服務消費者

  1. 創建一個web項目
  2. 修改配置文件
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-ribbon</artifactId>
       <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
      <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
       <artifactId>fastjson</artifactId>
       <version>1.2.49</version>
    </dependency>
    
  3. 在啟動類上添加@EnableDiscoveryClient 注解,並修改啟動類的代碼,修改如下
    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 ComsuerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ComsuerApplication.class, args);
        }
        // 實現負載均衡
        @Bean
        @LoadBalanced
        RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
  4. 添加配置文件
    spring.application.name=spring-cloud-consumer
    server.port=9001
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
    
  5. 編寫測試代碼
    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class helloController {
    
        @Autowired
        private RestTemplate rest;
        @PostMapping("/hello")
        @ResponseBody
        public String hello(String name){
            System.err.println(name);
            String url = "http://spring-cloud-producer/hello";
            User user = new User();
            user.setName(name);
            user.setId("1");
            String s1 = JSON.toJSONString(user);
            String s = rest.postForObject(url, s1, String.class);
            return s;
        }
    }
    

     

rest調用效果,會調一次一,調一次二

 

4.總結

feign方式的負載均衡和rest步驟基本一樣。


免責聲明!

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



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