From d2373c4dc5af06fe35fc1386c1d7bb741e00abc7 Mon Sep 17 00:00:00 2001 From: creeper Date: Sun, 7 Jul 2024 16:47:29 +0800 Subject: [PATCH 1/8] Site updated: 2024-07-07 16:47:29 --- 2024/07/07/hello-world/index.html | 205 ------------------ .../index.html" | 193 +++++++++++++++++ archives/2024/07/index.html | 8 +- archives/2024/index.html | 8 +- archives/index.html | 8 +- index.html | 26 +-- 6 files changed, 212 insertions(+), 236 deletions(-) delete mode 100644 2024/07/07/hello-world/index.html create mode 100644 "2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" diff --git a/2024/07/07/hello-world/index.html b/2024/07/07/hello-world/index.html deleted file mode 100644 index 75d09cd..0000000 --- a/2024/07/07/hello-world/index.html +++ /dev/null @@ -1,205 +0,0 @@ - - - - - - - Hello World | Creeper Blog - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
-
- -
- - -
- - -

- Hello World -

- - -
- -
- -

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

-

Quick Start

Create a new post

1
$ hexo new "My New Post"
- -

More info: Writing

-

Run server

1
$ hexo server
- -

More info: Server

-

Generate static files

1
$ hexo generate
- -

More info: Generating

-

Deploy to remote sites

1
$ hexo deploy
- -

More info: Deployment

- - -
- -
- - - -
- - -
- - - -
-
- -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - -
- - \ No newline at end of file diff --git "a/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" "b/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" new file mode 100644 index 0000000..aebcef0 --- /dev/null +++ "b/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" @@ -0,0 +1,193 @@ + + + + + + + 测试发布文章 | Creeper Blog + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + +
+
+ +
+ + +
+ + +

+ 测试发布文章 +

+ + +
+ +
+ +

Creeper Blog 测试发布文章

测试内容

+ + +
+ +
+ + + +
+ + +
+ + + +
+
+ +
+ +
+
+ +
+ + + + + + + + + + + + + + + + + + + + +
+ + \ No newline at end of file diff --git a/archives/2024/07/index.html b/archives/2024/07/index.html index d8a52f0..134bd71 100644 --- a/archives/2024/07/index.html +++ b/archives/2024/07/index.html @@ -83,13 +83,13 @@

-

- Hello World + 测试发布文章

@@ -131,7 +131,7 @@

letzter Beitrag

diff --git a/archives/2024/index.html b/archives/2024/index.html index f374f00..1fab0e6 100644 --- a/archives/2024/index.html +++ b/archives/2024/index.html @@ -83,13 +83,13 @@

-

- Hello World + 测试发布文章

@@ -131,7 +131,7 @@

letzter Beitrag

diff --git a/archives/index.html b/archives/index.html index 28e2a22..08d0001 100644 --- a/archives/index.html +++ b/archives/index.html @@ -83,13 +83,13 @@

-

- Hello World + 测试发布文章

@@ -131,7 +131,7 @@

letzter Beitrag

diff --git a/index.html b/index.html index 61eb931..769b1e8 100644 --- a/index.html +++ b/index.html @@ -69,10 +69,10 @@

-
+
@@ -83,7 +83,7 @@

- Hello World + 测试发布文章

@@ -91,24 +91,12 @@

-

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

-

Quick Start

Create a new post

1
$ hexo new "My New Post"
- -

More info: Writing

-

Run server

1
$ hexo server
- -

More info: Server

-

Generate static files

1
$ hexo generate
- -

More info: Generating

-

Deploy to remote sites

1
$ hexo deploy
- -

More info: Deployment

+

Creeper Blog 测试发布文章

测试内容

- Teilen + Teilen @@ -151,7 +139,7 @@

letzter Beitrag

From a9106342c52d08b3ed834441f704c033abb1d971 Mon Sep 17 00:00:00 2001 From: creeper Date: Fri, 26 Jul 2024 11:05:26 +0800 Subject: [PATCH 2/8] Site updated: 2024-07-26 11:05:26 --- .../index.html" | 18 ++ .../index.html" | 214 ++++++++++++++++++ archives/2024/07/index.html | 23 ++ archives/2024/index.html | 23 ++ archives/index.html | 23 ++ index.html | 50 ++++ 6 files changed, 351 insertions(+) create mode 100644 "2024/07/26/\344\275\277\347\224\250Spring-Cloud-Feign\346\236\204\345\273\272\345\243\260\346\230\216\345\274\217\345\256\242\346\210\267\347\253\257/index.html" diff --git "a/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" "b/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" index aebcef0..4b276dc 100644 --- "a/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" +++ "b/2024/07/07/\346\265\213\350\257\225\345\217\221\345\270\203\346\226\207\347\253\240/index.html" @@ -106,6 +106,20 @@

+ + + Neuer +
+ + 使用Spring Cloud Feign构建声明式客户端 + +
+
+ + + +

@@ -138,6 +152,10 @@

