eureka配置服务端与客户端

Eureka服务端与客户端注册流程

父工程的pom.xml文件中导入SpringCloud依赖的包
<dependencyManagement>
   <dependencies>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.SR1</version>
        <type>pom</type>
        <scope>import</scope>
     </dependency>
   </dependencies>
</dependencyManagement>
一.Eureka服务端配置操作流程

1.首先创建Eureka工程

导入pom.xml文件

 <!--eureka服务端的配置文件 --> 
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.编写eureka的启动类文件

/**
 * Eure服务端的启动类
 */
@SpringBootApplication
@EnableEurekaServer /*Eureka的服务端*/
public class EureApplication {
    public static void main(String[] args) {
        SpringApplication.run(EureApplication.class);
    }
}

3.编写eureka的application.yml或 properties 配置文件

server:
  port: 10086 #服务端口
eureka:
  client:
    registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需注册
    fetchRegistry: false #是否从Eureka中获取注册信息
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
      defaultZone: http://127.0.0.1:${server.port}/eureka/
Eureka其它服务端需要的配置

1.找到其它微服务打开pom.xml文件

<!-- eureka服务端的配置文件 -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.其它微服务的application.yml配置文件中添加配置信息将微服务注册到eureka注册中心

#微服务注册到eureka配置
#将其它微服务注册到eureka注册中心中
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka/

3.找到其它微服务在启动类上添加@EnableEurekaClient注解

//例如给员工微服务添加eureka客户端注解
//1.配置springboot的包扫描
@SpringBootApplication(scanBasePackages = "com.ihrm")
//2.配置jpa注解的扫描
@EntityScan("com.ihrm.domain.employee")
//3.注册到eureka的注解
@EnableEurekaClient
public class EmployeeApplication {
    //.....main方法等略
}
微服务调用组件Feign

谁去调用其它微服务就往谁身上配置依赖/注解等

1.谁调用别人就在谁身上添加依赖(例如系统微服务调用企业微服务就在系统微服务上添加依赖)

<dependency>
    <groupId>org.springframework.cloud</groupId>                
    <artifactId>spring-cloud-starter-openfeign</artifactId>             
</dependency>

2.修改系统微服务的启动类添加 @EnableDiscoveryClient 和@EnableFeignClients 注解

//springboot的包扫描
@SpringBootApplication(scanBasePackages = "com.ihrm")
//JPA对应的包扫描
@EntityScan(value = "com.ihrm.domain.system")
//3.注册到eureka
@EnableEurekaClient
//feign组件的接口的注解
@EnableDiscoveryClient
@EnableFeignClients
public class SystemApplication {
    main方法...}

3.在系统微服务下模块创建com.ihrm.system.client包,包下创建接口

//@FeignClient注解用于指定从哪个服务中调用功能 ,注意里面的名称与被调用的服务名保持一致
//声明的调用微服务名称
@FeignClient(value = "ihrm-company")
public interface DepartmentFeignClient {
//@RequestMapping注解用于对被调用的微服务进行地址映射
    //调用微服务的接口
	@RequestMapping(value = "/company/departments/{id}/", method = RequestMethod.GET)
	public Department findById(@PathVariable("id") String id) throws Exception;
}

4.配置一个feign的拦截器

@Configuration
public class FeignConfiguration {
    //配置feign拦截器,解决请求头的问题
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor(){
            //获取浏览器发送的所有请求属性,请求头赋值到feign
            public void apply(RequestTemplate requestTemplate){
               //请求属性
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes != null){
                    HttpServletRequest request = attributes.getRequest();
                    //获取浏览器发起的请求头
                    Enumeration<String> headerNames = request.getHeaderNames();
                    if (headerNames != null){
                        while (headerNames.hasMoreElements()){
                            String name = headerNames.nextElement();//请求头名称 Authorization
                            String value = request.getHeader(name);//请求头数据 "Bearer b1----"
                            requestTemplate.header(name,value);
                        }
                    }
                }
            }
        };
    }
}

5.测试

	/*注入刚才写的哪个接口*/
	@Autowired
    private DepartmentFeignClient departmentFeignClient;
	//写一个测试方法.....    
	/*
     * 测试Feign组件
     * 调用系统微服务的/test接口传递部门id,通过feign调用部门微服务获取部门信息
     */
    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public Result testFeign(@PathVariable(value = "id") String id) {
        Result result = departmentFeignClient.findById(id);
        return result;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值