Scenes
SpringCloud- service registration and implementation -Eureka create a service registry (with source code download):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957
Service registration and implementation SpringCloud- -Eureka create a service provider (with source code download):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558004
SpringCloud- create a service consumer -Ribbon way (with code to download):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080
In the above has been implemented service registry, service providers and to Ribbon manner under the premise of consumer services, Feign use another way to achieve the service consumer.
Feign
Feign is a pseudo Http client a declarative, it makes writing easier Http client. Using Feign, only you need to create an interface and annotation. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes. Feign pluggable encoder and decoder. Feign default integrated Ribbon, and Eureka and combined default implementation of load balancing
Note:
Blog:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.
achieve
Construction of the project refer to the above manner, in order to create a directory hello-spring-cloud-web-admin-feign directory as well as in
New pom.xml directory, and hosting it. Then New src / main / java directory and src / main / resources directory and directory were provided.
Then a new package in java, start a new package under the category, the new configuration file at application.yml resources.
After the completion of the directory is:
pom.xml Code:
<?xml version="1.0" encoding="UTF-8"?> <project 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> <parent> <groupId>com.badao</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-web-admin-feign</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-web-admin-feign</name> <url>https://blog.csdn.net/badao_liumang_qizhi</url> <inceptionYear>2019-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Spring Cloud End --> <! - solve thymeleaf template engine must be strict check html5 format problem -> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.badao.hello.spring.cloud.web.admin.feign.WebAdminFeignApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
Note:
Here's parent label them to correspond with the above unified dependency management.
To modify the entry specified program category for their respective paths.
Then start the application class code:
package com.badao.hello.spring.cloud.web.feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class WebAdminFeignApplication { public static void main(String[] args) { SpringApplication.run(WebAdminFeignApplication.class, args); } }
Note:
Registration to the service center through @EnableDiscoveryClient comment
Feign annotation feature is turned on by @EnableFeignClients
Then the configuration file application.yml
spring: application: name: hello-spring-cloud-web-admin-feign thymeleaf: cache: false mode: LEGACYHTML5 encoding: UTF-8 servlet: content-type: text/html server: port: 8765 eureka: client: ServiceUrl: defaultZone: http://localhost:8761/eureka/
Note:
1. Service registration and discovery is to find the light of the above name.
2.port the port number.
3.serviceURL set address eureka, the above URL is created when the corresponding service registry.
And different ways of using the Ribbon is here to create service interfaces, rather than the service class.
AdminService interface code:
package com.badao.hello.spring.cloud.web.feign.service; 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(value = "hello-spring-cloud-service-admin") public interface AdminService { @RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(@RequestParam(value = "message") String message); }
Note:
By @FeignClient (value = "hello-spring-cloud-service-admin") to specify which service calls.
Here is the corresponding name attribute profile of the above service providers.
import com.badao.hello.spring.cloud.web.feign.service.AdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class AdminController { @Autowired private AdminService adminService; @RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(@RequestParam String message) { return adminService.sayHi(message); } }
In order to reflect the load balancing effect, we have to start two service-admin, namely to start two service providers.
Let's start the service registry service 8761 Eureka port, then the port 8762 to start a service provider, and then click Run-Edit Configuration, will start a single-instance removed.
Then modify the service provider configuration file for the port number 8763, then start a service provider.
消费者要想实现负载均衡的效果,应该一会访问8762的服务提供者,一会访问8763的服务提供者。
然后运行当前服务消费者的启动程序。
打开浏览器输入:
http://localhost:8765/hi?message=HelloFeign
此时的架构
一个服务注册中心,Eureka Server,端口号为:8761
service-admin 工程运行了两个实例,端口号分别为:8762,8763
web-admin-feign 工程端口号为:8765
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11867357