标签:负载均衡 inf loading ada src lan 需要 名称 pass
启动类添加注解
@EnableFeignClients
添加依赖:spring-boot-starter-web
创建一个接口 RegisterApi
package com.test.UserAPI; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; /** * 用户操作相关接口 * @author 一明哥 * */ @RequestMapping("/User") public interface RegisterApi { @GetMapping("/isAlive") public String isAlive(); }
application.yml
eureka:
client:
service-url:
defaultZone:http://euk1.com:7001/eureka/
server:
port:81
spring:
application:
name:user-provider
Pom.xml添加依赖
<dependency> <groupId>com.mashibing.User-API</groupId> <artifactId>User-API</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
application.yml
eureka:
client:
service-url:
defaultZone:http://euk1.com:7001/eureka/
server:
port:90
spring:
application:
name:user-consumer
创建Service接口
package com.mashibing.UserConsumer; import org.springframework.cloud.openfeign.FeignClient; import com.test.UserAPI.RegisterApi; @FeignClient(name = "user-provider") public interface UserConsumerService extends RegisterApi { }
创建Controller
package com.mashibing.UserConsumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsumerController { @Autowired UserConsumerService consumerSrv; @GetMapping("/alive") public String alive() { return consumerSrv.isAlive(); } }
修改启动类
package com.mashibing.UserConsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class UserConsumerApplication { public static void main(String[] args) { SpringApplication.run(UserConsumerApplication.class, args); } }
访问 http://localhost:90/alive 即可完成声明式远程服务调用
Get和Post
Feign默认所有带参数的请求都是Post,想要使用指定的提交方式需引入依赖
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>
User-Provider的Pom.xml添加依赖
<dependency> <groupId>com.mashibing.User-API</groupId> <artifactId>User-API</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
创建UserController
package com.mashibing.UserProvider; import com.mashibing.UserAPI.RegisterApi; @RestController public class UserController implements RegisterApi { @Override public String isAlive() { // TODO Auto-generated method stub return "ok"; } }
并指明提交方式
@RequestMapping(value = "/alived", method = RequestMethod.POST)
@GetMapping("/findById")
带参请求
@GetMapping("/findById") public Map findById(@RequestParam("id") Integer id); @PostMapping("/register") public Map<String, String> reg(@RequestBody User user);
feign的默认配置类是:org.springframework.cloud.openfeign.FeignClientsConfiguration。默认定义了feign使用的编码器,解码器等。
允许使用@FeignClient的configuration的属性自定义Feign配置。自定义的配置优先级高于上面的FeignClientsConfiguration。
通过权限的例子,学习feign的自定义配置。
服务提供者,上述例子开放service-valuation的权限后访问。
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
WebSecurityConfig
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 关闭csrf http.csrf().disable(); // 表示所有的访问都必须认证,认证处理后才可以正常进行 http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated(); // 所有的rest服务一定要设置为无状态,以提升操作效率和性能 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } }
application.yml
spring:
security:
user:
name: root
password: root
继续feign原来访问,报错401
FeignAuthConfiguration
public class FeignAuthConfiguration { @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("root", "root"); } }
在feign上加配置
@FeignClient(name = "service-valuation",configuration = FeignAuthConfiguration.class)
如果在配置类上添加了@Configuration注解,并且该类在@ComponentScan所扫描的包中,那么该类中的配置信息就会被所有的@FeignClient共享。
最佳实践是:不指定@Configuration注解(或者指定configuration,用注解忽略)
@FeignClient(name = "service-valuation",configuration = FeignAuthConfiguration.class)
MyBasicAuthRequestInterceptor
import feign.RequestInterceptor; import feign.RequestTemplate; public class MyBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // TODO Auto-generated method stub template.header("Authorization", "Basic cm9vdDpyb290"); } }
application.yml
feign:
client:
config:
service-valuation:
request-interceptors:
- com.online.taxi.passenger.feign.interceptor.MyBasicAuthRequestInterceptor
代码中取消上面的配置,访问报401用下面的方式
接上面例子,此例子和上面例子实现的功能一样。记得两者取一个即可。说明用属性而不是用属性中的configuration。
定义拦截器
public class MyBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // TODO Auto-generated method stub template.header("Authorization", "Basic cm9vdDpyb290"); } }
application.yml
feign: client: config: service-valuation: request-interceptors: - com.online.taxi.passenger.feign.interceptor.MyBasicAuthRequestInterceptor
再次访问,测试Ok
指定服务名称配置
application.yml
feign:
client:
config:
service-valuation:
connect-timeout: 5000
read-timeout: 5000
logger-level: full
通用配置
feign:
client:
config:
default:
connect-timeout: 5000
read-timeout: 5000
logger-level: full
属性配置比Java代码优先级高。也可通过配置设置java代码优先级高。
feign:
client:
default-to-properties: false
server.compression.enabled=true
标签:负载均衡 inf loading ada src lan 需要 名称 pass
原文地址:https://www.cnblogs.com/YC-L/p/14407043.html