SpringCloud 세 번째 장 : 서비스 소비자 (척하기)

리본의 제거를 수행하려면

시작해야

  • EurekaService의 포트 번호 8100
  • EurekaClient * 2 개 포트 번호 8101,8102
  • 체하다 서비스 포트 번호 8105
  1. 서비스 체하다 SpringBoot라는 새로운 프로젝트를 만듭니다

  2. 다음 종속성을 추가하는 치어 파일을 수정

    • 스프링 클라우드 스타터 openfeign
    • 스프링 부팅 스타터 웹
    • 스프링 클라우드 스타터 넷플 릭스 - 유레카 - 클라이언트

    치어 파일을 다음과 같이

    1 
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    > 
    > < 프로젝트 의 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 >
    > < 부모 >
    > < 의 groupId > com.kthirty </ 의 groupId >
    > < artifactId를 >springboot 부모 </artifactId를 >
    > < 버전 > 0.0.1-SNAPSHOT </ 버전 >
    > < relativepath를 />
    > </ 부모 >
    > < artifactId를 > 서비스 꾀병 </ artifactId를 >
    > < 버전 > 0.0.1-SNAPSHOT </ 버전 >
    > < 포장 > 단지 </ 포장 >
    > < 이름 >서비스 체하다 </이름 >
    > < 설명 > 봄 부팅에 대한 데모 프로젝트 </ 설명 >
    > < 종속성 >
    > < 의존성 >
    > < 의 groupId > org.springframework.boot </ 의 groupId >
    > < artifactId를 > 스프링 부팅 스타터 웹 </ artifactId를 >
    > </ 의존성 >
    > < 의존성 >
    > <의 groupId > org.springframework.cloud </ 의 groupId >
    > < artifactId를 > 스프링 클라우드 스타터 넷플릭스 유레카 클라이언트 </ artifactId를 >
    > </ 의존성 >
    > < 의존성 >
    > < 의 groupId > org.springframework.cloud </ 의 groupId >
    > < artifactId를 > 스프링 클라우드 스타터 openfeign </ artifactId를 >
    > </ 의존성 >
    > < 의존성 >
    > < 의 groupId > org.springframework.boot </ 의 groupId >
    > < artifactId를 > 스프링 부팅 스타터 테스트 </ artifactId를 >
    > < 범위 > 시험 </ 범위 >
    > </ 의존성 >
    > </ 종속 >
    > </ 프로젝트 >
    >
  3. 구성 파일을 수정 Application.yml, 전체 파일 등록 서비스는 다음과 같이

    1 
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    > 봄 : 
    > 응용 프로그램 :
    > 이름 : 서비스-척하기
    >
    > 서버 :
    > 대형 열  SpringCloud 세 번째 장 : 서비스 소비자 (척하기) 포트 : 8105
    > 유레카 :
    > 클라이언트 :
    > 서비스 - URL :
    > defaultzone : HTTP : 로컬 호스트 // : 8100 / 유레카 /
    >
  4. 시작 클래스를 수정

    1 
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >package com.kthirty.servicefeign;
    >
    >import org.springframework.boot.SpringApplication;
    >import org.springframework.boot.autoconfigure.SpringBootApplication;
    >import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    >import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    >import org.springframework.cloud.openfeign.EnableFeignClients;
    >
    >@SpringBootApplication
    >@EnableDiscoveryClient
    >@EnableEurekaClient
    >@EnableFeignClients //开启Feign功能
    >public class {
    >
    > public static void main(String[] args) {
    > SpringApplication.run(ServiceFeignApplication.class, args);
    > }
    >}
    >
  5. 添加feign接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    >package com.kthirty.servicefeign.interfaces;
    >
    >import org.springframework.cloud.openfeign.FeignClient;
    >import org.springframework.web.bind.annotation.RequestMapping;
    >import org.springframework.web.bind.annotation.RequestMethod;
    >import org.springframework.web.bind.annotation.RequestParam;
    >
    >@FeignClient("client1")//指定调用服务名
    >public interface SchedualServiceHi {
    > @RequestMapping(value = "/hello",method = RequestMethod.GET)//服务链接
    > String sayHiFormClientOne(@RequestParam String name);
    >}
    >
  6. 创建HiController消费服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >package com.kthirty.servicefeign.web;
    >
    >import com.kthirty.servicefeign.interfaces.SchedualServiceHi;
    >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 HiController {
    > //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    > @Autowired
    > private SchedualServiceHi schedualServiceHi;
    > @RequestMapping("/hi")
    > public String sayHi(@RequestParam String name){
    > return schedualServiceHi.sayHiFormClientOne(name);
    > }
    >}
    >

启动测试

同上一篇将Ribbon服务替换为feign服务即可

访问链接[http://localhost:8105/hi?name=thirty]:http://localhost:8105/hi?name=thirty

추천

출처www.cnblogs.com/sanxiandoupi/p/11713221.html