letzter Beitrag

    +
  • + 使用Spring Cloud Feign构建声明式客户端 +
  • +
  • 测试发布文章
  • diff --git "a/2024/07/26/\344\275\277\347\224\250Spring-Cloud-Feign\346\236\204\345\273\272\345\243\260\346\230\216\345\274\217\345\256\242\346\210\267\347\253\257/index.html" "b/2024/07/26/\344\275\277\347\224\250Spring-Cloud-Feign\346\236\204\345\273\272\345\243\260\346\230\216\345\274\217\345\256\242\346\210\267\347\253\257/index.html" new file mode 100644 index 0000000..99c36b4 --- /dev/null +++ "b/2024/07/26/\344\275\277\347\224\250Spring-Cloud-Feign\346\236\204\345\273\272\345\243\260\346\230\216\345\274\217\345\256\242\346\210\267\347\253\257/index.html" @@ -0,0 +1,214 @@ + + + + + + + 使用Spring Cloud Feign构建声明式客户端 | Creeper Blog + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + +
    +
    + +
    + + +
    + + +

    + 使用Spring Cloud Feign构建声明式客户端 +

    + + +
    + +
    + +

    在使用Spring Boot开发微服务后端时,有时需要向其他微服务发起HTTP请求。如果使用传统的RestTemplate方法,先构造HTTP请求,再解析返回的HTTP响应,会产生很多冗余代码,降低项目的可读性。Spring Cloud Feign可以很好地解决这个问题。Feign是一种声明式、模板化的HTTP客户端,开发者只需要定义接口,便可以向调用本地方法一样调用远程接口,非常简洁方便。
    此处Spring Boot项目使用maven构建,在pom.xml中加入依赖并安装:

    +
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>4.1.1</version>
    </dependency>
    +

    在Spring Boot的主文件中加入@EnableFeignClients注解:

    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @SpringBootApplication
    @EnableFeignClients
    public class UniGptBotServiceApplication {

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

    }
    +

    创建一个client目录,用于放置所有Feign客户端类,例如,我需要向微服务user-service发送远程请求,则创建一个UserServiceClient类:

    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.unigpt.bot.client;

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestHeader;

    @Component
    @FeignClient(name = "user-service", url = "http://localhost:8082/internal")
    public interface UserServiceClient {

    @DeleteMapping("/users/used-bots/{botId}")
    public void deleteBotFromUsedList(
    @PathVariable Integer botId,
    @RequestHeader(name = "X-User-Id") Integer userId);
    }
    +

    此处,我们定义了UserServiceClient接口,@FeignClient注解定义了服务的名称url,接口的函数定义与controller类似,可以使用注解规定路径参数、请求参数、请求头、请求体信息。Feign会根据定义的接口和注解自动生成实现。

    +

    定义完客户端接口类后,依赖注入UserServiceClient类,即可使用deleteBotFromUsedList方法,像调用本地方法一样调用远程服务。

    + + +
    + +
    + + + + + +
    + + +
    + + + +
    +
    + +
    + +
    +
    + +
    + + + + + + + + + + + + + + + + + + + + +
    + + \ No newline at end of file diff --git a/archives/2024/07/index.html b/archives/2024/07/index.html index 134bd71..01cd85f 100644 --- a/archives/2024/07/index.html +++ b/archives/2024/07/index.html @@ -80,6 +80,25 @@

+ + + +
@@ -130,6 +149,10 @@

letzter Beitrag

+ + + +
@@ -130,6 +149,10 @@

letzter Beitrag

+ + + +
@@ -130,6 +149,10 @@

letzter Beitrag

    +
  • + 使用Spring Cloud Feign构建声明式客户端 +
  • +
  • 测试发布文章
  • diff --git a/index.html b/index.html index 769b1e8..26ed0be 100644 --- a/index.html +++ b/index.html @@ -69,6 +69,52 @@

    +
    + +
    + + +
    + + +

    + 使用Spring Cloud Feign构建声明式客户端 +

    + + +
    + +
    + +

    在使用Spring Boot开发微服务后端时,有时需要向其他微服务发起HTTP请求。如果使用传统的RestTemplate方法,先构造HTTP请求,再解析返回的HTTP响应,会产生很多冗余代码,降低项目的可读性。Spring Cloud Feign可以很好地解决这个问题。Feign是一种声明式、模板化的HTTP客户端,开发者只需要定义接口,便可以向调用本地方法一样调用远程接口,非常简洁方便。
    此处Spring Boot项目使用maven构建,在pom.xml中加入依赖并安装:

    +
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>4.1.1</version>
    </dependency>
    +

    在Spring Boot的主文件中加入@EnableFeignClients注解:

    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @SpringBootApplication
    @EnableFeignClients
    public class UniGptBotServiceApplication {

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

    }
    +

    创建一个client目录,用于放置所有Feign客户端类,例如,我需要向微服务user-service发送远程请求,则创建一个UserServiceClient类:

    +
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.unigpt.bot.client;

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestHeader;

    @Component
    @FeignClient(name = "user-service", url = "http://localhost:8082/internal")
    public interface UserServiceClient {

    @DeleteMapping("/users/used-bots/{botId}")
    public void deleteBotFromUsedList(
    @PathVariable Integer botId,
    @RequestHeader(name = "X-User-Id") Integer userId);
    }
    +

    此处,我们定义了UserServiceClient接口,@FeignClient注解定义了服务的名称url,接口的函数定义与controller类似,可以使用注解规定路径参数、请求参数、请求头、请求体信息。Feign会根据定义的接口和注解自动生成实现。

    +

    定义完客户端接口类后,依赖注入UserServiceClient类,即可使用deleteBotFromUsedList方法,像调用本地方法一样调用远程服务。

    + + +
    + +
    + +
    + + + +