美文网首页
微服务 之 服务调用

微服务 之 服务调用

作者: 诺之林 | 来源:发表于2020-11-01 09:56 被阅读0次

本文的示例代码参考cloud-feign-provider / cloud-feign-consumer

Contents

Nacos Server

wget http://file.nuozhilin.site/5_macos/nacos-server-1.4.0-BETA.tar.gz

tar xf nacos-server-1.4.0-BETA.tar.gz && cd nacos

sh bin/startup.sh -m standalone

Cloud OpenFeign Provider

spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,alibaba-nacos-discovery,cloud-feign --build=gradle cloud-feign-provider && cd cloud-feign-provider
vim src/main/kotlin/com/example/cloudfeignprovider/DemoApplication.kt
package com.example.cloudfeignprovider

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.openfeign.EnableFeignClients

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}
vim src/main/resources/application.properties
server.port=8001
spring.application.name=cloud-feign-provider
spring.cloud.nacos.discovery.server-addr=http://127.0.0.1:8848
lb.name=node1
vim src/main/kotlin/com/example/cloudfeignprovider/DemoController.kt
package com.example.cloudfeignprovider

import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController {
    @Value("\${lb.name}")
    lateinit var name: String

    @GetMapping("/")
    fun demo(): String {
        return name
    }
}
./gradlew bootrun

curl localhost:8001
# node1
vim src/main/resources/application.properties
server.port=8002
spring.application.name=cloud-feign-provider
spring.cloud.nacos.discovery.server-addr=http://127.0.0.1:8848
lb.name=node2
./gradlew bootrun

curl localhost:8002
# node2

Cloud OpenFeign Consumer

spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,alibaba-nacos-discovery,cloud-feign --build=gradle cloud-feign-consumer && cd cloud-feign-consumer
vim src/main/kotlin/com/example/cloudfeignprovider/DemoApplication.kt
package com.example.cloudfeignconsumer

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.openfeign.EnableFeignClients

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}
vim src/main/resources/application.properties
server.port=9001
spring.application.name=cloud-feign-consumer
spring.cloud.nacos.discovery.server-addr=http://127.0.0.1:8848
vim src/main/kotlin/com/example/cloudfeignconsumer/DemoService.kt
package com.example.cloudfeignconsumer

import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.GetMapping

@FeignClient("cloud-feign-provider")
interface DemoService {
    @GetMapping("/")
    fun demo(): String
}
vim src/main/kotlin/com/example/cloudfeignconsumer/DemoController.kt
package com.example.cloudfeignconsumer

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController {
    @Autowired
    lateinit var demoService: DemoService

    @GetMapping("/")
    fun demo(): String {
        return demoService.demo() + " | from feign"
    }
}
./gradlew bootrun

curl localhost:9001
# node1 | from feign

curl localhost:9001
# node2 | from feign

相关文章

网友评论

      本文标题:微服务 之 服务调用

      本文链接:https://www.haomeiwen.com/subject/xkbkvktx.html