1. 程式人生 > >JAVA ssm b2b2c多使用者商城系統原始碼(三):服務提供與呼叫

JAVA ssm b2b2c多使用者商城系統原始碼(三):服務提供與呼叫

上一篇文章我們介紹了eureka服務註冊中心的搭建,這篇文章介紹一下如何使用eureka服務註冊中心,搭建一個簡單的服務端註冊服務,客戶端去呼叫服務使用的案例。

案例中有三個角色:服務註冊中心、服務提供者、服務消費者,其中服務註冊中心就是我們上一篇的eureka單機版啟動既可,流程是首先啟動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。

服務提供

我們假設服務提供者有一個hello方法,可以根據傳入的引數,提供輸出“hello xxx,this is first messge”的服務

1、pom包配置

建立一個springboot專案,pom.xml中新增如下配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2、配置檔案

application.properties配置如下:

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

引數在上一篇都已經解釋過,這裡不多說。

3、啟動類

啟動類中新增@EnableDiscoveryClient註解

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

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

4、controller

提供hello服務

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

新增@EnableDiscoveryClient註解後,專案就具有了服務註冊的功能。啟動工程後,就可以在註冊中心的頁面看到SPRING-CLOUD-PRODUCER服務。

到此服務提供者配置就完成了。

服務呼叫

1、pom包配置

和服務提供者一致

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2、配置檔案

application.properties配置如下:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、啟動類

啟動類新增@EnableDiscoveryClient@EnableFeignClients註解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

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

}
  • @EnableDiscoveryClient :啟用服務註冊與發現
  • @EnableFeignClients:啟用feign進行遠端呼叫

Feign是一個宣告式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個介面,然後在上面添加註解,同時也支援JAX-RS標準的註解。Feign也支援可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支援了Spring MVC標準註解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支援負載均衡。

4、feign呼叫實現

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}
  • name:遠端服務名,及spring.application.name配置的名稱

此類中的方法和遠端服務中contoller中的方法名和引數需保持一致。

5、web層呼叫遠端服務

將HelloRemote注入到controller層,像普通方法一樣去呼叫即可。

@RestController
public class ConsumerController {

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

}

到此,最簡單的一個服務註冊與呼叫的例子就完成了。

整體程式碼結構如下:

完整專案的原始碼來源 

Spring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求: 一零叄八七七四六貳六